分三个函数来处理:
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录制执行脚本,输入默认正确数值后,将相应字段修改为异常数值后提交,再判断是否出现相应的异常提示。
(函数省略)
通过以上方法,可以完整覆盖页面输入的异常判断,减轻人工测试覆盖不足的问题。