How to test Android toasts in Appium (selenium Java)

I am using Selenium with Java to run scripts on android (via Appium server). I see it is impossible to find toast with selenium

driver.findElement(By.LinkText("User not logged in")

      

in Appium

But it can be used in Selendroid to record toasts.

Can I use both Selendroid and Appium in the same script?

+3


source to share


5 answers


It looks like you cannot switch the driver type in the same session. If you are trying to switch to Selendroid just for toast validation, you can use OSR's pattern recognition engine.

Check this answer w / Ruby bindings

The idea is pretty simple:



  • a toast message will appear
  • take some screenshots
  • iterate through screenshots and search for the required text

Here is a nice and simple example of using OCR in Java: tess4j example (make sure Tesseract engine is installed)

0


source


Step 1:
File scrFile=null;
String path1 = null;
BufferedImage originalImage=null;
BufferedImage resizedImage=null;
System.out.println("Starting\n\n\n\n");
scrFile = ((TakesScreenshot) appiumDriver).getScreenshotAs(OutputType.FILE);
System.out.println("after scrfile\n\n\n\n");
originalImage = ImageIO.read(scrFile);
System.out.println("after originalFile\n\n\n");
BufferedImage.TYPE_INT_ARGB : originalImage.getType();
resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
Step 2: 
BufferedImage pathforToast= original image;
Step 3:
System.setProperty("jna.library.path","C:/Users/Dell/workspace/MOBILEFRAMEWORK/dlls/x64/");
Tesseract instance = Tesseract.getInstance();
`enter code here`ImageIO.scanForPlugins();
String result=null;
result = instance.doOCR(pathforToast);`enter code here`
System.out.println(result);`enter code here`

      



0


source


Appium 1.6.4@beta latest version supports toast messages

0


source


Take a snapshot of the Toast Message page and try converting the image file to text and check the text using the below code.

  public void imageconversion(String filePath) throws IOException,          
    {    
                ITesseract instance = new Tesseract();
    //file path is the image which you need to convert to text
                File imageFile = new File(filePath);  
                BufferedImage img = null;
                img = ImageIO.read(imageFile);
                BufferedImage blackNWhite = new BufferedImage(img.getWidth(),img.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
                Graphics2D graphics = blackNWhite.createGraphics();
                graphics.drawImage(img, 0, 0, null);
                //path where your downloaded tessdata exists
                instance.setDatapath("E://ocr//data"); 
              //What language you required to convert,( e.g. English)
                instance.setLanguage("eng");        
                String result = instance.doOCR(blackNWhite);


                System.out.println(result);

            }

      

0


source


Appium Directly doesn't provide an API for reading toasts, we have to do it using the tess4j jar. First we need to take a screenshot and then we need to read the text from the screen using the 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;
 }

      

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

0


source







All Articles