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.
Before embarking on the optical recognition, it is necessary to disable screen font's smooth view which is done as follows:
Properties
menu item.In order to recognize text from a control we need to perform the following steps:
16
inputted to it to have a possibility to check that the sqrt button has been truly pushed as shown in the following screenshot:var wCalc = Sys.Process("CalcPlus").Window("SciCalc", "Calculator Plus"); wCalc.Activate(); wCalc.Keys("16");
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.
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); }
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.
If the needed text is recognized in part, there are two methods to resolve the issue of partially incorrect recognition. They are as follows:
FindRectByText
method. For example:calcOCR.FindRectByText("s*rt")
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); }
3.144.41.229