Tuesday, July 24, 2018

Capture Screenshot when Test Failed

Capture screenshot if the test case failed is an essential feature for investigating the defects. Selenium WebDriver implemented TakesScreenshot interface which is easy to take a screenshot from web browser screen. Here is an example:

public class Screenshot {

    public static void takeScreenshot(String screenshotPath, WebDriver driver) {
        try {
            File screenshotFile =
                  ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshotFile, new File(screenshotPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void takeScreenshot(WebDriver driver, String method) {
        String screenshotName = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss_")
                   + method + ".jpg";
        String dateString = DateUtils.formatDate(new Date(), "yyyyMMdd");
        File dir = new File("screenshots/" + dateString);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String screenshotPath = dir.getAbsolutePath() + "/" + screenshotName;
        takeScreenshot(screenshotPath, driver);
    }

}

JUnit Test Case:

@RunWith(SimpleRunner.class)
public class ScreenshotTest {

    public static WebDriver driver;

    @BeforeClass
    public static void setUpBeforeClass() {
        driver = new ChromeDriver();
    }

    @AfterClass
    public static void tearDownAfterClass() {
        driver.quit();
    }

    @Test
    public void testPercentageCalculation() {
        // open http://www.percentagecalculator.co/ web site
        driver.get("http://www.percentagecalculator.co/");
        // find A element and type 10
        driver.findElement(By.id("A")).sendKeys("10");
        // find B element and type 100
        driver.findElement(By.id("B")).sendKeys("100");
        // find C element
        WebElement c = driver.findElement(By.id("C"));
        // validate result
        assertEquals("11", c.getAttribute("value"));
    }

}

JUnit Runner:

public class SimpleRunner extends BlockJUnit4ClassRunner {

    public SimpleRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }

    @Override
    public void run(RunNotifier notifier) {
        notifier.addListener(new JUnitExecutionListener());
        notifier.fireTestRunStarted(getDescription());
        super.run(notifier);
    }

}

RunListener:

public class JUnitExecutionListener extends RunListener {

    public void testRunStarted(Description description) throws Exception {
    }

    public void testRunFinished(Result result) throws Exception {
    }

    public void testStarted(Description description) throws Exception {
    }

    public void testFinished(Description description) throws Exception {
    }

    public void testFailure(Failure failure) throws Exception {
        Screenshot.takeScreenshot(ScreenshotTest.driver,
            failure.getDescription().getMethodName());
    }

    public void testAssumptionFailure(Failure failure) {
        Screenshot.takeScreenshot(ScreenshotTest.driver,
            failure.getDescription().getMethodName());
    }

    public void testIgnored(Description description) throws Exception {
    }

}

No comments:

Post a Comment