Tuesday, 5 February 2013

Capture the Screen on Test failure - WebDriver


Screen captures of the browser session helps you to show error conditions.

#JAVA

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\sample.jpeg"),true);

#Python

#!/usr/bin/env python
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()


Example Test
[Note:- "Scripts in Orange color are mandatory"]

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
  
  public class classname{
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://in.yahoo.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testWww() throws Exception {
    driver.get(baseUrl + "/?p=us");
    driver.findElement(By.id("p_13838465-p")).clear();
    driver.findElement(By.id("p_13838465-p")).sendKeys("screen");
    driver.findElement(By.id("search-submit")).click();
    Thread.sleep(2000);
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File("c:\\sample.jpeg"),true);
    Thread.sleep(2000);
  }

@After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
}


For Selenium RC,
Use the command: captureEntirePageScreenshotAndWait
Target: C:\\$(image).jpg

This cmd won't work for WebDriver

Example with 'captureScreenshot()'

Selenium selenium;
selenium = new DefaultSelenium(seleniumServer, 4444,
    "*firefox", "http://www.google.com");
selenium.start();
selenium.open("/");
selenium.captureScreenshot("C:\screenshot.png");


Examples with 'captureEntirePageScreenshot()'


Selenium selenium;
selenium = new DefaultSelenium(seleniumServer, 4444,
    "*firefox", "http://www.google.com");
selenium.start();
selenium.open("/");
selenium.captureEntirePageScreenshot("C:\screenshot.png","");

13 comments:

  1. Tamil:
    I am getting a error message for the following script line. Can any one help me out to verify it?
    FileUtils.copyFile(scrFile, new File("c:\\sample.jpeg"),true);

    ReplyDelete
    Replies
    1. Tamil, can I have your script ?
      email to seleniumworks@gmail.com

      Cheers!!
      _
      Sams

      Delete
  2. Is this to capture screenshot or for screenshot on failure

    ReplyDelete
    Replies
    1. Make use of EventFiringWebDriver for capturing screenshot on failure
      I have a post on that too.. please check http://seleniumworks.blogspot.in/2014/02/eventfiringwebdriver.html

      Delete
  3. By writing only this command
    selenium.captureEntirePageScreenshot("C:\screenshot.png","");
    screenshot functionality is not working..pls help

    ReplyDelete
  4. Hi,

    Can anyone help me how to take multiple screenshots.

    -> How to take screenshots only for PASSED /FAILED test case

    Your answer would help me a lot.

    Explain me with some simple example.

    Thanks in advance

    ReplyDelete

  5. Hi Guys,
    Please find the link to jar file by which we can take Taking Multiple screenshots and element Screenshots and minimize browser in Webdriver 2.0

    https://github.com/MultiScreenShot/MultiScreenShot

      Regards,
      Pavankumar Nagaraj
      +91 8971673487
      pavankumar.nagaraj@gmail.com
      in.linkedin.com/in/PavankumarNagaraj/
      Bangalore.

    ReplyDelete

  6. I am using selenium to automate testing in IE using C#. When the case fails I take the screenshot. But the screenshot does not capture the pop up.

    ReplyDelete
  7. Thanks for sharing the useful information with us. This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.

    Thanks
    Pratical oritented Selenium Training

    ReplyDelete