Using Optical Character Recognition (OCR)

Optical text recognition (OCR) is usable in cases where other methods of obtaining text from the targeted controls element are not up to the task for whatever reason.

We will supply an example of searching for the sqrt text in the calculator window, and making a mouse-click in the center of the located text.

Getting ready

Before embarking on the optical recognition, it is necessary to disable screen font's smooth view which is done as follows:

  1. Right-click on the Computer icon (on the desktop or in the Start menu) and go for the Properties menu item.
  2. Click on Advanced system setting to the right of the window pane.
  3. Go to the Advanced tab and click on the Settings button in the Performance group of options.
  4. On the Visual effects tab disable option Smooth edges of screen fonts and click on OK. Besides, it is recommended one uses standard Windows theme instead of classic. To shuffle this off:
    • Make the right-hand mouse-click on the desktop and opt for the Personalize menu item.
    • Opt for Windows Classic in the Basic and High Contrast Themes group.
  5. Launch Calculator Plus and set the standard mode operandi to it (navigate to View | Standard).

How to do it...

In order to recognize text from a control we need to perform the following steps:

  1. First of all, let's enable the calculator window and get the number 16 inputted to it to have a possibility to check that the sqrt button has been truly pushed as shown in the following screenshot:
    How to do it...
    var wCalc = Sys.Process("CalcPlus").Window("SciCalc", "Calculator Plus");
    wCalc.Activate();
    wCalc.Keys("16");
  2. Then we will create a new object OCR for all the calculator window and enter all of the text into the log, that is, the one TestComplete can recognize text in:
    var calcOCR = OCR.CreateObject(wCalc);
    Log.Message("All text from Calc", calcOCR.GetText());

    Usage of the GetText method in this case is not mandatory; however, having all the recognized text in the log can simplify analysis of recognition errors and their underlying causes.

  3. Further, we will locate the sqrt text, place the found image into the log, and make a mouse-click on it.
    if(calcOCR.FindRectByText("sqrt"))
      {
        var sqrtPic = wCalc.Picture(calcOCR.FoundLeft, calcOCR.FoundTop, calcOCR.FoundWidth, calcOCR.FoundHeight);
        Log.Picture(sqrtPic, "Found sqrt image");
        wCalc.Click(calcOCR.FoundX, calcOCR.FoundY);
      }
  4. Now, if one were to launch the function, the sqrt button would be clicked on and the square root calculations for 16, that is, the result being 4, would be inputted into the results field.

How it works...

The FindRectByText method places coordinates of the located text (FoundX, FoundTop, FoundWidth, and so on) into the object of the OCRObject type. Thanks to these coordinates, we have the possibility to make the mouse-click or obtain the image with the targeted text.

It is recommended that one should always place the located image(s) into the log (as we have done so with the help of the Log.Picture method); because there always exists a possibility of an incorrect recognition, the image up your sleeve might help you dig out the reason for any incorrect script's behavior, making the task much simpler.

TestComplete by default is capable of recognizing all the symbols of the Latin alphabet, digits, and some specific symbols, covering practically all the standard fonts, sizes, and drawings; however, the quality of recognition may vary from one case to another. Besides, text recognition within images is a prolonged operation.

This is why it is recommended one should apply OCR approach only in some rare and extreme use cases.

There's more...

If the needed text is recognized in part, there are two methods to resolve the issue of partially incorrect recognition. They are as follows:

  • Usage of the wildcard when passing parameter to the FindRectByText method. For example:
    calcOCR.FindRectByText("s*rt")
  • Usage of an imprecise search and impermissible mistakes. To this end, it is necessary to set the property of OCROptions.ExactSearch to be equal to false, and in the property of OCROptions.SearchAccuracy the value of the error margin should range from 1 (successful match) to 0. For example, the following code would be searching for sort text in the calculator window; however, it would click on the button sqrt, because text matching would fall within the arranged error margin:
    var OCROpt = calcOCR.CreateOptions();
    OCROpt.ExactSearch = false;
    OCROpt.SearchAccuracy = 0.8;
    if(calcOCR.FindRectByText("sort", OCROpt))
    {
      var sqrtPic = wCalc.Picture(calcOCR.FoundLeft, calcOCR.FoundTop, calcOCR.FoundWidth, calcOCR.FoundHeight);
      Log.Picture(sqrtPic, "Found sqrt image");
      wCalc.Click(calcOCR.FoundX, calcOCR.FoundY);
    }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.171.153