参数化是使用参数,或变量替换硬编码的过程。在QTP中,脚本参数化可以通过环境变量,DataTable、Excel等方式实现参数化。
DataTable包括全局DataTable和本地DataTable。全局DataTalbe适用于所有的action,而本地DataTable只适用于该action。
访问DataTable中的数据的语句格式如下:
DataTable("ColumnName",
dtLocalSheet)---访问本地DataTable中的数据
DataTable("ColumnName",
dtGlobalSheet)--访问全局DataTable中的数据。
下面以Hp自带的订票Web应用为例,测试步骤如下:
1.启动应用。
2.输入DataTable中的用户名/密码,并点击Login
3.检查是否显示Find
Flights页面
4.如果显示Find
Flights页面,迭代通过
.5.返回到首页,并开始步骤2代码可以如下:
代码如下:
If
DataTable.LocalSheet.GetCurrentRow = 1 Then
SystemUtil.Run "iexplore.exe",
"http://newtours.demoaut.com"
End If
With
Browser("title:=Welcome: Mercury Tours", "index:=0")
'参数化: UserName
.WebEdit("name:=userName").Set
Datatable("UserName", dtLocalSheet)
'参数化密码: Password
.WebEdit("name:=password").Set
Datatable("Password", dtLocalSheet)
.Image("name:=login").Click
End With
'如果显示Flight page,返回到首页
If
Browser("title:=Find a Flight: Mercury Tours:",
"index:=0").Exist(10) Then
Reporter.ReportEvent micPass,
"Iteration " & DataTable.LocalSheet.GetCurrentRow, _
"UserName: " & Datatable("UserName", dtLocalSheet)
& " is valid"
Browser("title:=Find a Flight: Mercury
Tours:", "index:=0").Link("text:=Home").Click
Else
'如果验证失败,返回到首页
Reporter.ReportEvent micFail,
"Iteration " & DataTable.LocalSheet.GetCurrentRow, _
"UserName: " & Datatable("UserName", dtLocalSheet)
& " is invalid"
Browser("title:=Sign-on: Mercury
Tours", "index:=0").Link("text:=Home").Click
End If