首页 > 代码库 > 自动化测试(三):QTP参数化

自动化测试(三):QTP参数化

1 Datatable参数化

         Global表的数据可以被所有的action访问,Action的数据只能被对应的Action访问

         本地表循环的次数设置:Action Call Properties

         Global表循环的次数设置:File -> Test Settings -> Run

 

注:

         ① Global全局变量循环一次,本地Action表全部运行一次

         ② 删除表的内容:Edit -> Delete

         ③ 修改列名:鼠标右击对应的列

 

Example4Flight程序登录验证

Data Table

 

username

password

errmsg

1

 

mercury

Please enter agent name

2

mer

mercury

Agent name must be at least 4 characters long

3

mercury

 

Please enter passwoed

4

mercury

mercury

null

Text

dialog("Login").WinEdit("Agent Name:").Set datatable("username","login")

dialog("Login").WinEdit("Password").Set datatable("password","login")

dialog("Login").WinButton("OK").Click

expected_result = datatable("errmsg","login")

If dialog("Login").Dialog("Flight Reservations").Exist Then

         actual_result = dialog("Login").Dialog("Flight Reservations").Static("errmsg").GetROProperty("text")

if expected_result = actual_result Then

                   reporter.ReportEvent micPass, "登陆验证","输入错误的用户名和密码,弹出正确的提示信息!"

else

                   reporter.ReportEvent micFail, "登陆验证","输入错误的用户名和密码,弹出错误的提示信息!"

End if

dialog("Login").Dialog("Flight Reservations").WinButton("确定").Click

else

         If expected_result = "null" Then

                   If window("Fight Reservations").Exist Then

                            Reporter.ReportEvent micPass, "登陆验证","输入正确的用户名和密码,登陆系统成功!"

                   else

                            Reporter.ReportEvent micFail, "登陆验证","输入正确的用户名和密码,登陆系统失败!"

                   End If

         else

                   Reporter.ReportEvent micFail, "登陆验证","输无效的用户名和密码,系统没有任何响应!"

         End If

End If

 

Practice2Agileone自动登录验证

Data Table

username

password

errmsg

 

admin

出错啦: 用户名不能为空 ...

adm

admin

出错啦: 找不到该用户名 ...

admin

adm

出错啦: 密码输入错误 ...

admin

admin

null

Text

Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebEdit("WebEdit").Set  datatable("username","Action1")

Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebEdit("WebEdit_2").Set datatable( "password","Action1")

Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").Image("loginbt").Click

expected_result = datatable("errmsg","Action1")

If  Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebElement("errmsg").Exist Then

         actual_result = Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebElement("errmsg").GetROProperty("innertext")

         If  expected_result = actual_result Then

                   reporter.ReportEvent micPass,"登陆验证","输入错误的用户名和密码,提示正确的提示信息!"

                   else

                   reporter.ReportEvent micFail,"登陆验证","输入错误的用户名和密码,提示错误的提示信息!"

         End If

else

         If expected_result = "null" Then

                   If  Browser("AgileOne - Welcome to").Page("AgileOne - Power to Agile").WebElement("wellcome").Exist Then

         reporter.ReportEvent micPass,"登陆验证","输入正确的用户名和密码,登陆系统成功!"

         Browser("AgileOne - Welcome to").Page("AgileOne - Power to Agile").Link("exit").Click

         else

         reporter.ReportEvent micFail,"登陆验证","输入正确的用户名和密码,系统登陆失败!"

                   End If

                   else

                   reporter.ReportEvent micFail,"登陆验证","输入无效的用户名和密码,系统无响应!"

         End If

End If

2 Excle参数化

利用datatable对象导入

         Import:将指定的Excle的所有数据导入,第一个sheet导入Global,第二个导入Action1,以此类推

         ImportSheet:可将指定的sheet页导入到指定的表中,第一行默认导入为列标题

         QTP只支持Excle2003,将Excle另存为Excle2003

Example1datatabe.ImportSheet"C:\123.xls","Sheet1","Action1" (在代码开始前加入)

 

通过环境变量

         环境变量设置:File -> Setting -> Environment -> Variable:User-defined 添加环境变量,用户定义的环境变量,需要自己定义变量名和值,定义好后就可以用这些变量去参数化脚本中的常量。

Example2Dialog("Login").WinEdit("Agent Name:").Set Environment("password")

 

Example3Excle读取、写入操作(Filith程序)

datatable

username

password

errmsg

actual_result

test_result

 

mercury

Please enter agent name

 

 

mer

mercury

Agent name must be at least 4 characters long

 

 

mercury

 

Please enter passwoed

 

 

mercury

mercury

null

 

 

Text

datatabe.ImportSheet"C:\123.xls","login","Action1"

For i=1 to datatable.GetSheet("Action1").GetRowCount

         dialog("Login").WinEdit("Agent Name:").Set datatable("username","Action1")

         dialog("Login").WinEdit("Password").Set datatable("password","Action1")

         dialog("Login").WinButton("OK").Click

         If dialog("Login").Dialog("Flight Reservations").Exist Then

                   expected_result = datatable("errmsg","login")

                   actual_result = dialog("Login").Dialog("Flight Reservations").Static("errmsg").GetROProperty("text")

                   if expected_result = actual_result Then

                            datatable("actual_result", "Action1")=actual_result

                            datatable("test_result", "Action1")="pass"

                   else

                            datatable("actual_result", "Action1")=actual_result

                            datatable("test_result", "Action1")="Fail"

                   End If

                   dialog("Login").Dialog("Flight Reservations").WinButton("确定").Click

         End If

         datatable.GetSheet("Action1").SetNextRow

Next

datatable.Export"C:\Flight_result.xls"

 

Practice1Agileone登录读取、写入Excle(window7,需调试)

写入Excle的函数:

Function QTP_WriteExcel(sExcelName,SheetNum,x,y,Content)

Set xlsobj=createobject("excel.application")

Set xlsbook=xlsobj.Workbooks.Open(sExcelName)

Set xlssheet=xlsbook.Sheets(SheetNum)

xlssheet.cells(x,y)=Content

xlsbook.Save

xlsbook.Close

End Function

调用:QTP_WriteExcel"C:\Users\In_october\Desktop\agileone.xls","sheet2",5,5,"abcde"

Text

Function QTP_WriteExcel(sExcelName,SheetNum,x,y,Content)

Set xlsobj=createobject("excel.application")

Set xlsbook=xlsobj.Workbooks.Open(sExcelName)

Set xlssheet=xlsbook.Sheets(SheetNum)

xlssheet.cells(x,y)=Content

xlsbook.Save

xlsbook.Close

End Function

 

Col=5

Row=2

datatable.ImportSheet"C:\Users\In_october\Desktop\agileone.xls","Sheet1","Action1"

Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebEdit("WebEdit").Set  datatable("username","Action1")

Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebEdit("WebEdit_2").Set datatable( "password","Action1")

Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").Image("loginbt").Click

expected_result = datatable("errmsg","Action1")

If  Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebElement("errmsg").Exist Then

         actual_result = Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebElement("errmsg").GetROProperty("innertext")

         QTP_WriteExcel"C:\Users\In_october\Desktop\agileone.xls","sheet2",Row,Col,actual_result

         Row=Row+1

         If  expected_result = actual_result Then

                   reporter.ReportEvent micPass,"登陆验证","输入错误的用户名和密码,提示正确的提示信息!"

                   else

                   reporter.ReportEvent micFail,"登陆验证","输入错误的用户名和密码,提示错误的提示信息!"

         End If

else

         If expected_result = "null" Then

                   If  Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").WebElement("wellcome").Exist Then

         reporter.ReportEvent micPass,"登陆验证","输入正确的用户名和密码,登陆系统成功!"

         Browser("AgileOne - Welcome to").Page("AgileOne - Welcome to").Link("exit").Click

         else

         reporter.ReportEvent micFail,"登陆验证","输入正确的用户名和密码,系统登陆失败!"

                   End If

                   else

                   reporter.ReportEvent micFail,"登陆验证","输入无效的用户名和密码,系统无响应!"

         End If

End If

3 对象库管理

保存共享数据库:Action1的对象数据库 -> File -> Export Local Objects -> Save

关联共享数据库:Action2关联Action1的对象数据库:右键点击Action2 –> Association Repository to local -> 选择文件,Open,关联的对象数据库不能修改

关联的对象数据库修改:1.本地修改,加入到本地对象数据库 2.全局修改,在Objict Repository中修改

Action管理:调用Action  增加Action  划分Action  Action管理的原则

Action管理的原则:

         1.每个Action其实就是一个用户场景,如登录,订票,退出等

         2.每个业务流程由若干个用户场景组成,即由若干个Action组成,在业务流程测试时需要首先分析该流程可以划分为多少个用户场景

         3.Action划分的粒度以是否方便作为可复用Action被其他业务流程复用为标准,不能太粗也不能太细

 

Example1订票业务场景(Flight程序)

         订票业务场景,划分为3个业务场景:login,order,logout  Reusable Action打勾才能被其他的调用

         1.创建对象库,新建3个Action:login,order,logout

         2.-> Record业务流程

         3.Save Test

         4.新建Action:Call to Copy of Action 选择Test

         5.划分Action:定位鼠标,-> Split Action

自动化测试(三):QTP参数化