Tuesday, 26 August 2014

How to capture Screenshot of Web Page in Selenium Webdriver

We know that capturing a screenshot is very important to a tester for validations ,As per the Automation testing in Selenium the java code required to capture a screenshot, To capture it the piece of code is given below.


package Spicejet;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;

public class FirstApplication{
private WebDriver driver;
File screenshot;

public void SpiceConnect() throws Exception {

                 driver = new FirefoxDriver();

// get the base url

driver.get("http://www.spicejet.com/");
             
                 //To maximize a window
                driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

//click on the link spice connect

driver.findElement(By.linkText("SpiceConnect")).click();

/// user registration login page screen shot

try {


screenshot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(screenshot, new File("C:\\Users\\MY                                          PC\\Desktop\\Screenshots\\userregistration.png"));


}catch(Exception e){

System.out.println("Screen shot for registration page" + e);

}

driver.close();

}



To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

No comments:

Post a Comment