QTP实践总结

 QTP实践总结

查询数据库修改freq

1.Testcasetable创建查询select * from testcasetable order by fseq desc

2.设计表-选项-修改自动递增数值

 

新建Action 需要修改Script.mts(D:\NewSoftShop\NewSoftShop\Action0)文件,把文件中新增的Action注释,重打开脚本

 

一、获取某个表格中指定位置的字符并累加输出:

(1)、对象转换:

tobj(i).object.children(0).innertext;将QTP对象转换为COM对象

(2)、累加输出:

str1= str1& tobj(i).object.children(0).innertext & ";" & tobj(i).object.children(1).innertext & "||"

完整方法:

Set obj=Description.Create()

obj("html tag").Value="TR"

Set tobj=Browser("NearMe市场后台 - 新排行榜管理").Page("NearMe市场后台 - 新排行榜管理").Frame("main_2").WebElement("html id:=table1","html tag:=table","class:=tb tb2","index:=1").ChildObjects(obj)

Msgbox tobj.count

For i=1 to tobj.count-2

       str1= str1& tobj(i).object.children(0).innertext & ";" & tobj(i).object.children(1).innertext & "||"

Next

Msgbox str1

 

 

 

二、选择时间输入框当使用Set执行无效是需使用Object转换后再通过“Value=”输入时间:

Browser("NearMe市场后台 - 新排行榜管理").Page("NearMe市场后台 - 新排行榜管理_4").Frame("main_2").WebEdit("name:=starttime","html tag:=INPUT","type:=text").Object.Value="2015-01-08 15:36:55"

Browser("NearMe市场后台 - 新排行榜管理").Page("NearMe市场后台 - 新排行榜管理_4").Frame("main_2").WebEdit("name:=endtime","html tag:=INPUT","type:=text").Object.Value= "2015-01-09 15:36:55"

 

获取系统当前时间:now

 

比较时间:Cdate(Result)>=Cdate("2012-05-01 09:01:00") and Cdate(Result)<=Cdate("2014-06-05 22:59:59")

三、获取某个位置输入后得到的值,使用描述性编程描述对象后通过Object转换得到value值:

ID=Browser("NearMe市场后台 - 新排行榜管理").Page("NearMe市场后台 - 新排行榜管理").Frame("main").WebElement("html tag:=input","html id:=content").Object.value

 

Msgbox ID

四、获取一个html tag下的子对象:

 

如获取“TR”下tag为“strong”的值可使用方法:

tobj=Browser("…").Page("…").Frame("…").WebElement("…").ChildObjects(obj)

tobj(0).object.getElementsByTagName("STRONG")(0).innertext

 

五、Object、parentElement、GetElementsbyTagName,可同名子对象查找父对象:

说明:先使用描述性编程描述所要操作的子对象,通过Object将QTP对象转化为html 对象,再使用parentElement方法查找子对象查找上一层父对象,如果所查找位置未得到想要的结果继续查找上一层加一层(. parentElement),查找到对应结果后使用GetElementsbyTagName(“”)对应的html tag名称,如果有多个,添加(index),获取需要的值(.innertext)

Set Button=Browser("").Page("").Frame("").Link("html tag:=A","innertext:=添加","index:=0")

 

Msgbox Button.Object.parentElement.parentElement.GetElementsbyTagName("TD")(0).title

 

Button.Click

 

补充:如果出现获取不到时在Object后增加Document,

示例:

Button.Object.Document.parentElement.parentElement.GetElementsbyTagName("TD")(0).title

 

 

六、Trim(string…)使用方法,去除所获取字符串的前后空格

 

七、去除获取字符串中的字符,使用Replace(string,”需取代字符”,”替换字符”)(如:省略号)

SetButton=Browser("…").Page("…").Frame("main_6").WebElement("htmltag:=FONT","innertext:=…","index:=0")          str=Button.Object.parentElement.parentElement.parentElement.GetElementsByTagName("TD")(2).innertext

Tname=Replace(str,"…","")

 

       使用Left 或者Right获取前几个字符

     TdName=Left(text,2)

     TdName=Right(text,2)

 

删除段落前空格:

vbCrLf

vbTab

 

八、选择日期

方式一:

Browser("…").WebEdit(“html tag:=INPUT”,”html id:=startDate”).set “2015-12-02”

方式二:

Browser("…").WebEdit(“html tag:=INPUT”,”html id:=startDate”).Object.value= “2015-12-02”

 

Browser("Browser_2").Page("预算追加").WebEdit("html id:=txtPStartDate","html tag:=INPUT","html type:=text","html name:=WebEdit").Object.value="2014-10-09"

 

九、添加图片

Browser("…").Page("…").Frame("…").WebFile("pic_old").Click

Browser("…").Dialog("…").WinEdit("文件名(N):").Set "D:\NewSoftShop\resource\JPG\300x170.jpg"

Browser("…").Dialog("…").WinButton("打开(O)").Click

 

十、同行控件查找

转换为html方式的脚步无法对点击后或产生对话框的空间进行操作(如”删除”按钮),因为vbs脚本为单线程,弹出对话框后会停止脚本运行

 

十一、当前页面中的隐藏页面,使用描述性变成时需要先转换为Object,再逐层获得所需的值:

如:

 

Browser("").Page("").Frame("main").WebElement("html tag:=DIV","html id:=add").Object.style.display

 

 

 

十二、页面隐藏形式界面的检查方法

如:

Str= Browser("…").Page("..").Frame("main").WebElement("html tag:=DIV","html id:=addDiv").Object.style.display

       Msgbox Str

       If instr(Str,"none")<=0 Then

              insertDataBasenew "fseq", fseq,"pass"&time

              Else

              insertDataBasenew "fseq", fseq,"fail"&time

       End If

 

页面隐藏时display值为none;页面显示时display值为block

 

 

十三、翻页查找判断

 

循环方式实现:

'翻页查询使用循环判断

'找到对象所在页面

Function FindElement(Mname)

                     Browser("…").Page("..").Link("管理员").Click

                     Browser("…").Page("…").Link("管理员管理").Click

                     Mystate="false"

                     If  Browser("…").Page("…").webelement("html tag:=DIV","class:=pg").Exist(2)Then

                                   Strs=Browser("…").Page("…").webelement("html tag:=DIV","class:=pg").GetROProperty("innertext")

                                   str=Split(Strs," ")

                                   Num=str(1)

                                   Mnum=int(int(Num-1)/10)+1

                     else

                                   Mnum=1

                     End If

                     For i=1 to Mnum

                                   If Browser("…").Page("…").Link("text:=" &Mname,"html tag:=A" ).Exist (2) Then

                                                        Mystate="true"

                                                        Exit for

                                   else

                                                        If Mnum>i Then

                                                                      j=i+1

                                                                      Browser("…").Page("…").Link("html tag:=A","text:="&j).click

                                                        End If

                                   End If

                                   Next

                     FindElement=Mystate

End Function

'找到对象所在页面

Function FindElement(Mname)

                     'Browser("NearMe市场后台 - 搜索词运营管理").Page("NearMe市场后台 - 搜索词运营管理").Link("搜索词运营管理").Click

                     Mystate="false"

                     If  Browser("NearMe市场后台 - 活动中心").Page("NearMe市场后台 - 临时活动").WebElement("html tag:=DIV","class:=pg").Exist(2)Then

                                   Strs=Browser("NearMe市场后台 - 活动中心").Page("NearMe市场后台 - 临时活动").WebElement("html tag:=DIV","class:=pg").GetROProperty("innertext")

                                   str=Split(Strs," ")

                                   Num=str(1)

                                   Mnum=int(int(Num-1)/10)+1

                     else

                                   Mnum=1

                     End If

                     For i=1 to Mnum

                                   If Browser("NearMe市场后台 - 活动中心").Page("NearMe市场后台 - 临时活动").WebElement("innertext:="&Mname,"html tag:=TD" ).Exist (2) Then

                                                        Mystate="true"

                                                        Exit for

                                   else

                                                        If Mnum>i Then

                                                                      j=i+1

                                                                      Browser("NearMe市场后台 - 活动中心").Page("NearMe市场后台 - 临时活动").Link("html tag:=A","text:="&j).click

                                                        End If

                                   End If

                                   Next

                     FindElement=Mystate

End Function

 

 

 

递归方式实现:

'翻页查询使用递归循环

Function funloop()

   Set BrowserObject=Browser("…").Page("…").Frame("main").WebElement("html tag:=table","html id:=table1")

If BrowserObject.WebElement("html tag:=DIV","class:=pg").Exist(2) Then

       Str=BrowserObject.WebElement("html tag:=DIV","class:=pg").GetROProperty("innertext")

       Msgbox Str

       Onum=split(Str," ")

       Msgbox Onum(1)

       Num=int(Abs(int(Onum(1))-1)/10)+1

       Msgbox Num

       Else

       Num=1

End If

    Set trs=BrowserObject.Object.GetElementsByTagName("tr")

       Knum=BrowserObject.WebElement("html tag:=DIV","class:=pg").Object.GetElementsByTagName("strong")(0).innertext

       Msgbox trs.length

       For trindex=1 to trs.length-2

               Strs=trs(trindex).innertext

               Msgbox Strs

              If instr(Strs,"   QTP测试")>0 Then

                     res=true

                     Exit For

              End If

       Next

       if(res=True) then

              msgbox fseq & ":pass"

       elseif instr(Num,Knum)>0 then

              msgbox fseq & ":fail"

       else

              Browser("…").Page("…").Frame("data-rule-gt_2").WebEdit("name:=custompage","html tag:=INPUT").Object.value=Knum+1

              Set WshShell =CreateObject("WScript.shell")

               WshShell.Sendkeys "{ENTER}"

               Msgbox "翻页"

               funloop()

       End If

End Function

 

最新提炼方法(查找对象所在界面)

'找到对象所在页面

Function FindElement(Mname,TdName)

                     Mystate="false"

                     If Browser(BrowserName).Page(PageName).webelement("html tag:=DIV","class:=pg").Exist(2)Then

                                   Strs=Browser(BrowserName).Page(PageName).webelement("html tag:=DIV","class:=pg").GetROProperty("innertext")

                                   str=Split(Strs," ")

                                   Num=str(1)

                                   Mnum=int(int(Num-1)/10)+1

                     else

                                   Mnum=1

                     End If

                     For i=1 to Mnum

                                   If Browser(BrowserName).Page(PageName).WebElement("innertext:="&Mname,"html tag:="&TdName,"index:=0" ).Exist (2) Then

                                                        Mystate="true"

                                                        Exit for

                                   else

                                                        If Mnum>i Then

                                                                      j=i+1

                                                                      Browser(BrowserName).Page(PageName).Link("html tag:=A","text:="&j).click

                                                        End If

                                   End If

                                   Next

                     FindElement=Mystate

End Function

 

十四、随机选取下拉列表中元素

实现方式:

itemscount = WebList.GetROProperty ("items count") '获得下拉框选项个数

Rn=RandomNumber (0,itemscount-1) 'QTP特有的随机函数

WebList.Select Rn '用序号来选择

示例:

'随机获取下拉列表中元素

Set BrowserObject1=Browser("…").Page("…").Frame("main_5").WebList("select")

itemscount = BrowserObject1.GetROProperty ("items count") '获得下拉框选项个数

Rn=RandomNumber (0,itemscount-1) 'QTP特有的随机函数

BrowserObject1.Select Rn '用序号来选择

 

 

十五、随机获取下拉列表中元素后获取对应元素的属性值

  1. 直接定位需要获取元素位置

Set ObjSelect=Browser("NearMe市场后台 - 活动中心").Page("NearMe市场后台 - 活动中心").Frame("main_5").WebElement("html tag:=select","html id:=remain")

  1. 找到所要获取元素的属性值,唯一的属性

selection= int(replace(ObjSelect.GetROProperty("selected item index"),"#",""))

  1. 获取所需元素值

ObjSelect.Object.options(selection).text

 

十六、Function获取某个位置的值时需要设置返回值才能获取到对应文本

示例:获取表格中某行某列的值

'获取某行某列资源名称

Function GetTdName(IdTag,ID,IdIndex,TrIndex,TdIndex)

       Set BrowserObject=Browser("…").Page("…").WebElement("html tag:="&IdTag,"html id:="&ID,"index:="&IdIndex)

       Set Trs=BrowserObject.Object.GetElementsByTagName("TR")

       GetTdName=Trs(TrIndex).GetElementsByTagName("TD")(TdIndex).innertext

End Function

 

十七、获取有数字和文本字符串后如果需要对获取的数字进行运行要先使用Cint转换为整形

示例:

Num1=Cint(Replace(Replace( TdName1(1).GetRoProperty("innertext"),"取消","")," ",""))

转换后Num1为整形,此时可进行运算

 

十八、实现双击使用 FireEvent "ondblclick"

示例:

Set BrowserObject=Browser("NearMe市场后台 - 运营点组合").Page("NearMe市场后台 - 运营点组合").WebElement("html id:=conbination","html tag:=table")

Set Trs2=BrowserObject.Object.GetElementsByTagName("TR")

Trs2(2).GetElementsByTagName("TD")(0).FireEvent "ondblclick"

 

十九、html 标签A 带有href属性才能被QTP识别,否则需转换为html 对象,QTP才能

示例:

 

 

 

Set BrowserObject=Browser("NearMe市场后台 - 运营点组合").Page("NearMe市场后台 - 首页").WebElement("html tag:=table","html id:=table1")

       Set Trs=BrowserObject.Object.GetElementsByTagName("TR")

       For i=1 to Trs.length-1

              If instr(Trs(i).outerhtml,TdName)>0 Then

              Trs(i).GetElementsByTagName("A")(1).Click

              Exit For

              End If

       Next

 

二十、杀毒软件阻止QTP点击跳转等操作

处理办法:退出杀毒软件

 

二十一、QTP中出现复制粘贴脚本失败、直接脚本失败,可能原因是脚本出现乱码

处理办法:在对应Action下打开Script.mts文件,将@@及其后乱码删除,重新保存即可完成修改

示例:ZJB_Manage后台,Login模块出现此问题

 

二十二、QTP获取class属性值是使用classname

示例:

Browser("…").Page("…").WebElement("…").WebElement("…").Object.Classname

 

二十三、获取列表中某行某列资源

'获取某一行某一列的元素

Function GetElementByPosition(ID,TrIndex,TdIndex)

                     Dim Oname

                     Set BrowserObject=Browser("..").Page("..").WebTable("html id:="&ID)

                     Set trs= BrowserObject.Object.getElementsByTagName("tr")

                     Set tds= trs(TrIndex)

                     Oname= trim(trs(trindex).children(TdIndex).innertext)

                     GetElementByPosition=Oname

End Function

 

二十四、精确点击某行按钮

'点击编辑、删除等按钮

Function ClickButton(Tag,ID,TdName, ButtonTag, PropertyName,ButtonName)

       Set obj=Description.Create()

       obj("html tag").value="TR"

       Set tobj=Browser("…").Page("…").WebElement("html tag:="&Tag,"html id:="&ID).ChildObjects(obj)

       For i=0 to tobj.count-1

              TrName=tobj(i).GetRoProperty("innertext")

              If instr(TrName,TdName)>0 Then

                     Set aobj=Description.Create()

                     aobj("html tag").value=ButtonTag

                     aobj(PropertyName).value=ButtonName

                     Set taobj=tobj(i).ChildObjects(aobj)

                     taobj(0).Click

                     Exit For

              End If

       Next

End Function

 

'点击缺少href属性的编辑、下架等无弹出框按钮

Function ClickButton(Tag,ID,IDIndex,TdName,ButtonTag,ButtonIndex)

       Set BrowserObject=Browser(BrowserName).Page(PageName).WebElement("html tag:="&Tag,"html id:="&ID,"index:="&IDIndex)

              Set Trs=BrowserObject.Object.GetElementsByTagName("TR")

              For i=1 to Trs.length-1

                     If instr(Trs(i).outerhtml,TdName)>0 Then

                     Trs(i).GetElementsByTagName(ButtonTag)(ButtonIndex).Click

                     Exit For

                     End If

              Next

End Function

 

 

'点击缺少href属性的删除等有弹出框按钮

Function ClickDel(Tag,ID,IDIndex,TdName,ButtonName,ButtonTag)

       Set BrowserObject=Browser(BrowserName).Page(PageName).WebElement("html tag:="&Tag,"html id:="&ID,"index:="&IDIndex)

       Set Trs=BrowserObject.Object.GetElementsByTagName("TR")

       For i=1 to Trs.length-1

       If instr(Trs(i).outerhtml,TdName)>0 Then

              IndexNum=i

              Browser(BrowserName).Page(PageName).WebElement("innertext:="&ButtonName,"html tag:="&ButtonTag,"index:="&IndexNum-1).Click

              Exit For

       End If

       Next

End Function

 

 

二十五、通过一个元素获取同行的某列的元素

示例:

'通过一个元素获取同行另一元素

Function GetElementByAnotherElement(Tag,Ename,TdIndex,Index)

                     Dim Oname

                     Set BrowserObject=Browser("…").Page("…").WebElement("html tag:="&Tag,"innertext:="&Ename,"index:="&Index)

                     Set tr= BrowserObject.Object.parentElement

                     Oname= trim(tr.children(TdIndex).innertext)

                     GetElementByAnotherElement=Oname

End Function

 

二十六、点击列表中上移下移操作

 

示例:

'上移下移

Function ClickSpecialButton(Kname,Tname)

       Set objs=Description.Create()

       objs("html tag").value="TR"

       Set tobjs=Browser("…").Page("…").Frame("main").WebElement("html tag:=table","html id:=table2").Object.GetElementsByTagName("TR")

       For x=0 to tobjs.length-1

              If instr(tobjs(x).innertext,Kname)>0 Then

                     Set obj=Description.Create()

                     obj("html tag").value="A"

                     Set tobj=tobjs(x).GetElementsByTagName("A")

                     For i=0 to tobj.length-1

                            If (instr(tobj(i).title,Tname)>0) Then

                                   tobj(i).Click

                                   Exit For

                            End If

                     Next

                     Exit For

              End If

       Next

End Function

 

二十七、如何获取下拉列表中的所有值

 

Dim stcount2,stnum2,rel2,stobj2
set stobj2=Browser(“XXX系统”).Page(“XXX平台”).Frame(“mainFrame_2″).WebList(“repositorySytleID”).Object
stcount2=stobj2.options.length

For x=0 to stcount2-1
stnum2=GetList(stcount2-1)
rel2=stobj2.options.item(stnum2).text

Next

Function GetList(i)

Randomize

GetList=RandomNumber(x,i)

End Function

 

二十八、对比时间大小

If Cdate(Result)>=Cdate("2012-05-01 09:01:00") and Cdate(Result)<=Cdate("2014-06-05 22:59:59")   Then

                                     insertDataBasenew "fseq", fseq,"pass" & time

                     else

                                    insertDataBasenew "fseq", fseq,"fail" & time

                     End If

 

二十九、获取表格TR和TD数量

 

Set BrowserPage1=Browser("游戏中心后台管理").Page("游戏中心后台管理")

                           

Set BrowserPageObject1=BrowserPage1.WebElement("html tag:=table","html id:=gc_026_002")

                           

Set Tr1=BrowserPageObject1.Object.GetElementsByTagName("TR")

                           

Tr1Index=Cint(Tr1.length)

 

Set Td1= Tr1(Tr1Index).GetElementsByTagName(“TD”)

 

Td1Index=Cint(Td1.length)

 

 

三十、网页刷新办法Refresh

Browser("NearMe市场后台 - 活动中心").Refresh

 

设置:IE浏览器——工具——管理加载项——BHOManager 开启

 

三十一、Function之间传递参数

Dim st

st=””

Function sti

st=1

   st=st+1

End Function

 

Function sti2

   msgbox st

End Function

 

sti2

 

sti

 

三十二、使用Do While

                                          Do While instr(now,Td_TimeN)>0

 

                                                 ClickButton "table","tblMsgs",Td_TimeN,"A","innertext","删除"

 

                                                 If Browser("后台管理").Dialog("来自网页的消息").Exist(2) Then

      

                                                        Browser("后台管理").Dialog("来自网页的消息").WinButton("确定").Click

 

                                                        Else

 

                                                        Exit Do

      

                                                 End If

 

                                          Loop

 

三十三、检查链接

QTP中,对于网页链接测试可以使用Link对象,例如下面的脚本用于对链接的地址进行检查

  ‘ 对链接的地址进行检查

  Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours"

  ).Link("REGISTER").CheckProperty ("url"

  ,http://newtours.demoaut.com/mercuryregister.php)

  其中使用了CheckProperty方法,用于对比Link对象的URL属性是否等于指定的地址。也可以使用Link对象的

  Object属性中的href属性来进行比较,例如下面的代码所示:

  href = Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours

  ").Link("REGISTER").Object.href

  ‘ 如果地址不正确,则报告错误

  If NOT href ="http://newtours.demoaut.com/mercuryregister.asp" Then

  Reporter.ReportEvent micFail,"Link Error",href

  ExitAction

End If

三十四、初始化执行用例

在For循环中添加

err.clear

 

三十五、网页执行时某个Action用例不执行

Action名称不能包含与VB启动脚本有相同的关键字(RUN)

 

三十六、获取下拉列表中某个指定元素(数组中的第几项)

'获取LIST表中的一个选项

Function getValueFromList(Lid,Index)

                     Set BrowserObject = Browser(BrowserName).Page(PageName).WebList("html id:="&Lid).Object

                     scount = BrowserObject.options.length

                     getValueFromList = BrowserObject.options.item(Index).text

End Function

 

三十七、最大化浏览器-----BrowserOperates longhwnd,1

转载于:https://www.cnblogs.com/furenjian/p/6096870.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值