Verifying Toast

Here is an implementation code for this in python. It could be easily adopted to other languages

Please explain, how you used Tesseract OCR. I have downloaded but not not knowing that how to use?

Please see the link i posted above. And if you already did, what specific part of using the Tesseract OCR you’re having trouble?

I have installed Tessract ocr (version: 3.02.02). Now how i have to write in code? I am using appium+java.

After you imported the OCR lib, are you seeing the new methods provided by the Tesseract OCR library?

Hi Gourav ,

Did you find the Code in Java for verifying toast messages using Tessract ocr ?
If found could you please share the code as I am unable to get it.

Here is the Java Implementation. I personally used it across platforms and it worked like a charm.

2 Likes

HI Sahajamit,

Are your code successful for verifying toast message.

Yes It worked for me.

toast is system property,not handled by application. Toast messages can be instrument using espresso which is different instrumentation.
If test toast verification is on very high priority then you should not use appium or Uiautomator. Also one suggestion these kind of verifications need to be done in UI unit testing using Espresso or other tools which are much powerful tool than Appium (Provided you have access of application’s source code). Automation suites need to focus on regression e2e functionality, not on toast message or UI verification part.

1 Like

Priyank, I dont have source code of application? so cant we test toast?

We can test without source code as well. Please check the below video for more details

1 Like

is this possible if we use python?

What is Verify() method doing? :slight_smile:

I found a way for python.

First install tesseract like this:

sudo apt-get install tesseract

Then you can use it as below:

tesseract example.jpg out

install pytesseract using pip:

pip install pytesseract

install pillow if not done yet:

pip install pillow

install tesseract-ocr:

sudo apt-get install tesseract-ocr


Then use it in your python code like this:

`from PIL import Image
import pytesseract

path to your file

im = Image.open(‘test.png’)
print pytesseract.image_to_string(im)`

1 Like

Instead of checking and putting all energy just for sake of toast messages, the same we can easily cover using espresso at Unit/Integration test level. I believe that is better approach, We need to focus on critical functionality that really need to be automated instead of such trivial use cases. That way we can better categorize test cases also implement better test practices to our work.

Hey guys, can anyone help with a Java example for 3. Increase resolutions of all screenshots ?
Thanks a lot,

What i have so far | File screen = ((TakesScreenshot) AndroidDriverAppium.driver).getScreenshotAs(OutputType.FILE);

    Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping

     try {
            String result = instance.doOCR(screen);
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }

For who using Python, Can do that with the help of pyautogui Library this is piece of code that’s help

toaster_image = toaster image file path
screenshot_image = screenshot image file path [ taken by Appium ]

from pyautogui import locate

class ClassName:
    def compareImage (self, toaster_image, screenshot_image):
        result = locate(str(toaster_image), str(screenshot_image))
        if result is None:
            return False
        else:
            return True

Please send me the code on my mail id [email protected]

also send me the video part 2 for OCR