Automation Using Selenium Webdriver
Showing posts with label Screen Shot In Selenium. Show all posts
Showing posts with label Screen Shot In Selenium. Show all posts

Wednesday 28 September 2016

On test case failure take a screenshot with Selenium WebDriver

Take Screen Shot in Selenium

We have discussed about simple way of taking a screen shot earlier. Now in this , 
we will see how to take screen shot.


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.chrome.ChromeDriver;


public class Screenshoot {

public static WebDriver driver;
public static void main(String[] args) throws IOException
{
//Step1:Launch FirefoxDriver
        //Step2:Navigate to http://newtours.demoaut.com/
//Take screenshot
System.out.println("********Excution stating wait*********");
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("http://newtours.demoaut.com/");
//code for taking screenshot
File Srcfile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(Srcfile, new File("E:\\Jagan\\Screenshot.png"));
System.out.println("Screen shot taken");
        driver.close();
}


}Take Screen Shot in selenium


On test case failure take a screenshot with Selenium WebDriverOn 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 runfails.

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("E:\\jagan\\failure.png"));
   
  }
 }
}
And here is the screenshot of webpage on test failure

On test case failure take a screenshot with Selenium WebDriver