QTP的TextUtil对象的使用

QTP中有专门的一个对象TextUtil,可用于识别指定窗口中的文本。有时候使用其中的GetTextLocation方法可以解决某些控件识别和定位的问题。

The object used to recognize text within a specified window handle.

但是如果有其它方法的话,建议不要使用这种识别方式。例如,使用GetROPropertyGetVisibleText等。

When possible, it is recommended to use alternative methods of capturing text instead of using the TextUtil object. For example:

  • Use the GetROProperty method or the Object property to retrieve the value of the text (or equivalent) property from an object in your application
  • Use a text or text area output value step to retrieve text or a text or text area checkpoint step to verify a text value.
  • Use the GetVisibleText or GetTextLocation methods of the appropriate test object.

 

GetText方法:

Returns the text from the specified window handle area. The area is defined by pairs of coordinates that designate two diagonally opposite corners of a rectangle.

 

例如:

Extern.Declare micLong,"FindWindow","User32","FindWindowA",micString,micString

hNotepad = Extern.FindWindow("Notepad", "无标题 - 记事本")

NotepadText = TextUtil.GetText(hNotepad)

MsgBox NotepadText

 

GetText支持查找指定句柄的窗口中的文本,也支持通过指定某个区域,例如:

MsgBox TextUtil.GetText(0, 20, 20, 200, 200)

第一个参数0表示不指定窗口,而是整个屏幕区域。

 

 

GetTextLocation方法:

Checks whether a specified text string is contained in a specified window area. If the text string is located, the location coordinates are also returned.

TextUtil.GetTextLocation(TextToFind, hWnd, Left, Top, Right, Bottom[, MatchWholeWords])

Important Information

  • The text to capture must be visible in the application window when the step runs.
  • This method returns True only if the the TextToFind argument value is found within a single line in the specified area. The text search restarts on each line of text.
  • If the TextToFind argument value includes a space, then this method searches for that text as whole words, regardless of the value set in the MatchWholeWords argument. For example, if you search for "a b" and the text "bla bla" exists, the method will still return False. However, if the MatchWholeWords argument is set to False, then a search for "la" in an area where "bla bla" exists, would return True.
  • If the text is found (return value = True) and if the Left, Top, Right, and Bottom arguments are supplied as variables, then the method also returns the exact coordinates of the specified text to the supplied arguments (the returned coordinates overwrite the supplied ones).
  • The results of this method may be different depending on the settings selected in the Text Recognition pane of the Options dialog box (Tools menu > Options item > General node > Text Recognition node).
  • The results of this method may be different in different run sessions depending on the operating system version you are using, service packs you have installed, other installed toolkits, the APIs used in your application, and so on. Therefore, when possible, it is highly recommended to use alternative ways retrieve the value of the text (or equivalent) property from an object in your application instead of using the GetText method, as described in the Important Information section of the TextUtil Object.

Syntax

Argument

Type

Description

TextToFind

String

The text string you want to locate.

hWnd

Number

The handle to a run-time object's window.

Notes:

  • If hWnd is not 0, then the coordinate arguments apply to the specified window. If hWnd is 0, the coordinates apply to the screen.
  • The value 0 is not supported when running steps on Vista or on any 64-bit operating system.

Left, Top, Right, Bottom

InOut, Number

These arguments define the search area within the window or screen. Set all coordinates to -1 to search for the text string within the entire window or screen. The method returns the coordinates of the rectangle containing the first instance of the text into these variables if the text is found.

MatchWholeWordOnly

Boolean

Optional. 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

 

例子:

l = -1

t = -1

r = -1

b = -1

Succeeded = TextUtil.GetTextLocation("文件",0,l,t,r,b,false)

If Not Succeeded Then

       MsgBox "Text not found"

else

       x = (l+r) / 2

       y = (t+b) / 2

       Set dr = CreateObject("Mercury.DeviceReplay")

          dr.MouseMove x,y

       dr.MouseClick x, y, 0

End If

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值