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;
 }
}

Saturday, 7 October 2017

Swipe Right,Left,Up & Down using Appium

This is very useful tutorial for those who are learning appium as swiping and scrolling is commonly used for almost all mobile apps. So learning to automate it using Appium is going to help you in writing test code.Below is the code where I have written different functions for swiping from top to bottom, from bottom to top, from left to right, from right to left using Appium. We can all these methods in our test cases to perform required actions. plus I have created a video tutorial with detailed explanation.



/**
  /* Method to swipe screen from Bottom to Top (Vertical) Get the size of
  * screen. Find swipe start and end point from screen's width and height.
  * Find starty point which is at bottom side of screen. Find endy point
  * which is at top side of screen. Find horizontal point where you wants to
  * swipe. It is in middle of screen width.
  * Time duration should be in milliseconds
  */

 public void bottomTopswipe(int timeduration) {

  
  size = driver.manage().window().getSize();
  System.out.println(size);
  starty = (int) (size.height * 0.50);
  endy = (int) (size.height * 0.20);
  startx = size.width / 2;
  System.out.println("Start swipe operation");
  driver.swipe(startx, starty, startx, endy, timeduration);

 }

 /**
  * 
  * Method to swipe screen from Top to Bottom (Vertical) Get the size of
  * screen. Find swipe start and end point from screen's width and height.
  * Find starty point which is at bottom side of screen. Find endy point
  * which is at top side of screen. Find horizontal point where you wants to
  * swipe. It is in middle of screen width. 
         * Time duration should be in milliseconds
   */

 public void topBottomswipe(int timeduration) {

  
  size = driver.manage().window().getSize();
  System.out.println(size);
  starty = (int) (size.height * 0.50);
  endy = (int) (size.height * 0.20);
  startx = size.width / 2;
  System.out.println("Start swipe operation");
  driver.swipe(startx, endy, startx, starty, timeduration);

 }

 /**
  * 
  * Method to swipe screen from right to left (Horizontal) duration should be
  * in milliseconds Get the size of screen. Find swipe start and end point
  * from screen's width and height. Find startx point which is at right side
  * of screen. Find endx point which is at left side of screen. Find vertical
  * point where you wants to swipe. It is in middle of screen height. 
         * Time duration should be in milliseconds
   */

 public void rightLeftSwipe(int timeduration) {

  size = driver.manage().window().getSize();
  System.out.println(size);
  startx = (int) (size.width * 0.70);
  endx = (int) (size.width * 0.30);
  starty = size.height / 2;
  System.out.println("Start swipe operation");
  driver.swipe(startx, starty, endx, starty, timeduration);

 }

 /**
  * 
  * Method to swipe screen from left to right (Horizontal) duration should be
  * in milliseconds Get the size of screen. Find swipe start and end point
  * from screen's width and height. Find startx point which is at right side
  * of screen. Find endx point which is at left side of screen. Find vertical
  * point where you wants to swipe. It is in middle of screen height. 
         * Time duration should be in milliseconds
      */

 public void leftRightSwipe(int timeduration) {
  // duration should be in milliseconds
  size = driver.manage().window().getSize();
  System.out.println(size);
  startx = (int) (size.width * 0.70);
  endx = (int) (size.width * 0.30);
  starty = size.height / 2;
  System.out.println("Start swipe operation");
  driver.swipe(endx, starty, startx, starty, timeduration);

 }

Monday, 19 June 2017

Selenium Webinar by Perfecto: Automate More with Selenium for your Responsive Web Design

Hey guys this is upcoming Free Webinar by Perfecto and I feel its is very important to attend if you are working with Selenium or you want to learn Selenium. I always believe in attending these types of webniar as they are conducted by experienced people and I always end up learning something new at the end.

If you are passionate about learning then attending this session will ad on something new to you knowledge bucket.

Let us see what so special about thise Webniar which is held on 22nd June 11am EST/ 8am PST/ 4pm GMT for India its 8.30 PM IST. -

In this webinar, Automate More with Selenium for your RWD, Perfecto’s Uzi Eilon and Eran Kinsbruner will walk you through several technical challenges and solutions around test automation for responsive sites. See live demos around testing responsive web sites using extended test automation capabilities that can increase your test coverage suite.

You will learn how to:
1.Author basic selenium scripts using a powerful recorder for both mobile and web
2.Define a robust XPath using an innovative free online tool
3.Build a test lab for parallel script execution on multiple devices and browsers
4. Gain high quality analysis post execution with mature digital reporting

Always Spend time in learning  something new and click here to Register Now .
This is Free Webinar .
I will also be attending the same so see you all guys ..:-)
Keep Learning Keep Testing !!!