Wednesday, 17 January 2018

How to Capture Screenshot when test failes with Selenium Webdriver

As we know it is very important to know the reason behind test failure we need to capture screenshot when test case fails. The reasons behind test failure can be a bug in application or error in our automation scripts so to know the exact problem it is very important to capture screenshot on test case failure.

I have already wrote a post on how to take screenshot you can refer that Post HERE. In this post we will see how to capture screenshot in case of test case failure.

1. ITestResult Interface  - this will provide us the test case execution status and test case name.
2. @AfterMethod - An annotation of TestNG which will execute after every test execution irrespective of  test case pass or fail @AfterMethod will always execute.

Lets take look complete code -

package captureScreenshot;
// Import all classes and interface
import library.Utility;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class FacebookScreenshotExample {
// Create Webdriver reference
WebDriver driver; 
System.setProperty("", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
 driver=new ChromeDriver(); 

public void captureScreenshot() throws Exception
// Initiate Chrome browser

// Maximize the browser
// Pass application url
// Here we are forcefully passing wrong id so that it will fail our testcase
driver.findElement(By.xpath(".//*[@id='emailasdasdas']")).sendKeys("QA Automated");
// It will execute after every test execution 
public void tearDown(ITestResult result)
// Here will compare if test is failing then only it will enter into if condition
// Create refernce of TakesScreenshot
TakesScreenshot ts=(TakesScreenshot)driver;
// Call method to capture screenshot
File source=ts.getScreenshotAs(OutputType.FILE);
// Copy files to specific location here it will save all screenshot in our project home directory and
// result.getName() will return name of test case so that screenshot name will be same
FileUtils.copyFile(source, new File("./ScreenShots/"+result.getName()+".png"));
System.out.println("Screenshot taken");
catch (Exception e)
System.out.println("Exception while taking screenshot "+e.getMessage());
// close application

Tuesday, 16 January 2018

How to Scroll using Selenium WebDriver

In this post we are going to talk about how exactly we can scroll this using Selenium. Unfortunately Selenium does not have inbuilt method which allow us to scroll into view but, we can scroll into view in Selenium using JavaScript executor.

Now let us see the detailed code on how to Scroll using Selenium WebDriver

package Demo;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class TestScroll {

public static void main(String[] args) {

// Start browser

WebDriver driver=new FirefoxDriver();

// Maximize browser


// Pass application URL


// Create instance of Javascript executor

JavascriptExecutor je = (JavascriptExecutor) driver;

//Identify the WebElement which will appear after scrolling down

WebElement element = driver.findElement(By.xpath(".//*[@id='id_sample']"));

// now execute query which actually will scroll until that element is not appeared on page.


// Extract the text and verify




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.text.SimpleDateFormat;
import java.util.Date;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;


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