Applies to TestComplete 14.70, last modified on October 26, 2020

Description

Use the OCRTextDocument object to access the text that TestComplete recognizes in the specified UI element or image. The object describes all the recognized text and individual text blocks and tabular data with their bounds (rectangles within the UI element or image that contains the text block). To get the object in your test, use the OCR.Recognize method.

Members

Example

The following example shows how to use optical character recognition to recognize text in the About dialog of Windows Notepad and verifies that the recognized text contains the About Notepad substring.

JavaScript, JScript

function VerifyRecognizedText()
{
  var textToCheck = "*About*Notepad*";

  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);

        // Verify that the recognized text matches the specified string
        recognizedText.CheckText(textToCheck);

      }
    }
  }
  else
  {
    Log.Warning("Notepad is not running.");
  }
}

Python

def VerifyRecognizedText():
   textToCheck = "*About*Notepad*"

   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)

         # Verify that the recognized text matches the specified string
         recognizedText.CheckText(textToCheck)

   else:
     Log.Warning("Notepad is not running.")

VBScript

Sub VerifyRecognizedText
  textToCheck = "*About*Notepad*"

  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)

        ' Verify that the recognized text matches the specified string
        Call recognizedText.CheckText(textToCheck)

      End If
    End If
  Else
    Log.Warning("Notepad is not running.")
  End If
End Sub

DelphiScript

procedure VerifyRecognizedText();
var textToCheck, p, wndNotepad, wndAbout, recognizedText;
begin
  textToCheck := '*About*Notepad*';

  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);

        // Verify that the recognized text matches the specified string
        recognizedText.CheckText(textToCheck);

      end;
    end;
  end  else
    Log.Warning('Notepad is not running.');
end;

C++Script, C#Script

function VerifyRecognizedText()
{
  var textToCheck = "*About*Notepad*";

  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);

        // Verify that the recognized text matches the specified string
        recognizedText["CheckText"](textToCheck);

      }
    }
  }
  else
  {
    Log["Warning"]("Notepad is not running.");
  }
}

See Also

Optical Character Recognition
OCR Object

Highlight search results