Verifying Toast

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
            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?

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:

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.");

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

    // Destroy used object and release memory


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"./"+ 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);
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”);
return destFile;

For more details Refer this video -

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

I’m using:
io.appium 6.1.0
selenium-java 3.11.0
guava 24.0-jre.

Try this:
String xmlFormat = driver.getPageSource();
if(xmlFormat.contains(“Your Toast Message Here”)){
System.out.println("Toast message displayed: "+yourToastMessage);

  • The first line of instruction will give the xml format of the app page. In this, I got the toast message in one of the tags. It works for me.
    You can put the getPageSource instruction after the desired activity, and since toast messages generally have 2 seconds of timeOut, so the above method will work.
1 Like

I tried your method but for me is not working.
I don’t think you have a toast message there as far as the getPageSource will find the message you looking for.
Normally if getPageSource is finding the element/text we should be able to identify the element like the rest elements.

Hi @Zuzeac,

We need to check if the xmlFormat string contains the toast message. Whether getPageSource() method is able to capture the exact XML DOM at the time of the toast message getting displayed is dependent on how you tweak wait times to ensure the exact toast message is captured.
You need to give some TimeOut.MILLISECONDS.sleep(200) or 500 before the page source is captured.
Also, the capture has to be done before the toast message vanishes away.
I think the standard of any toast message time out is 2 seconds.

Please let me know after doing it if you are still facing any problems.
More than this, I need to see your code what you are exactly trying to achieve.

Thanks … Cheers :slight_smile: