您的位置:首页 > 编程语言 > Qt开发

自动化测试工具QTP的一些简单代码

2010-07-22 14:29 483 查看
1,利用QTP向excel表格中写入数据:

Option explicit

Dim fso,ddfile
Dim excelbook,excelsheet
ddfile="F:/test/data.xls"

Set fso=createobject("scripting.filesystemobject")
If fso.fileexists(ddfile) Then
fso.deletefile(ddfile)
End If
wait 3
Set excelbook=createobject("excel.application")
Set excelsheet=createobject("excel.sheet")
excelsheet.application.visible=false

ExcelSheet.ActiveSheet.Cells(1,1).Value = "Agent Name"
ExcelSheet.ActiveSheet.Cells(1,2).Value = "Password"
ExcelSheet.ActiveSheet.Cells(1,3).Value = "Expire Value"
ExcelSheet.ActiveSheet.Cells(1,4).Value = "Fact Value"
ExcelSheet.ActiveSheet.Cells(1,5).Value = "Execute Result"

ExcelSheet.ActiveSheet.Cells(2,1).Value = "ad"
ExcelSheet.ActiveSheet.Cells(2,2).Value = "Mercury/"
ExcelSheet.ActiveSheet.Cells(2,3).Value = "Agent name must be at least 4 characters long."

ExcelSheet.ActiveSheet.Cells(3,1).Value = "Admin"
ExcelSheet.ActiveSheet.Cells(3,2).Value = "Merc"
ExcelSheet.ActiveSheet.Cells(3,3).Value = "Incorrect password. Please try again"

ExcelSheet.ActiveSheet.Cells(4,1).Value = "Admin"
ExcelSheet.ActiveSheet.Cells(4,2).Value = "Mercury"
ExcelSheet.ActiveSheet.Cells(4,3).Value = "Flight Reservation"

excelsheet.saveas ddfile
excelbook.quit
set excelbook=nothing

2,利用QTP向excel表格中读写数据:

Option explicit

Dim fso,filepath,i
Dim excelbook,excelsheet,myexcelbook,myexcelsheet
filepath="F:/test/data.xls"

Set fso=createobject("scripting.filesystemobject")

Set excelbook=createobject("excel.application")
Set excelsheet=createobject("excel.sheet")
Set myexcelbook=excelbook.workbooks.open(filepath)
Set myexcelsheet=myexcelbook.worksheets("sheet1")

For i=2 to 4
systemutil.CloseProcessByName "Flight4a.exe"
systemutil.Run "C:/Program Files/Mercury Interactive/QuickTest Professional/samples/flight/app/flight4a.exe"

dialog("Login").WinEdit("Agent Name:").Set myexcelsheet.cells(i,1)
dialog("Login").WinEdit("Password:").Set myexcelsheet.cells(i,2)
dialog("Login").WinButton("OK").Click
If dialog("Login").Dialog("Flight Reservations").Exist Then
myexcelsheet.cells(i,4).value=dialog("Login").Dialog("Flight Reservations").static("errInfo").GetROProperty("text")
If dialog("Login").Dialog("Flight Reservations").static("errInfo").GetROProperty("text")=myexcelsheet.cells(i,3) Then
myexcelsheet.cells(i,5).font.color=vbblue
myexcelsheet.cells(i,5).value="pass"
else
myexcelsheet.cells(i,5).font.color=vbred
myexcelsheet.cells(i,5).value="false"
End If
dialog("Login").Dialog("Flight Reservations").winbutton("确定").Click
dialog("Login").WinButton("Cancel").Click
elseif window("Flight Reservation").Exist then
myexcelsheet.cells(i,4).value=window("Flight Reservation").GetROProperty("text")
myexcelsheet.cells(i,5).font.color=vbblue
myexcelsheet.cells(i,5).value="pass"
window("Flight Reservation").Close
else
logfile.writeline "no window,false!"
exitaction
End If
Next
myexcelbook.save
excelbook.quit
Set excelbook=nothing

3,从txt文件中读出数据执行用例:

Dim fso
Dim f
systemutil.Run "F:/test/calc.exe"

Set fso=createobject("scripting.filesystemobject")
Set f=fso.opentextfile("F:/test/date.txt",1,false)

Do while f.atendofline <> true
strtemp=f.readline
x=split(strtemp,",")
For i=1to len(x(0))
btn=mid(x(0),i,1)
window("计算器").WinButton(btn).Click
Next
btn=x(2)
window("计算器").WinButton(btn).Click
For i=1 to len(x(1))
btn=mid(x(1),i,1)
window("计算器").WinButton(btn).Click
Next
btn="="
window("计算器").WinButton(btn).Click
a=window("计算器").WinEdit("Edit").GetROProperty("text")
msgbox a
Loop
window("计算器").Close
f.close
Set f=nothing
Set fso=nothing
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: