OCR Object

Applies to TestComplete 15.40, last modified on March 25, 2022

This topic describes the OCR object introduced in TestComplete version 12.60. It is provided by the Optical Character Recognition feature of the TestComplete Intelligent Quality add-on. For information on the OCR object used in earlier versions of TestComplete, see OCR Object (Deprecated).

Description

Use the OCR object to access optical character recognition in your tests and recognize and get text and tables rendered on the screen or represented in an image.

Requirements
  • Your TestComplete version must be 12.60 or later.

  • You need an active license for the TestComplete Intelligent Quality add-on.

  • The Intelligent Quality add-on must be enabled in TestComplete.

    You can enable the add-on during TestComplete installation. If you did not enable the add-on during the installation, you can do this at any moment later via the File > Install Extensions dialog. To do that, select File > Install Extensions from the TestComplete main menu and enable the Intelligent Quality > Intelligent Quality Core plugin in the resulting dialog.

  • Optical Character Recognition support must be enabled in TestComplete.

    By default, it is enabled automatically if you enable the Intelligent Quality add-on during TestComplete installation. If you experience issues with optical character recognition in your tests, select File > Install Extensions from the TestComplete main menu and make sure the Optical Character Recognition plugin is enabled (you can find it in the Intelligent Quality group). If the plugin is disabled, enable it. In the confirmation message that TestComplete shows when you enable the plugin, click the link to read a third-party license agreement. If you agree to the license terms, click Enable OCR.

    In addition, make sure that you do not have the legacy OCR plugin installed and enabled. You can find the plugin in the Common group.

  • Your computer must have access to the ocr.api.dev.smartbear.com web service. If you have firewalls or proxies running in your network, they should allow your computer to access the web service.

  • Your firewall must allow traffic through port 443.

Members

Example

The following example shows how to use optical character recognition to recognize text in the About dialog of Windows Notepad and post the recognized text to the test log.

JavaScript, JScript

function GetRecognizedText()
{
  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);

        // Post the recognized text to the test log
        Log.Message("View all the recognized text in the Details panel", recognizedText.FullText);

        // Post portions of the recognized text to the test log
        if (recognizedText.BlockCount > 0)
        {
          Log.AppendFolder("Recognized text by blocks");
          for (var i = 0; i < recognizedText.BlockCount; i++)
          {
            Log.Message(recognizedText.Block(i).Text);
          }
          Log.PopLogFolder();
        }

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

Python

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

         # Post the recognized text to the test log
         Log.Message("View all the recognized text in the Details panel", recognizedText.FullText)

         # Post portions of the recognized text to the test log
         if recognizedText.BlockCount > 0:
           Log.AppendFolder("Recognized text by blocks")
           for i in range(0, recognizedText.BlockCount):
             Log.Message(recognizedText.Block[i].Text)
           Log.PopLogFolder()

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

VBScript

Sub GetRecognizedText
  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)

        ' Post the recognized text to the test log
        Call Log.Message("View all the recognized text in the Details panel", recognizedText.FullText)

        ' Post portions of the recognized text to the test log
        If recognizedText.BlockCount > 0 Then
          Log.AppendFolder("Recognized text by blocks")
          For i = 0 To recognizedText.BlockCount - 1
            Log.Message(recognizedText.Block(i).Text)
          Next
          Log.PopLogFolder
        End If

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

DelphiScript

procedure GetRecognizedText();
var p, wndNotepad, wndAbout, recognizedText, i;
begin
  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);

        // Post the recognized text to the test log
        Log.Message('View all the recognized text in the Details panel', recognizedText.FullText);

        // Post portions of the recognized text to the test log
        if recognizedText.BlockCount > 0 then
        begin
          Log.AppendFolder('Recognized text by blocks');
          for i := 0 to recognizedText.BlockCount - 1 do
            Log.Message(recognizedText.Block[i].Text);
          Log.PopLogFolder;
        end;

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

C++Script, C#Script

function GetRecognizedText()
{
  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);

        // Post the recognized text to the test log
        Log["Message"]("View all the recognized text in the Details panel", recognizedText["FullText"]);

        // Post portions of the recognized text to the test log
        if (recognizedText["BlockCount"] > 0)
        {
          Log["AppendFolder"]("Recognized text by blocks");
          for (var i = 0; i < recognizedText["BlockCount"]; i++)
          {
            Log["Message"](recognizedText["Block"](i)["Text"]);
          }
          Log["PopLogFolder"]();
        }

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

See Also

Optical Character Recognition

Highlight search results