QTP - 右键菜单操作总结
上一篇 /
下一篇 2009-09-28 16:02:04
/ 个人分类:QTP
今天继续研究QTP精华板块,又看到了右键操作的帖子,于是结合前几天看到的那个,做个整理。前几天研究的右键问题是针对WebEdit的,不知道原来image对象的还不可以用我之前的方法。而且很开心,找到了image对象实例,比如开心网上的图片就是image对象的。
1.普通的右键方法:
http://bbs.51testing.com/thread-97834-1-34.html
cur_replay_type = Setting.WebPackage("ReplayType")
Setting.WebPackage("ReplayType") = 2
Browser("Browser").Page("Welcome: Mercury Tours").WebEdit("userName").Click 5,4, micRightBtn
Setting.WebPackage("ReplayType") = cur_replay_type
idx=4
Set WshShell = CreateObject("WScript.Shell")
For i = 1 To idx
WshShell.sendKeys "{DOWN}"
Next
WshShell.sendKeys "{ENTER}"
Set WshShell = nothing
对于这个帖子里的问题,我现在还剩下replay_type的问题,已发问,等待回答。
2.转精华贴里的image对象的右键处理:
http://bbs.51testing.com/thread-82266-1-4.html
练习代码路径:Test->RightClick
Web里很多对象都有Click方法(object.Click [x], [y], [BUTTON]),带上micRightBtn参数,都能实现右键点击调出右键菜单的操作。
可是不知为何,Image对象就是不能右键点击,不管是否带上micRightBtn参数,Click执行的都是左键点击操作,所以无法调出右键菜单。假如按照上面我写的方法1,会在click的那步提示出错:The statement contains one or more invalid function arguments.
这篇帖子里提供了一个函数,我觉得不错。(而且以后我也要养成习惯,比如把要右键的object和之后的热键操作参数化,这样更规范,也更便于管理)
SelectImageMenu browser("test").Page("test").Image("image001"), "R"
我用的调用例子是:
SelectImageMenu browser("我的首页 - 开心网").Page("我的首页 - 开心网").Image("120_2620320_5"), "{DOWN}{DOWN}{DOWN}"
'================================================================
' FUNCTION NAME:
' SelectImageMenu
' FUNCTION DESCRIPTION:
' Use this function to right-click on a image object and select the pop-up menu
' FUNCTION USAGE:
' ret = SelectImageMenu(browser("test").Page("test").Image("image001"), "R")
' FUNCTION PARAMETER
' ImageObject : The web image object
' MenuKey : The hot key of pop-up menu item you will select
' FUNCTION RETURN:
' Return 0 when successfully, or return -1 when fail
'================================================================
Function SelectImageMenu(ImageObject, MenuKey)
Dim wshShell
Dim ieServerObject
Dim x1, y1
'Get the ie server object
Set ieServerObject = Window("regexpwndclass:=IEFrame").WinObject("regexpwndclass:=Internet Explorer_Server")(这一步操作会出错,说不认window对象,于是我用了这样一个办法:录制时选用低级录制,然后点一下page上任何一个地方,那么它就被加入了对象库,然后用下面这一句即可识别。低级录制的时候,我用object spy发现整个page就这一个对象。然后在这个ie page的句柄再click对应image对象的坐标来实现对image的右键操作)
Set ieServerObject = Window("我的首页 - 开心网 - Microsoft").WinObject("Internet Explorer_Server")
'get the image position
x1 = CInt(ImageObject.GetROProperty("abs_x"))
y1 = CInt(ImageObject.GetROProperty("abs_y"))
'right-click on image through ie object
ieServerObject.Click x1, y1, micRightBtn
wait 1
'Select the menu item by press hot key
Set wshShell = CreateObject("WScript.Shell")
wshShell.SendKeys MenuKey
wait 1
SelectImageMenu = 0
End Function
说明:
因为那右键弹出菜单QTP无法识别,所以QTP无法操作这菜单,只能用键盘按键来代替鼠标选择。
键盘按键可以是热键,如“复制(C)”这一菜单项,可以用按键C来选择,MenuKey就设为"C"。
键盘按键也可以是下箭头和回车键,如要选择第三个菜单项,可以按三下下箭头和回车键来选择,MenuKey就设为"{DOWN}{DOWN}{DOWN}{ENTER}"。这样没有热键的菜单也可以选择了,前提是菜单项目是固定的,不是可变的。
收藏
举报
TAG: