关闭

使用QTP时的一些小心得

发表于:2008-3-10 14:29

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:陈能技    来源:陈能技的博客

#
QTP

1.如何在QTP运行过程中屏蔽鼠标 
        在运行QTP脚本的过程中,有些操作可能是对鼠标、键盘敏感的,需要在QTP运行过程中屏蔽鼠标或键盘输入。这是可用SystemUtil对象的BlockInPut方法,如下代码所示:

SystemUtil.BlockInput

Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set "mercury"

Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("password").SetSecure "4082986e39ea469e70dbf8c5a29429fe138c6efc"

Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click 2, 2

SystemUtil.UnblockInput

2.QTP中的DeviceReplay对象的使用
        DeviceReplay对象可用于在QTP中直接操纵鼠标和键盘,例如单击鼠标、发送键盘按键、输入字符串等动作。

        下面的脚本首先创建DeviceReplay对象,然后使用其SendString方法发送一段字符串,然后使用MouseMove方法移动鼠标,使用MouseClick方法单击鼠标:

Dim devicereplay

word = "Hello World!"

Set devicereplay = CreateObject("Mercury.DeviceReplay")

devicereplay.SendString(word)

devicereplay.MouseMove 200,200

devicereplay.MouseClick 200,200,LEFT_MOUSE_BUTTON

        而下面的脚本将按下“SHIFT”键:
Dim devicereplay
VK_SHIFT = 42
Set devicereplay = CreateObject("Mercury.DeviceReplay")
devicereplay.KeyDown(VK_SHIFT)

3.QTP访问DOM的方法
        QTP支持直接访问DOM(Document Object Model),可以通过DOM来访问HTML标签。在QTP中,访问DOM是通过使用Page测试对象的Object属性来进一步访问的,例如下面的脚本获取到Google主页中的:

On Error Resume Next
Set Doc = Browser("Google").Page("Google").Object
' 循环获取Page中的所有对象
For Each Element In Doc.all
       TagName = Element.TagName ' 获取标签名
       InnerText = Element.innerText ' 获取inner text.
       ' 把信息写入测试结果
       Reporter.ReportEvent 0, TagName, InnerText
Next

4.在QTP中巧用WebTable对象的ChildItem方法进行测试
        WebTable对象中的ChildItem方法用于获取单元格中的测试对象,例如下面的脚本首先获取WebTable对象,然后通过ChildItem获取指定单元格中的链接对象,并单击该链接:


' 获取WebTable对象
Set objTable = Browser("WebSite").Page("WebSite").WebTable("WebSite")
intRow = 2
intCol = 2
' 通过ChildItem获取单元格中的链接对象
Set objLink = objTable.ChildItem(intRow, intCol, "Link" , 0)
' 单击链接
objLink.click


 

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号