如何使用UFT的GetVisibleText、GetROProperty、GetTextLocation函数?
Hey there! Let me walk you through how to use UFT's GetVisibleText, GetROProperty, and GetTextLocation specifically for rich client applications (like WPF, WinForms, or SAP GUI for Windows)—these tools are total workhorses when you need to interact with or validate text in thick clients.
This function pulls the visible, on-screen text from a control or a specific region of a control. It’s perfect when you need to read text that’s displayed on buttons, labels, or even custom UI elements that render text visually.
Syntax
object.GetVisibleText([left, top, right, bottom])
- The optional
left, top, right, bottomparameters let you narrow down to a specific rectangular region of the object (useful if only part of the text matters).
Example for Rich Client
Say you have a WPF button in your app and want to confirm its displayed text:
' Identify the WPF button object Set submitBtn = Window("MyRichApp").WpfButton("btnSubmit") ' Grab all visible text from the button btnText = submitBtn.GetVisibleText() ' Validate the text If btnText = "Submit Order" Then MsgBox "Button text is correct!" End If
If you only need a portion of the text (e.g., the first 50px width of a long label):
Set orderLabel = Window("MyRichApp").WpfLabel("lblOrderStatus") partialText = orderLabel.GetVisibleText(0, 0, 50, 20) MsgBox "Partial status text: " & partialText
This is your go-to for fetching runtime properties of a rich client control. Every rich client control has unique properties (like AutomationId for WPF, Text for WinForms, or Caption for SAP GUI) that you can access to validate state, content, or metadata.
Syntax
object.GetROProperty(propertyName)
- To find which properties are supported by your control, use UFT's Object Spy tool—just hover over the control and check the "Runtime Properties" section.
Example for Rich Client
Let’s validate that a WinForms text box has the correct input:
Set usernameField = Window("MyWinFormsApp").WinEdit("txtUsername") ' Get the current text in the field currentUser = usernameField.GetROProperty("Text") If currentUser = "john_doe" Then MsgBox "Username is correctly populated!" End If
Or check if a WPF button is enabled before clicking it:
Set saveBtn = Window("MyWPFApp").WpfButton("btnSave") isBtnEnabled = saveBtn.GetROProperty("IsEnabled") If isBtnEnabled Then saveBtn.Click Else MsgBox "Save button is disabled—can't proceed!" End If
Use this function when you need to find the on-screen coordinates of specific text within a control or window. It’s great for scenarios where you need to click near text, capture a screenshot of a text region, or validate text placement.
Syntax
object.GetTextLocation(textToFind, left, top, right, bottom [, matchWholeWord][, caseSensitive])
left, top, right, bottomare output variables (declare them first) that will hold the coordinates of the text’s bounding box.- Optional parameters:
matchWholeWord(True/False) andcaseSensitive(True/False) let you refine the text search.
Example for Rich Client
Suppose you need to click on the "Help" text link in a WPF window:
Set mainWindow = Window("MyRichApp") ' Declare output variables Dim textLeft, textTop, textRight, textBottom ' Search for the text (whole word, case-sensitive) textFound = mainWindow.GetTextLocation("Help", textLeft, textTop, textRight, textBottom, True, True) If textFound Then MsgBox "Help text found at coordinates: (" & textLeft & "," & textTop & ")" ' Click the center of the text mainWindow.Click (textLeft + textRight)/2, (textTop + textBottom)/2 Else MsgBox "Help text not found on screen!" End If
Quick Note for Rich Clients
If you’re working with custom or non-standard rich client controls, some of these functions might rely on UFT’s OCR (Optical Character Recognition) engine. You can enable OCR for specific objects in UFT’s object settings if needed.
内容的提问来源于stack exchange,提问作者narcisse

