Verify Text Contents

Applies to TestComplete 14.71, last modified on April 22, 2021

In your tests, you may need to verify that the text your tested application renders on screen matches a pattern (for example, it includes a substring). To do this, you can use OCR checkpoints:

  1. Get a screen area that contains the text you want to verify. It can be any of the following:

    • An onscreen object. It can be a control, a window or the entire screen.

      The selected object must exist in your system and must be visible on screen.

      Note: To specify an object in a mobile application, you can select it in the Mobile Screen window. Both the application under test, and the mobile device where the application is running must be prepared for testing in order for TestComplete to be able to access them.
      To get the entire screen of your mobile device, you can use the Mobile.Device.Desktop property.

    • An expression that returns a reference to the Picture object.

      Note: When creating an OCR checkpoint in a keyword test, you cannot specify the Picture object directly. As a workaround, add an arbitrary OCR checkpoint to the test and then modify it to verify the needed Picture object via the Operation Parameters dialog.

  2. Specify a pattern against which TestComplete will verify the text. You can use an asterisk (*) or a question mark (?) wildcards. The asterisk corresponds to a string of any length (including an empty string), the question mark corresponds to any single character (including none).

  3. Depending on the verification results, TestComplete will post either the Passed or the Failed message to the test log.

In keyword tests

Use the OCR Checkpoint operation:

Verify text contents in keyword tests

In scripts

Use the OCR.Recognize.CheckText method:

JavaScript, JScript

// Verifies whether the '*substring*' pattern matches the optically recognized text in:
// A tested application main form
OCR.Recognize(Aliases.myApp.wndForm).CheckText("*substring*");
// A picture captured in the tested application main form
OCR.Recognize(Aliases.myApp.wndForm.Picture()).CheckText("*substring*");
// Your computer desktop
OCR.Recognize(Sys.Desktop).CheckText("*substring*");
// A region captured in a tested application main form
OCR.Recognize(Aliases.myApp.wndForm.Picture(0, 0, 200, 400)).CheckText("*substring*");

Python

# Verifies whether the '*substring*' pattern matches the optically recognized text in:
# A tested application main form
OCR.Recognize(Aliases.myApp.wndForm).CheckText("*substring*")
# A picture captured in the tested application main form
OCR.Recognize(Aliases.myApp.wndForm.Picture()).CheckText("*substring*")
# Your computer desktop
OCR.Recognize(Sys.Desktop).CheckText("*substring*")
# A region captured in a tested application main form
OCR.Recognize(Aliases.myApp.wndForm.Picture(0, 0, 200, 400)).CheckText("*substring*")

VBScript

' Verifies whether the '*substring*' pattern matches the optically recognized text in:
' A tested application main form
Call OCR.Recognize(Aliases.myApp.wndForm).CheckText("*substring*")
' A picture captured in the tested application main form
Call OCR.Recognize(Aliases.myApp.wndForm.Picture()).CheckText("*substring*")
' Your computer desktop
Call OCR.Recognize(Sys.Desktop).CheckText("*substring*")
' A region captured in a tested application main form
Call OCR.Recognize(Aliases.myApp.wndForm.Picture(0, 0, 200, 400)).CheckText("*substring*")

DelphiScript

// Verifies whether the '*substring*' pattern matches the optically recognized text in:
// A tested application main form
OCR.Recognize(Aliases.myApp.wndForm).CheckText('*substring*');
// A picture captured in the tested application main form
OCR.Recognize(Aliases.myApp.wndForm.Picture()).CheckText('*substring*');
// Your computer desktop
OCR.Recognize(Sys.Desktop).CheckText('*substring*');
// A region captured in a tested application main form
OCR.Recognize(Aliases.myApp.wndForm.Picture(0, 0, 200, 400)).CheckText('*substring*');

C#Script

// Verifies whether the '*substring*' pattern matches the optically recognized text in:
// A tested application main form
OCR["Recognize"](Aliases["myApp"]["wndForm"])["CheckText"]("*substring*");
// A picture captured in the tested application main form
OCR["Recognize"](Aliases["myApp"]["wndForm"]["Picture"]())["CheckText"]("*substring*");
// Your computer desktop
OCR["Recognize"](Sys["Desktop"])["CheckText"]("*substring*");
// A region captured in a tested application main form
OCR["Recognize"](Aliases["myApp"]["wndForm"]["Picture"](0, 0, 200, 400))["CheckText"]("*substring*");

A Possible Alternative

To check whether the recognized text contains the expected substring, you can use the if … then … else statement and various operations that work with string values. This way, you can implement a custom check and perform various actions depending on the check result (for example, to post custom messages to the test log or to simulate user actions). To learn more, see Advanced Text Contents Verification.

See Also

About Optical Character Recognition
About OCR Checkpoints
Advanced Text Contents Verification
About Mobile Screen Window
Selecting an Object on the Mobile Screen (Android Testing)
Select an Object on the Mobile Screen (iOS Testing)

Highlight search results