7

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

NOTE: Currently, I am working in selenium webdriver (junit) and I did not have an idea about how to code for adding methods to take screen shots.

7 Answers 7

9

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();
    }
}
4

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.

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();
        }
    }
}
3
  • 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
    Commented Oct 24, 2014 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. Commented Oct 25, 2014 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
    Commented Oct 26, 2014 at 2:50
2

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"));

  }
2

MultiScreenShot

I found following useful link in Github for Multiscreenshot

1
  • Could you please explain further as to what the tool is and how it could solve this need? Thanks!
    – Paul Muir
    Commented Dec 16, 2014 at 13:45
2

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. Example as requested:

WebDriver driver = new FirefoxDriver();   
driver.get("http://www.google.com/"); 
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
FileUtils.copyFile(scrFile, new File("c:\saving location\filename.png"))`;

Hope this clears your problem.

3
  • Hi Atlaf, could you write a small code snippet? Is it as simple as your answer makes it seem? I mean, maybe it is. But some code would be useful. Also, what do you mean by "this has to be written manually"?
    – corsiKa
    Commented Nov 5, 2014 at 0:23
  • Dear corsika answer helpful to you is posted below.
    – Altaf
    Commented Nov 5, 2014 at 3:47
  • Altaf interesting, it doesn't use the captureEntireScreenshot you mentioned in your answer. Also know that you can edit your answer instead of posting a new one. I recommend you pick one to edit the code into and delete the other.
    – corsiKa
    Commented Nov 5, 2014 at 4:30
2

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...

1
  • 7
    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
    Commented Nov 13, 2013 at 18:20
1

Since it hasn't been mentioned so far: There is Shutterbug.

Selenium Shutterbug is a utility library written in Java for making screenshots using Selenium WebDriver and further customizing, comparing and processing them with the help of Java AWT.

Screenshot of an entire page:

Shutterbug.shootPage(driver, ScrollStrategy.WHOLE_PAGE)
        .save("/path/to/screenshots/");

Get the image for a single element:

Shutterbug.shootElement( driver, element )
         .getImage()

You can also add highlighting, titles, text, etc.:

Shutterbug.shootPage(driver)
        .blur(searchBox)
        .highlight(searchBtn)
        .monochrome(googleLogo)
        .highlightWithText(googleLogo, Color.blue, 3, "Monochromed logo", Color.blue, new Font("SansSerif", Font.BOLD, 20))
        .highlightWithText(searchBox, "Blurred secret words")
        .withTitle("Google home page - " + new Date())
        .withName("home_page")
        .withThumbnail(0.7)
        .save("/path/to/screenshots/");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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