OCRTextBlock Object

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

Use the OCRTextBlock object to access an individual portion of text that TestComplete recognizes in a UI element or image. To get the object in tests, use one of the following:

Members

Example

The code below shows how to get the OCRTextBlock object by using the BlockByText method:

JavaScript, JScript

function GetObjectByText()
{
  var textToGet = "OK";

  var p = Sys.WaitProcess("notepad", 3000);
  if (p.Exists)
  {
    // Get the Notepad window
    var wndNotepad = p.WaitWindow("Notepad", "*", 1, 3000);
    if (wndNotepad.Exists)
    {
      wndNotepad.MainMenu.Click("Help|About Notepad");
      // Get the About Notepad window
      var wndAbout = p.WaitWindow("#32770", "About Notepad", 1, 3000);
      if (wndAbout.Exists)
      {
        // Recognize the text that the About Notepad window contains
        var recognizedText = OCR.Recognize(wndAbout);

        // Checks whether the recognized text contains the needed fragment
        if (aqString.Find(recognizedText.FullText, textToGet, 0, false) > -1)
        {
          var okTextBlock = recognizedText.BlockByText(textToGet);
          // Click on the window area that contains the specified text
          okTextBlock.Click();
        }
      }
    }
  }
  else
  {
    Log.Warning("Notepad is not running.");
  }
}

Python

def GetObjectByText():
  textToGet = "OK"

  p = Sys.WaitProcess("notepad", 3000)
  if p.Exists:
    # Get the Notepad window
    wndNotepad = p.WaitWindow("Notepad", "*", 1, 3000)
    if wndNotepad.Exists:
      wndNotepad.MainMenu.Click("Help|About Notepad")
      # Get the About Notepad window
      wndAbout = p.WaitWindow("#32770", "About Notepad", 1, 3000)
      if wndAbout.Exists:
        # Recognize the text that the About Notepad window contains
        recognizedText = OCR.Recognize(wndAbout)

        # Checks whether the recognized text contains the needed fragment
        if aqString.Find(recognizedText.FullText, textToGet, 0, False) > -1:
          okTextBlock = recognizedText.BlockByText(textToGet)
          # Click on the window area that contains the specified text
          okTextBlock.Click()
  else:
    Log.Warning("Notepad is not running.")

VBScript

Sub GetObjectByText

  textToGet = "OK"

  Set p = Sys.WaitProcess("notepad", 3000)
  If p.Exists Then
    ' Get the Notepad window
    Set wndNotepad = p.WaitWindow("Notepad", "*", 1, 3000)
    If wndNotepad.Exists Then
      wndNotepad.MainMenu.Click("Help|About Notepad")
      ' Get the About Notepad window
      Set wndAbout = p.WaitWindow("#32770", "About Notepad", 1, 3000)
      If wndAbout.Exists Then
        ' Recognize the text that the About Notepad window contains
        Set recognizedText = OCR.Recognize(wndAbout)

        ' Checks whether the recognized text contains the needed fragment
        If aqString.Find(recognizedText.FullText, textToGet, 0, False) > -1 Then
          Set okTextBlock = recognizedText.BlockByText(textToGet)
          ' Click on the window area that contains the specified text
          okTextBlock.Click
        End If
      End If
    End If
  Else
    Log.Warning("Notepad is not running.")
  End If
End Sub

DelphiScript

procedure GetObjectByText();
var textToGet, p, wndNotepad, wndAbout, recognizedText, okTextBlock;
begin

  textToGet := 'OK';

  p := Sys.WaitProcess('notepad', 3000);
  if p.Exists then
  begin
    // Get the Notepad window
    wndNotepad := p.WaitWindow('Notepad', '*', 1, 3000);
    if wndNotepad.Exists then
    begin
      wndNotepad.MainMenu.Click('Help|About Notepad');
      // Get the About Notepad window
      wndAbout := p.WaitWindow('#32770', 'About Notepad', 1, 3000);
      if wndAbout.Exists then
      begin
        // Recognize the text that the About Notepad window contains
        recognizedText := OCR.Recognize(wndAbout);

        // Checks whether the recognized text contains the needed fragment
        if aqString.Find(recognizedText.FullText, textToGet, 0, false) > -1 then
        begin
          okTextBlock := recognizedText.BlockByText(textToGet);
          // Click on the window area that contains the specified text
          okTextBlock.Click;
        end;
      end;
    end;
  end
  else
    Log.Warning('Notepad is not running.');
end;

C++Script, C#Script

function GetObjectByText()
{
  var textToGet = "OK";

  var p = Sys["WaitProcess"]("notepad", 3000);
  if (p["Exists"])
  {
    // Get the Notepad window
    var wndNotepad = p["WaitWindow"]("Notepad", "*", 1, 3000);
    if (wndNotepad["Exists"])
    {
      wndNotepad["MainMenu"]["Click"]("Help|About Notepad");
      // Get the About Notepad window
      var wndAbout = p["WaitWindow"]("#32770", "About Notepad", 1, 3000);
      if (wndAbout["Exists"])
      {
        // Recognize the text that the About Notepad window contains
        var recognizedText = OCR["Recognize"](wndAbout);

        // Checks whether the recognized text contains the needed fragment
        if (aqString["Find"](recognizedText["FullText"], textToGet, 0, false) > -1)
        {
          var okTextBlock = recognizedText["BlockByText"](textToGet);
          // Click on the window area that contains the specified text
          okTextBlock["Click"]();
        }
      }
    }
  }
  else
  {
    Log["Warning"]("Notepad is not running.");
  }
}

See Also

Optical Character Recognition

Highlight search results