FindRectByText Method

Applies to TestComplete 12.60, last modified on September 17, 2018
The Optical Character Recognition feature provided by the OCR plugin is deprecated. It was removed from TestComplete in version 12.60 and is no longer available for later versions.
Instead, TestComplete version 12.60 introduces the new Optical Character Recognition engine featuring Google Cloud Vision API. To learn more, see Optical Character Recognition.
If you need to use the legacy feature, please contact our Customer Care team.

Description

The OCRObject.FindRectByText method lets you recognize all of the text within the visible area of an onscreen object and searches for a rectangular region in this area by the text this region holds.

Declaration

OCRObjectObj.FindRectByText(TextOptions)

OCRObjectObj An expression, variable or parameter that specifies a reference to an OCRObject object
Text [in]    Required    String    
Options [in]    Optional    An OCROptions object    
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Text

Specifies the sought-for text.

Options

Specifies the OCROptions object that includes settings to be used during character recognition.

Result Value

True if a region that holds the sought-for text is found and False otherwise.

Remarks

If the method finds the region that includes the sought-for text, the FoundLeft, FoundTop, FoundHeight, FoundWidth, FoundX and FoundY properties of the OCRObject object will have the values that correspond to the found region.

The method can search for the exact match of the given string or for a string that is similar to the given string. This behavior is controlled by the OCROptions.ExactSearch setting. The amount of string similarity is set by the OCROptions.SearchAccuracy option.

A number of factors affect the recognition accuracy, read the Using Optical Character Recognition - Tips topic to learn how to improve it.

Example

The following example searches for the string “Test” in the Notepad window and posts an image of the found rectangular region to the test log.

JavaScript, JScript

function Test()
{
  var notepad, Window, Rect, OCRObj, s, X, Y;
  // Obtain the Notepad window
  notepad = Sys.Process("notepad");
  Window = notepad.Window("Notepad", "*").Window("Edit");

  // Capture an image of the Notepad window and post it to the test log
  Rect = Window.Picture();
  Log.Picture(Rect, "Region with the text to be recognized");

  // Create an OCR object
  OCRObj = OCR.CreateObject(Rect);

  if (OCRObj.FindRectByText("Test"))
    Log.Picture(Window.Picture(OCRObj.FoundLeft, OCRObj.FoundTop, OCRObj.FoundWidth, OCRObj.FoundHeight), "Found region.");

}

Python

def Test():
  # Obtain the Notepad window
  notepad = Sys.Process("notepad")
  Window = notepad.Window("Notepad", "*").Window("Edit")
  # Capture an image of the Notepad window and post it to the test log
  Rect = Window.Picture()
  Log.Picture(Rect, "Region with the text to be recognized")
  # Create an OCR object
  OCRObj = OCR.CreateObject(Rect)
  if OCRObj.FindRectByText("Test"):
    Log.Picture(Window.Picture(OCRObj.FoundLeft, OCRObj.FoundTop, OCRObj.FoundWidth, OCRObj.FoundHeight), "Found region.")

VBScript

Sub Test

  ' Obtain the Notepad window
  Set notepad = Sys.Process("notepad")
  Set Window = notepad.Window("Notepad", "*").Window("Edit")

  ' Capture an image of the Notepad window and post it to the test log
  Set Rect = Window.Picture
  Call Log.Picture(Rect, "Region with the text to be recognized")

  ' Create an OCR object
  Set OCRObj = OCR.CreateObject(Rect)

  If OCRObj.FindRectByText("Test") Then
    Call Log.Picture(Window.Picture(OCRObj.FoundLeft, OCRObj.FoundTop, OCRObj.FoundWidth, OCRObj.FoundHeight), "Found region.")
  End If

End Sub

DelphiScript

procedure Test();
var notepad, Window, Rect, OCRObj, s, X, Y: OleVariant;
begin
  // Obtain the Notepad window
  notepad := Sys.Process('notepad');
  Window := notepad.Window('Notepad', '*').Window('Edit');

  // Capture an image of the Notepad window and post it to the test log
  Rect := Window.Picture();
  Log.Picture(Rect, 'Region with the text to be recognized');

  // Create an OCR object
  OCRObj := OCR.CreateObject(Rect);

  if OCRObj.FindRectByText('Test') then
    Log.Picture(Window.Picture(OCRObj.FoundLeft, OCRObj.FoundTop, OCRObj.FoundWidth, OCRObj.FoundHeight), 'Found region.');

end;

C++Script, C#Script

function Test()
{
  var notepad, Window, Rect, OCRObj, s, X, Y;
  // Obtain the Notepad window
  notepad = Sys["Process"]("notepad");
  Window = notepad["Window"]("Notepad", "*")["Window"]("Edit");

  // Capture an image of the Notepad window and post it to the test log
  Rect = Window["Picture"]();
  Log["Picture"](Rect, "Region with the text to be recognized");

  // Create an OCR object
  OCRObj = OCR["CreateObject"](Rect);

  if (OCRObj["FindRectByText"]("Test"))
    Log["Picture"](Window["Picture"](OCRObj["FoundLeft"], OCRObj["FoundTop"], OCRObj["FoundWidth"], OCRObj["FoundHeight"]), "Found region.");

}

See Also

OCROptions Object
GetText Method
FoundLeft Property
FoundTop Property
FoundHeight Property
FoundWidth Property
FoundX Property
FoundY Property
Using Optical Character Recognition - Tips

Highlight search results