技术开发 频道

使用QTP检查页面中的基本异常

  【IT168 技术文档】页面异常输入测试是系统测试的一个重要方面,但是因为比较繁杂,人工测试时往往难以完全覆盖,此时采用自动化测试,就有相当的优势了。

  其中就包括很多异常输入测试点:

  名称不能包含特殊字符,固定的电话格式,固定的手机格式,银行账号(必须全为数字),付费号码长度限制,手机号码格式、长度限制,Email格式限制等

  其实异常策略是有通用性的,现总结如下:

  1.exp@@SpecialChar(e_chr) 包含特殊字符,e_chr为指定的特殊字符

  2.exp@@StringLenMax(e_len) 字符串超过最大长度,e_len为最大长度值

  3.exp@@StringLenMin(e_len) 字符串不足最小长度,e_len为最小长度值

  4.exp@@String 字符串包含数字(要求全字符)

  5.exp@@Num 字符串包含字符(要求全数字)

  6.exp@@NumMax(e_max) 输入超过指定最大值,e_max,最大值

  6.exp@@NumMin(e_min) 输入不足指定最大值,e_mmin,最小值

  7.exp@@NumLenMax(e_len) 数值长度超限,e_len,长度最大值

  8.exp@@NumLenMin(e_len) 数值长度不足,e_len,长度最小值

  9.exp@@StringPre(e_pre) 字符串不为指定前缀,e_pre,前缀值

  10.exp@@NumPre(e_pre) 数值不使用指定前缀,e_pre,前缀值

  11.exp@@Email 数值不为标准Email格式

  12.exp@@Need 必输入项,提供空字符串

  具体实现上,在准备数据文件时,提供两种数据:

  1. 每个字段的默认输入值,即正确值。也是自动化程序执行时默认输入的数据。

  2. 异常值,针对每个字段,提供该字段可能需要的异常策略,对所有异常进行遍历。

  第一行为默认值,第二行为字段对应的异常策略。

  分三个函数来处理:

  1. 执行函数

  Public Sub excep_OpUser()

  routingname = "企业用户输入-异常测试"

  ' 载入数据表数据的第2行

  GE_SetCurRow OppDataFile,"企业用户",2

  Dim objStr,oScript,i

  set objStr = CreateObject("Scripting.Dictionary")

  set oScript = CreateObject("Scripting.Dictionary")

  ‘ 根据数据表字段设置每个字段的QTP执行语句

  for i = 1 to DataTable.GetSheet("UserData").GetParameterCount

  if DataTable.GetSheet("UserData").GetParameter(i)="企业名称" Then _

  objStr.add DataTable.value(i,"UserData"),".WebEdit("opername").set "

  ...

  Next

  ‘ 调用异常数据生成函数,返回值保存在dictionary对象oScript中

  Gen_excepData(objStr,oScript)

  ‘ 依次对含异常策略的字段进行异常测试

  For i = 0 to oScript.Count-1

  Call OpUserSet(1,oScript(i))

  Next

  set objStr = Nothing

  set oScript = Nothing

  End Sub

  2. 异常数据生成函数

  Public Function Gen_excepData(byval dObj,byRef oScript)

  ‘ 异常值,正确值,异常类型,异常参数

  Dim errorValue,CorrectValue,expType,expPara

  Dim objName,scriptstr

  Dim i,j,expItem

  expItem = 0 ‘ 记录异常数

  Randomize

  for i = 1 to DataTable.GetSheet("UserData").GetParameterCount

  ‘ 第一行数据为默认正确数值

  CorrectValue = DataTable.GetSheet("UserData").GetParameter(i).ValueByRow(1)

  datatable.SetCurrentRow 2

  if Instr(Datatable.value(i,"UserData"),"exp@@")>0 Then

  objName = dObj.Item(Datatable.value(i,"UserData"))

  ‘ 同一字段多种异常策略的处理

  arr1 = split (Datatable.value(i,"UserData"),"|",-1,1)

  For j = 0 to Ubound(arr1)

  ‘ 获取异常类型和异常参数

  If RegExpTest("exp@@.{1,}\(.{1,}\)",arr1(j)) Then

  exptype = Mid(arr1(j),6,InStr(arr1(j),"(")-6)

  expPara = Mid(arr1(j),InStr(arr1(j),"(")+1,InStr(arr1(j),")")-InStr(arr1(j),"(")-1)

  Else

  expType = replace(arr1(j),"exp@@","")

  End If

  ‘ 不同策略对应的数据生成

  select Case expType

  Case "SpecialChar"

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"%")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"'")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"/")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),":")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"*")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"&")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"?")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"""")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"<")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),">")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"|")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),"(")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  errorValue = Replace(CorrectValue,Right(CorrectValue,1),")")

  scriptstr = objName + chr (34) + errorValue + chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "StringLenMax"

  errorValue = CorrectValue

  Dim n

  For n = 1 to expPara+1-Len(CorrectValue)

  errorValue = errorValue & chr(Int(26 * Rnd + 97))

  Next

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "StringLenMin"

  errorValue = Left(CorrectValue,expPara-1)

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "String"

  errorValue = ""

  Dim n

  For n = 1 to Len(CorrectValue)

  errorValue = errorValue & chr(Int(10 * Rnd + 48))

  Next

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "Num"

  errorValue = Left(CorrectValue,Len(CorrectValue)-1) & chr(Int(26 * Rnd + 97))

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "NumMax"

  errorValue = expPara + 1

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "NumMin"

  errorValue = expPara - 1

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "NumLenMax"

  errorValue = CorrectValue

  Dim n

  For n = 1 to expPara+1-Len(CorrectValue)

  errorValue = errorValue & chr(Int(10 * Rnd + 48))

  Next

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "NumLenMin"

  errorValue = Left(CorrectValue,expPara-1)

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "StringPre"

  Dim n,tmpStr

  tmpStr = ""

  For n = 1 to Len(expPara)

  tmpStr = tmpStr & chr(Int(26 * Rnd + 97))

  Next

  errorValue = Replace(CorrectValue,expPara,tmpStr)

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "NumPre"

  Dim n,tmpStr

  tmpStr = ""

  For n = 1 to Len(expPara)

  tmpStr = tmpStr & chr(Int(10 * Rnd + 48))

  Next

  errorValue = Replace(CorrectValue,expPara,tmpStr)

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "Email"

  errorValue = Replace(CorrectValue,"@",chr(Int(26 * Rnd + 97)))

  errorValue = Replace(errorValue,".",chr(Int(26 * Rnd + 97)))

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  Case "Need"

  errorValue = ""

  scriptstr = objName & chr(34) & errorValue & chr(34)

  expItem = expItem + 1

  oScript.add "excepData" & expItem,scriptstr

  End Select

  Next

  End If

  Next

  End Function

  3. 页面输入函数

  即相应的QTP录制执行脚本,输入默认正确数值后,将相应字段修改为异常数值后提交,再判断是否出现相应的异常提示。

  (函数省略)

  通过以上方法,可以完整覆盖页面输入的异常判断,减轻人工测试覆盖不足的问题。

0
相关文章