QTP的TextUtil物件的使用

TIB發表於2010-01-21

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

 

 

相關文章