My Thoughts: On test case failure take a screenshot with Selenium WebDriver

Thursday, March 6

On test case failure take a screenshot with Selenium WebDriver

Don't you think "A picture is worth a thousand words.." :)

It is always(at least in most of the cases :P) better to take screenshot of webpage when the test run fails.

Because with one look at the screenshot we can get an idea of where exactly the script got failed. Moreover reading screenshot is easier compare to reading 100's of console errors :P

Here is the sample code to take screenshot of webpage
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("PathOnLocalDrive")

To get screenshot on test failure , we should put the entire code in try-catch block . In the catch block make sure to copy the above screenshot code.

In my example I am trying to register as a new user. For both first and last name fields I have used correct locator element whereas for emailaddress field I have used wrong locator element i.e name("GmailAddress1").

So when I run the script , test failed and I got the screenshot with pre filled first and last names but not email address.

Here is the sample code :
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TakeScreenshot {
   WebDriver driver;
 public void start(){
  driver = new FirefoxDriver();
 public void Test() throws IOException{
  driver.findElement("FirstName")).sendKeys("First Name");
  driver.findElement("LastName")).sendKeys("Last Name");
 }catch(Exception e){
  //Takes the screenshot  when test fails
     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
     FileUtils.copyFile(scrFile, new File("C:\\Users\\Public\\Pictures\\failure.png"));
And here is the screenshot of webpage on test failure

Reference  :

Between have a great day ...!!! :)


  1. Madiraju Krishna ChaitanyaFriday, March 07, 2014

    Hi Vamsi Kurra,Thanks a LOT for sharing this concept with us.I am yet to work on this program,though.Please keep up the Good Work.May GOD BLESS You...

  2. Thank you for the post,
    other alternative is by creating the custom profile using WebDriver

    hear the sample code snippet for creating custom profile to download the files using selenium webdriver

  3. Hi Vamshi,

    I'm getting the Error while, Clicking on Pack Extension button.

    It shows the popup with

    " The 'manifest_version' key must be present and set to 2 (without quotes). See for details."

    Can you please help me out this issue ?

  4. Hi Thyagu pugaz,

    May I know for which extension you are getting this error. It worked fine for me .

  5. Its working fine .. Thanks.

  6. I'm trying to automate the "Rest Console " - its chrome extensions.

    ChromeOptions options = new ChromeOptions();
    options.addExtensions(new File("C:\\Users\\pugazd\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Extensions\\cokgbflfommojglbmbpenpphppikmonn\\4.0.2_0.crx"));
    // options.setBinary(new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"));
    System.setProperty("", "C:\\Users\\pugazd\\Desktop\\chromedriver.exe");
    ChromeDriver driver = new ChromeDriver(options); // Error thrown in this line.

    Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: failed to wait for extension background page to load: chrome-extension://omlijhidnmlobpccmlkhjeikgbbhebab/background.html

    from unknown error: page could not be found: chrome-extension://omlijhidnmlobpccmlkhjeikgbbhebab/background.html

    (Driver info: chromedriver=2.7.236900,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)

  7. thank you very much for your clear explanation..

  8. Please find the video tutorial of selenium WebDrive here with java:

  9. Hi vamshi,

    I have followed the same steps as you have specified.
    after i had copy-pasted the selenium code in eclipse, am unable to import the file which you are using.

    Thats the reasson unable to specify the statement

    String s = new OCR().recognizeCharacters((RenderedImage) image);

    Can u please give me a solution to this.

  10. Hi Mamatha,

    You can't import the file . Please download the AspriseOCR.dll file and keep it at the below location :

    "C:\Windows\System32" .