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

5 Answers 5

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;

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

    @Test
    public void aTest() {
        driver.get("http://www.google.com/");
        //more test logic  - test might pass or fail at this point
    }

    @After
    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
        driver.quit();
    }
}
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:

http://www.thucydides.info/

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
4  
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
1  
Thanks for the comment, will edit the post... :) –  Martin Nov 14 '13 at 9:20

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.

http://apttest.in/how-to-capture-screenshot-in-webdriver-using-java/

package screenshot_package;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class screenshot {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.get("http://apttest.in");

        try{
            //take screenshot and save it in a file
            File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

            //copy the file to the required path
            FileUtils.copyFile(screenshot,new File("C:\\Users\\udit\\Desktop\\screenshot.jpeg"));

        }catch(Exception e){
            //if it fails to take screenshot then this block will execute
            System.out.println("Failure to take screenshot "+e);

        }
        finally
        {
            //Whatever happens with the screenshot, driver will quit.
            driver.quit();
        }
    }
}
share|improve this answer
1  
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 Oct 24 at 14:48
    
Thanks for the comment. I have edited the code. Please let me know if i can make it more clear and precise. –  udit choudhary Oct 25 at 9:34
    
That's a lot better. This is an answer that stands on its own, and then links to the blog for further information. As a side note, I fixed the formatting (code blocks need to have 4 spaces in front of them, go look at the source (edit) to see how I did it) so it looks a little cleaner. But that's great, thanks a bunch! –  corsiKa Oct 26 at 2:50

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;

@Test
  public void browserValidate() throws IOException, InterruptedException {

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

  }
share|improve this answer

The command should be: captureEntireScreenhot which is available in commands and the target should be the address of the folder where the screenshot is to be saved.

This has to be written manually.

share|improve this answer

Your Answer

 
discard

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.