Capturing screenshots in selenium Webdriver
When we run our Selenium test on server sometime our test case failed.Debugging of each and every test cases is very challenging.If we take screenshots of everytest case it helps us to complete our task.
And sometime we have a requirement that you have to take screenshot of every Test case.Selenium provides an interface which helps us to take screenshots of Everytestcase.
I am writing that two lines of code which helps you to take screenshot in Selenium Webdriver
// take the screenshot at the end of every test
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//WebDriver provides an interface TakesScreenshots for capturing the screen shots of every testcase/failed Testcase.
//getScreenshotAs is a method to capture screenshots and saves into temporary location
FileUtils.copyFile(scrFile, new File("c:\\screenshot.png"));
Here is the code of taking sceenshots of failed testcase
package test;
import java.io.File;
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.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Chrome {
WebDriver driver;
@BeforeTest
public void openBrowser()
{
driver = new FirefoxDriver();
driver.get("http://facebook.com");
}
@Test
public void getScreenshot()
{
try {
File scrFile =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new
File("e:\\facebook_page.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void closeBrowser()
{
driver.close();
}
}












































