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 java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
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;
 
 @BeforeTest
 public void start(){
  driver = new FirefoxDriver();
 }
 
 @Test
 public void Test() throws IOException{
 try{
  driver.get("https://mail.google.com/");
  driver.findElement(By.id("link-signup")).click();
  driver.findElement(By.name("FirstName")).sendKeys("First Name");
  driver.findElement(By.name("LastName")).sendKeys("Last Name");
  driver.findElement(By.name("GmailAddress1")).sendKeys("GmailAddress@gmail.com");
  
 }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  :
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#taking-a-screenshot
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/TakesScreenshot.html

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


12 comments:

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

    ReplyDelete
  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
    http://qaautomationworld.blogspot.in/2014/02/file-downlaoding-using-selenium.html

    ReplyDelete
  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 developer.chrome.com/extensions/manifestVersion.html for details."

    Can you please help me out this issue ?

    ReplyDelete
  4. Hi Thyagu pugaz,



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

    ReplyDelete
  5. Its working fine .. Thanks.

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

    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    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("webdriver.chrome.driver", "C:\\Users\\pugazd\\Desktop\\chromedriver.exe");
    ChromeDriver driver = new ChromeDriver(options); // Error thrown in this line.
    driver.get("chrome-extension://cokgbflfommojglbmbpenpphppikmonn/index.html");



    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)

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

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

    http://msrqatec.blogspot.in/p/selenium-with-java-tutorial.html

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

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

    ReplyDelete