Saturday 16 December 2017

How to Test Toast Messages using Appium?

We can not directly used Appium Client API to test toast messages. So here we will import Tess4j jar in our project and their APIs to Read Toast Messages From our Screenshot . Once we have the text on the screen we can verify the toast message text in our Appium Test Case via various assertions .

For more Details Refer to this Video -


Let us see the code to take screen shot and then read the text from screen shot.



package com.appium.xample;



import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.util.LoadLibs;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

import io.appium.java_client.android.AndroidDriver;

public class ReadToastMessage {
 
  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;
 }
}

9 comments:

  1. hi,
    i tried with above code, first i got unsatisfiedlinked error , got the solution for that but now i am getting below error,

    Error opening data file /opt/local/share/tessdata/eng.traineddata
    Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory.
    Failed loading language 'eng'
    Tesseract couldn't load any languages!


    It will be better if you run any example using this in youtube video and suggest resolution of subsequent issues which i am getting.

    Thanks,
    Pranay

    ReplyDelete
  2. Thank you for your helpful video and code display!

    ReplyDelete
  3. Awesome example. Very helpful...

    Thank you..

    ReplyDelete
  4. Finally, we are able to read the toast message without need of taking screenshots.
    I have tested this on appium 1.15.1.

    Toast messages comes under com.package.system
    Normally, Xpath for this will be "/hierarchy/android.widget.Toast".
    And, Class Name will be "android.widget.settings"

    You can confirm this by refreshing element inspector screen when toast message is displayed.

    Code:
    WebDriverWait waitForToast = new WebDriverWait(driver.25);
    wait.until(ExpectedConditions.presenceOfElementLoacted(By.xpath("/hierarchy/android.widget.Toast")));
    String toastMessage = driver.findElement((By.xpath("/hierarchy/android.widget.Toast")).getText();
    System.out.println(toastMessage);

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Thank u so much! It works for me

    ReplyDelete