PAGE 39 : CAPTURE SCREEN SHOTS WHEN TESTS FAILS

In this post will cover how to capture screenshot using Selenium WebDriver and TestNG Listeners when any test fails. To do so we have to extendTestListenerAdapter class.


TestListenerAdapter class implements ITestListener interfaceSo whenever a test fails it invokes method onTestFailure(ITestResult tr) and that we will use to call our custom method to take screenshot.

package com.example.tests;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class ApplicationController {
protected WebDriver driver;
protected String baseUrl;
@BeforeClass
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://accounts.google.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//set driver object for ScreenshotController 
ScreenshotController.setBaseWebDriver(driver);
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}

---------------------------------------------------------------------------------------------------------------------------------------------


package com.example.tests;

import org.openqa.selenium.*;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(ScreenshotController.class)
public class LoginUITest extends ApplicationController {
@Test
public void first() throws Exception {
driver.get(baseUrl);
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("XXXXX");
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("XXXXX");
driver.findElement(By.id("signIn")).click();
driver.findElement(By.xpath("//a[contains(.,'Outbox')]"));
}

@Test
public void second() throws Exception {
driver.findElement(By.xpath("//a[contains(.,'Inbox')]"));
}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------

package com.example.tests;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;


public class ScreenshotController extends TestListenerAdapter {
private static  WebDriver driver;
public static void setBaseWebDriver(WebDriver driver) {
ScreenshotController.driver=driver;
}
//calls scrrenShot method only when test fails
@Override
public void onTestFailure(ITestResult tr) {
screenShot();
}
/**
* Capture Screenshot and copy it to "user.dir\Screenshots" folder as png.
* Screenshot naming convention: dd_MM_yyyy_hh_mm_ss_Screenshot.png
*/
private void screenShot() {
try {
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Date date = new Date();
DateFormat df = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
String screenshotDirectory = System.getProperty("user.dir")+"\\Screenshots\\"+df.format(date)+"_Screenshot.png";
FileUtils.copyFile(sourceFile, new File(screenshotDirectory));
}catch(Exception e){
System.out.println(e) ;
}
}
}

No comments:

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com