Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

How to take screen shot on both test cases "Positive and Negative" results.

NOTE: Currently, iam working in selenium webdriver (junit) and i didnt have an idea about how to code for adding methods to take screen shots.

Thanks in Advance.

share|improve this question

4 Answers 4

Take the screenshot in the @After test tear down method, which is run after every test. This way you will always get a screenshot for both passed and failed tests.

public class TestSample
    static WebDriver driver;

    public void setUp() {
        //start new webdriver session, for eg using firefox
        driver = new FirefoxDriver();

    public void aTest() {
        //more test logic  - test might pass or fail at this point

    public void tearDown() {
        // take the screenshot at the end of every test
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // now save the screenshto to a file some place
        FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

    //quit WebDriver session
share|improve this answer

I am not sure how to do it manually, but there's a testing framework (thucydides) that can do what you need:


This tool in fact integrates Selenium Webdriver with other tools to make your life easier.

You can setup a test maven project quite fast. Since it is open-source, you could check how they capture the screenshots if you don't want to mess with another tool. However, I must say that working with such a tool makes life much easier...

share|improve this answer
Martin, it would help if you explicitly stated that Thucydides extends Webdriver. Someone who has never heard of that particular tool could think your response wasn't relevant to the question. –  Kate Paulk Nov 13 '13 at 18:20
Thanks for the comment, will edit the post... :) –  Martin Nov 14 '13 at 9:20

How to take a screenshot in selenium webdriver 2 in MAC O/S

Need to import->

package generic;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

  public void browserValidate() throws IOException, InterruptedException {

      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File("/Users/mhossain/Desktop/Mixed/screenshot.png"));

share|improve this answer

I have written a blog on how to take screen shots while using webdriver APIs, This is a basic code but it will help you in taking screenshots.


share|improve this answer
The fact that you make it clear that this is your post (and not just random spam) is a positive sign, and is appreciated. One note I'd make is that we tend to discourage link-only answers. Could you make this answer stand on its own, with a few code samples and small explanation, retaining the link back to your blog for further reading? Thanks! –  corsiKa 13 hours ago

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.