Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium 2 and JUnit4: How to capture screenshot on exception?

I want to capture a screenshot only upon unexpected exception.

like image 225
Alberto Avatar asked Jun 29 '11 12:06

Alberto


People also ask

How do you take a screenshot on assert failure?

assertEquals(result, true) will take a screen shot in case the assertion fails because we enabled our listener. class to it.

Is it possible to take screenshot of a particular element in Selenium?

Step 1) Create an Ashot object and call takeScreenshot() method if you just want the screenshot for the screen size page. Screenshot screenshot = new Ashot(). takeScreenshot(driver);

What method will help you to take screenshot in Selenium?

To take a screenshot in Selenium, we use an interface called TakesScreenshot, which enables the Selenium WebDriver to capture a screenshot and store it in different ways. It has a got a method "getScreenshotAs() " which captures the screenshot and store it in the specified location.


1 Answers

Note.- This answer could be outdated. The answer is based on Selenium 2.15

Using TestWatcher does the trick (the unit test must extend following BaseTest):

public abstract class BaseTest {
// ...
protected WebDriver driver;


@Rule
public TestRule testWatcher = new TestWatcher() {
    @Override
    public void starting(Description desc) {
        LOG.info("Launching browser...");
        driver = Utils.getFirefoxDriver();
    }

    @Override
    public void finished(Description desc) {
        LOG.info("Quitting driver...");
        driver.quit();
    }

    @Override
    public void failed(Throwable e, Description d) {
        LOG.debug("Creating screenshot...");
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(
                OutputType.FILE);
        String scrFilename = "Screenshot.png";
        File outputFile = new File(SCREEN_SHOTS_RESULTS_PATH, scrFilename);
        LOG.info(scrFilename + " screenshot created.");
        try {
            org.​apache.​commons.​io.FileUtils.copyFile(scrFile, outputFile);
        } catch (IOException ioe) {
            LOG.error("Error copying screenshot after exception.", ioe);
        }
    }
};
}

Note

Utils.getFirefoxDriver() returns a customized WebDriver. Something like:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class Utils {
    // ...
    public static WebDriver getFirefoxDriver() {
        FirefoxProfile firefoxProfile = new FirefoxProfile();

        // Profile customization. For example:
        // firefoxProfile.addExtension("firebug-1.8.4-fx.xpi");
        // firefoxProfile.setPreference("extensions.firebug.currentVersion","1.8.4");

        FirefoxBinary firefox = new FirefoxBinary();

        // Firefox customization. For example:
        // firefox.setEnvironmentProperty("DISPLAY", display);

        WebDriver driver = new FirefoxDriver(firefox, firefoxProfile);

        // WebDriver customizations. For example:
        // driver.manage().timeouts().implicitlyWait(SHORT_TIMEOUT_S, TimeUnit.SECONDS);
        return driver;
    }
}
like image 119
Alberto Avatar answered Oct 24 '22 04:10

Alberto