51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3222|回复: 3
打印 上一主题 下一主题

[原创] GetTextLocation —— 依据文本值动态获取坐标进行点击、拖拽操作

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2009-8-20 11:28:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在实际工作中我们经常会遇到对一些不能被捕获的对象进行点击、拖拽操作,直接去录制可能得到的就是Object.Click xxx,yyy ,这样的代码健壮性和可维护性都非常的差。
如果需要被操作的位置上有相对唯一的文本值,那么就可以考虑使用GetTextLocation方法来辅助我们来动态定位到预期的坐标,这样就不会因为界面位置一点点的变动导致脚本运行失败了。

先在帮助文档中查找GetTextLocation方法看看,我们会惊奇的发现几乎所有的对象都能支持该方法,这就使得该方法的应用范围会比较广,帮助文档中的描述如下:
Syntaxobject.GetTextLocation (TextToFind, Left, Top, Right, Bottom, [MatchWholeWordOnly])


Syntax Details
ArgumentDescription
TextToFindRequired. A String value. The text string you want to locate.
LeftRequired. A Variant value. The left coordinate of the search area within the window or screen.
TopRequired. A Variant value. The top coordinate of the search area within the window or screen.
RightRequired. A Variant value. The right coordinate of the search area within the window or screen.
BottomRequired. A Variant value. The bottom coordinate of the search area within the window or screen.
Note: Set the Left, Top, Right, and Bottom coordinates to -1 to search for the text string within the object’s entire window.
MatchWholeWordOnlyOptional. A Boolean value. If True, the method searches for occurrences that are whole words only and not part of a larger word. If False, the method does not restrict the results to occurrences that are whole words only. Default value = True


下面我们来看一个运用GetTextLocation的实例

先简单的描述下需求,现在需要点击自定义的菜单栏上的"Generate Report"按钮,但按钮控件不能被捕获,只能识别到整个菜单栏对象为VbWindow("Window").WinObject("Menu"),点击按钮的代码如下:

  1. '获取"Generate Report"文本在WinObject("Menu")中的坐标范围,并返回给L(left),T(top),R(right),B(bottom)
  2. VbWindow("Window").WinObject("Menu").GetTextLocation strText,L,T,R,B,True
  3. '点击该文本所在坐标区域的正中心位置
  4. VbWindow("Window").WinObject("Menu").Click (L+R)/2, (T+B)/2
复制代码


为了便于使用,我们也可以进行简单的封装,例如:

  1. Function  ClickText(objOB, strText)
  2.    If objOB.Exist(1) Then
  3.      objOB.GetTextLocation strText,L,T,R,B,True
  4.      objOB.Click (L+R)/2, (T+B)/2
  5.    Else
  6.      Reporter.ReportEvent micFail , "Can not find the specified object!", ""
  7.    End If
  8. End Function

  9. ClickText VbWindow("Window").WinObject("Menu"),"Generate Report"
复制代码


进行拖拽操作也是一样的,通过GetTextLocation方法来获取文本中心位置坐标,动态的做为Drag和Drop的参数,这样就能比较精确的完成拖拽操作。
下面也举个简单的例子吧

  1. Sub MoveItemByText(objFrom,strFrom,objTo,strTo)
  2. Dim l,r,t,b,l1,r1,t1,b1
  3. 'Drag Item by specified text
  4. objFrom.GetTextLocation strFrom, l, t, r, b
  5. objFrom.Drag (l+r)/2, (t+b)/2, 1

  6. ' Drop Item on specified text
  7. objTo.GetTextLocation strTo, l1, t1, r1, b1
  8. objTo.Drop (l1+r1)/2, (t1+b1)/2, 1
  9. End Sub
  10. '将hsjzfling从Names列表中拖拽到Result列表的Champion子列表中
  11. Set objAct = VbWindow("frmOvertime").ActiveX("ActiveX").ActiveX("ActiveX").ActiveX("ActiveX")
  12. MoveItemByText objAct.VbTreeView("Names"), "hsjzfling", objAct.VbTreeView("Result"), "Champion"
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2009-9-1 14:49:02 | 只看该作者
用第三方控件开发的Menu都有这个问题,目前没有很好的解决办法。
我们的解决办法之一:
用快捷键代替Menu的点击,例如:推出程序:ALT+F4
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2009-9-2 18:57:17 | 只看该作者
这个方法也只是封装了的Getroproperty 类似功能而已

并且,对于那些没有显示出来的对象就无效了.
比如有个按钮,需要先拖动窗口才会出现的,这样的按钮,取得的坐标是无法通过click方法点击的.
回复 支持 反对

使用道具 举报

该用户从未签到

4#
 楼主| 发表于 2009-9-8 18:18:02 | 只看该作者

回复 3# 的帖子

这样的问题我也遇到过,其中之一的解决方法是:
先判断当前界面是否能找到文本,找不到就控制滚动条翻一页,依次循环直到找到文本或者滚动条已经到最底部。
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-9-21 22:19 , Processed in 0.080187 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表