lllqtp测试代码

来源:互联网 发布:数据分析师学什么专业 编辑:程序博客网 时间:2024/06/10 08:30
1.利用QTP脚本编写一个获取随机的固定长度字符串函数,函数名为getStr并进行正确调用。
Function getStr (inputlength)
If IsNumeric(inputlength) Then 
For i = 1 To inputlength
'把希望加入的字符放到数组中
A=Array("a","b","c","d","1","2","&","~","!","@","#","$","%","^","&","*")
Randomize 
x=RandomNumber (0,15)
B = A(x)
getStr = getStr +B
Next
'置返回 值
getStr = getStr
Else
msgbox ("错误的输入数据:"&inputlength&",请输入正确的数值!")
End If 
End Function
MsgBox getStr ("5")






Ps: Setting.WebPackage("ReplayType") = 2 换鼠标
2.打开“百度”首面,获取页面上所有对象,并进行遍历(利用描述性编程)
SystemUtil.Run("www.baidu.com")
'创建一个描述对象
Set dpAllTxt= Description.Create
'获取页面上的所有对象
Set allChilds =Browser("百度一下,你就知道").Page("百度一下,你就知道").ChildObjects(dpAllTxt)
iCount =allChilds.Count-1
'遍历所有文本框对象
For i = 0 to iCount
MsgBox allChilds.item(i).GetTOProperty("micclass")
Next






3.进入百度首页,完成下列功能
在搜索框中输入“QTP自动化测试技术领航”字样;
使用GetRoProperty去获取搜索框中刚才输入的字样,并传入Global Sheet的指定列中;
读取Global Sheet中的Run-time Data;获取搜索结果页面中的搜索框的值,假设为CheckValue;
将Run-time Data与CheckValue做比较,并利用适当的格式报告显示测试结果。
'打开百度首页
SystemUtil.Run "www.baidu.com"
Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("wd").Set "QTP自动化测试技术领航"
Run_Time=Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("wd").GetROProperty ("value") 
'将Run_time传入Global Sheet指定列
DataTable.Value ("Runtime_Data","Global") = Run_Time
Browser("百度一下,你就知道").Page("百度一下,你就知道").WebButton("百度一下").Click
Run_Time = DataTable.Value ("Runtime_Data","Global")
CheckValue = Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("wd").GetROProperty ("value")
If CheckValue = Run_Time Then
msgBox "Passed"
else
msgBox "Failed"
End If






4.打开http://www.google.cn/页面,获取该页面上所有的链接,打印链接数,并对所有的链接进行遍历,判断它是否有“翻译”这个链接。


'创建一个描述对象
Set GoogleText= Description.Create
'只设置为WebEdit类型
GoogleText ("micclass").value = "Link"
'获取所有匹配描述属性的对象
systemUtil.Run "www.google.cn"
set Links= Browser("Google").Page("Google").ChildObjects (GoogleText)
'遍历所有文本框对象
icount = Links.Count 
msgbox icount
mark = 0 '标志位
theFoundLink = "翻译"
For i = 0 to icount-1
LinkName = Browser("Google").Page("Google").Link("micClass:=Link","index:="&i).GetRoproperty("innertext")
If LinkName = theFoundLink Then
mark = mark + 1
End If
If mark =1 Then
Reporter.ReportEvent micPass,"step","passed"
Exit for
End If
If i =  icount-1 Then
Reporter.ReportEvent micPass,"step","passed"
End If
Next
















5.打开“百度”进行二十次百度搜索的业务流程,但是每次输入的关键字必须不一样,试用“数据池”的相关技术进行实现。
'打开百度首页
SystemUtil.Run "www.baidu.com"
'将DataTable里的值传递给一个变量
testData = DataTable.Value ("关键字输入","Global")
'使用该变量,并将填入关键字输入框
Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("wd").Set testData
Browser("百度一下,你就知道").Page("百度一下,你就知道").WebButton("百度一下").Click
'关闭网页
Browser("百度一下,你就知道").Close






'##########################报告生成################################
Private Function HReprot(ExpectedValue,ActualValue,testStatus,nodename)
   Set oEventDesc = CreateObject("Scripting.Dictionary")
   '添加状态
   oEventDesc("Status") = micFail
'添加是否过滤
oEventDesc("EnableFilter") = False
'添加节点名称
oEventDesc("NodeName") = nodename
'添加HTML结果\
oEventDesc("StepHtmlInfo")= "<TABLE border='1'>" & _
"<TR><TD>Actual Value</TD><TD>"+ExpectedValue+"</TD></TR>" & _
"<TR><TD>Expected Value</TD><TD>"+ActualValue+"</TD></TR>" & _
"<TR><TD>Checkpoint Status</TD><TD style='background-color:red'><b>Failed</b></TD></TR>" & _
"</TABLE>"
'判断状态
if testStatus Then
oEventDesc("Status") = micPass
oEventDesc("StepHtmlInfo") = Replace(oEventDesc("StepHtmlInfo"),_
"<TD style ='background-color:red'><b>Failed</b></TD>","<TD style ='background-color:green'><b>Passed</b></TD>")
End if 


'生成报告
newEventContext = Reporter.LogEvent("Replay",oEventDesc,Reporter.GetContext)
'释放资源
Set oEventDesc = nothing
End Function


'######################################################################
Function ValidateProperty(Object,PropertyName,ExpectedValue)
'判断预期是否与实际值相等
if Object.GetROProperty(PropertyName) = ExpectedValue Then
'成功
HReprot  ExpectedValue,Object.GetROProperty(PropertyName),true,_
"check "+Object.GetTOProperty("testObjName")+"<"+PropertyName+">属性"
ValidateProperty = True
Exit Function
Else
'失败
HReprot  ExpectedValue,Object.GetROProperty(PropertyName),false,_
"check "+Object.GetTOProperty("testObjName")+"<"+PropertyName+">属性"
ValidateProperty = False
Exit Function
End if 
End Function
































6.打开http://www.google.cn/页面,采用描述性编程在文本框中输入“软件测试”,然后点击Google搜索,并对所有的链接进行遍历,判断它是否有“软件测试工程师”这个链接。


SystemUtil.Run "www.google.com"


'创建一个描述对象
set objBrowser=Description.Create
objBrowser("micClass").value="Browser"


set objPage=Description.Create
objPage("micClass").value="Page"


Dim btn
set btn=Description.Create
btn("type").value="submit"
btn("type").RegularExpression=False
btn("name").value="google 搜索"
btn("html tag").value="INPUT“


set objwebedit=Description.Create
'objwebedit("type").value="text"
objwebedit("name").value="q"
objwebedit("html tag").value="INPUT"


Set GoogleText= Description.Create
'只设置为WebEdit类型
GoogleText ("micclass").value = "Link"
'获取所有匹配描述属性的对象
systemUtil.Run www.google.cn
Browser(objBrowser).page(objPage).WebEdit(objwebedit).Set  “软件测试"
Browser(objBrowser).page(objPage).WebButton(btn).Click


set Links= Browser("Google").Page("Google").ChildObjects (GoogleText)
'遍历所有文本框对象
icount = Links.Count 
msgbox icount
mark = 0 '标志位
theFoundLink = "软件测试工程师"
For i = 0 to icount-1
LinkName = Browser("Google").Page("Google").Link("micClass:=Link","index:="&i)._
GetRoproperty("innertext")
If LinkName = theFoundLink Then
mark = mark + 1
End If
If mark =1 Then
Reporter.ReportEvent micPass,"step","passed"
Exit for
End If
If i =  icount-1 Then
Reporter.ReportEvent micPass,"step","passed"
End If
Next














四、综合题(30分)
登录http://www.baidu.com/网站,进行用户注册,用户名为名的第一个字母加姓,再加学号(例如:张三丰,学号1号,用户名为:sfZhang_01,密码为:0757gdfy),进行功能测试。功能测试满足下面要求:
1)打开百度首页,点击登录链接,进行登录,并检查是否登录成功;
2)点击个中心,点击搜索链接,在其页面的设置下拉菜单中,选中“高级搜索”,在关键词框内输入“软件测试”,搜索结果显示15条,搜索网页语言是“简体中文”,搜索关键词位于“网页的任何地方”,其它选项采用默认选项,并查找“软件测试工程师”的有关信息,并显示记录数;
3)点击“私信”链接,进行写信页面,设置收信人为pubtester.sina.com;内容是“软件测试目前是最有前途的工作之一,请珍惜!”,手动输入验证码;发送邮件并验证其是否发送成功!
4)回到百度首页,并关闭所有打开的网页。


SystemUtil.Run"www.baidu.com"
 Browser("百度一下,你就知道").Page("百度一下,你就知道").Link("登录").Click
 Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("userName").Set
 Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("password").Set


wait 10


Browser("百度一下,你就知道").Page("百度一下,你就知道").WebButton("登录").Click


 aa =Browser("百度一下,你就知道").Page("百度一下,你就知道").WebElement("hsFang_60").exist(2)
 If  aa Then
Reporter.ReportEvent micPass,"step1","通过"
else
Reporter.ReportEvent micFail,"step1","失败"
 End If
wait 4
Browser("百度一下,你就知道").Page("百度一下,你就知道").Link("个人中心").Click










随机从excle文件中导入数据并对百度搜索页进行测试
首先从数据池的局部表中导入数据
最后代码
SystemUtil.Run "www.baidu.com"




rowcount = DataTable.GetSheet("Action1").GetRowCount 


'msgBox rowcount 
For i=1 to rowcount

'产生一个1至100的随机数

randNumber = Int(100  * Rnd +1)

‘DataTable.SetCurrentRow(randNumber)(代码同下)

DataTable.GetSheet("Action1").SetCurrentRow(randNumber)
Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("wd").Set DataTable.Value ("p_Text","Action1")


Next 




Browser("百度一下,你就知道").Page("百度一下,你就知道").WebButton("百度一下").Click

0 0