严小婷
2013.10.27
QTP第三天
环境变量设置、使用、导入
语句导入:
Environment.LoadFromFile("C:\Users\usera\Desktop\qtp3\Test1\环境变量.xml")
workfolder1 = Environment.Value("workfolder1")
导入DataTable
DataTable.ImportSheet
Action传参的使用
MainAction(Action1)进行insert call to new Action,name为login,选择After the current step
MainAction(Action1)的Action properties的Parameter添加INPUT1和OUTPUT1
login(子Action)的Action properties的Parameter添加INPUT2 和OUTPUT2
login(子Action)的call properties—Parameter---(参数化为MainAction的变量)
MainAction的内容如下:
Parameter("INPUT1")="test1"
RunAction "login", oneIteration, Parameter("INPUT1"), Parameter("OUTPUT1")
print Parameter("OUTPUT1")
子Action内容如下:
'If Parameter("INPUT2 ")="hi" Then
'
'
'
'End If
往报告中添加内容:成功OR失败
reporter.ReportEvent micPass,"Login pass","Login successful"
reporter.ReportEvent micFail,"Login fail","Login failed"
描述性编程
WebElement("innertext:="&uname,"y:=42")
‘直接描述性编程
‘计算器的的代码:
Dim calcstr,lens,i,Expect,actual
calcstr=datatable("calcstr")
Expect=datatable("Expect")
lens=len(calcstr)
SystemUtil.CloseProcessByName "calc.exe"
SystemUtil.Run "calc"
Window("text:=计算器").Activate
Window("text:=计算器").WinButton("text:=C").Click
For i = 1 To lens
Next
actual=Window("text:=计算器").WinEdit("nativeclass:=Edit").GetROProperty("text")
'print actual
'print Expect
If cdbl(actual)= cdbl(Expect) Then
Reporter.ReportEvent micPass, "OK", "OK"
Else
Reporter.ReportEvent micFail, "NO", "NO"
End If
‘使用对象描述性编程
‘计算器的的代码:
SystemUtil.CloseProcessByName "calc.exe"
SystemUtil.Run "calc"
Window("text:=计算器").Activate
Window("text:=计算器").WinButton("text:=C").Click
Dim mywindow,mybutton,objCol,i
Set mywindow=description.Create()'创建虚拟对象
Set mybutton=description.Create()
mywindow("Class Name").value="Window"'进行对象属性描述
mywindow("text").value="计算器"
mybutton("Class Name").value="WinButton"
'mybutton("text").value="1"
mybutton("text").value="\d+"'正则表达式
mybutton("x").value="62"
'Window(mywindow).WinButton(mybutton).Click'使用
Set objCol=Window(mywindow).ChildObjects(mybutton)
For i=0 to objCol.count-1
Next
Set mybutton=nothing
‘使用对象描述性编程
‘随机回帖
Dim mylink,mylinks,MyValue
set mylink=description.Create()
mylink("x").value=62
mylink("href").value="http://172.168.1.202:8000/upload/read\.php\?tid=\d+"
Set mylinks=Browser(":=").Page(":=").WebTable("html tag:=TABLE","Index:=2").ChildObjects(mylink)
Randomize
MyValue = Int((mylinks.count * Rnd) )
'print MyValue
'print mylinks.count
'print (cstr(mylinks(MyValue).GetRoProperty("href")))
Browser(":=").Navigate (cstr(mylinks(MyValue).GetRoProperty("href")))
错误处理方法1:
On Error Resume Next'进行忽略错误
Dim result
result=1/0'错误
result=1/2
print Err.number'错误号
print Err.Description'错误内容
Err.Clear'错误清楚
print Err.Description
On Error GoTo 0'取消忽略错误
错误处理方法2
File—settings—run
错误处理方法3:场景恢复
触发事件
恢复操作
恢复后运行设置
恢复
具体步骤如下自行探索(图片上传比较难)
事先判断button能不能用,能用就点击,不能就不点击
RegisterUserFunc "WinButton", "Click", "MyClick" ,True
Window("Flight Reservation").WinButton("Button_2").Click
Window("Flight Reservation").WinButton("FLIGHT").Click
Function MyClick (objButton)
End Function