Verifying Toast

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

Do you have any sample code for tessearct OCR?

Hello
I have try the above link and created the example with tess4j 3.2.1. when i execute the example it take the screenshot and convert the image to text but not the toast message of the image .please help me out ?
Below is image and output result:
1

Has anyone have a clue to how to use Toast on Appium 1.6.3?

Appium release notes

Android - Uiautomator2

Add ability to verify TOAST messages (these can’t be interacted with, only
text retrieval allowed)

It looks like it’s supported but there’s very few threads out there that are helpful.

Please refer this sample java test case to verify toast message.

how read toast message from native app using appium…

Hi Kiran,

I found one, hope u will enjoy it.

Hi Dear Hamza
I have follow your suggestion:
1-pip install tesseract
2-pip install pytesseract
3-pip install pillow
4-pip install tesseract-ocr

then I do not know how to start the codes to take capture of the elements inside the toast?
I might need your help on the detailed steps for that!!!

thanks very much and appreciated.

import org.bytedeco.javacpp.;
import static org.bytedeco.javacpp.lept.
;
import static org.bytedeco.javacpp.tesseract.*;

public class BasicExample {
public static void main(String[] args) {
BytePointer outText;

    TessBaseAPI api = new TessBaseAPI();
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api.Init(null, "eng") != 0) {
        System.err.println("Could not initialize tesseract.");
        System.exit(1);
    }

    // Open input image with leptonica library
    PIX image = pixRead(args.length > 0 ? args[0] : "/usr/src/tesseract/testing/phototest.tif");
    api.SetImage(image);
    // Get OCR result
    outText = api.GetUTF8Text();
    System.out.println("OCR output:\n" + outText.getString());

    // Destroy used object and release memory
    api.End();
    outText.deallocate();
    pixDestroy(image);
}

}

Can you please the detailed video where the dll placing is also defined? this video has a very limited understanding but didn’t worked when i tried this

Appium Directly does not give any API to read toast message we need to do it using tess4j jar. First we need to take screen shot and then we need to read the text from screen shot using tess4j API.

static String scrShotDir = “screenshots”;
File scrFile;
static File scrShotDirPath = new java.io.File("./"+ scrShotDir+ “//”);
String destFile;
static AndroidDriver driver = null;

public String readToastMessage() throws TesseractException {
String imgName = takeScreenShot();
String result = null;
File imageFile = new File(scrShotDirPath, imgName);
System.out.println(“Image name is :” + imageFile.toString());
ITesseract instance = new Tesseract();

File tessDataFolder = LoadLibs.extractTessResources(“tessdata”); // Extracts
// Tessdata
// folder
// from
// referenced
// tess4j
// jar
// for
// language
// support
instance.setDatapath(tessDataFolder.getAbsolutePath()); // sets tessData
// path

result = instance.doOCR(imageFile);
System.out.println(result);
return result;
}

/**

  • Takes screenshot of active screen
  • @return ImageFileName
    */
    public String takeScreenShot() {
    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

SimpleDateFormat dateFormat = new SimpleDateFormat(“dd-MMM-yyyy__hh_mm_ssaa”);
new File(scrShotDir).mkdirs(); // Create folder under project with name
// “screenshots” if doesn’t exist
destFile = dateFormat.format(new Date()) + “.png”; // Set file name
// using current
// date time.
try {
FileUtils.copyFile(scrFile, new File(scrShotDir + “/” + destFile)); // Copy
// paste
// file
// at
// destination
// folder
// location
} catch (IOException e) {
System.out.println(“Image not transfered to screenshot folder”);
e.printStackTrace();
}
return destFile;
}

For more details Refer this video - https://www.youtube.com/watch?v=lM6-ZFXiSls

1 Like

How to verify toast messages using ruby lib in BDD framework ?

In Ruby I’m looking for toast like this:

toast = driver.find_element(:xpath, "//android.widget.Toast[1]")

then you can use .text to get toast text like:

if toast.text == "Hello"

1 Like

I am Can verify toast with this, But if i use uiautomator2, I am not able o identify few elements