【软件自动化测试-QTP实战技能 8】== QTP中页面的基本异常检查

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

如下图就是一个典型的信息录入页面

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

名称不能包含特殊字符,固定的电话格式,固定的手机格式,银行账号(必须全为数字),付费号码长度限制,手机号码格式、长度限制,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 bjStr = CreateObject("Scripting.Dictionary")

             set Script. = 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 bjStr = Nothing

             set Script. = 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

你可能感兴趣的:(【软件自动化测试-QTP实战技能 8】== QTP中页面的基本异常检查)