Taking Screen Shot using Selenium Webdriver.
Sample example to help all in taking screen shot using selenium webdriver. It has been quit sometime i stopped posting update in my blog.
So, Found this way to update this blog.Few more to come.
import java.io.File;
import java.io.IOException;
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.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.reporters.JUnitReportReporter;
/**
* Sample Test Case to show case how screen shot can be captured and stored with name of
* Test method. This would be useful when multiple test method are present in single java file
*/
@Listeners(SeleniumRunnerExample.class)
@Test(groups={"Sample1"})
public class SeleniumRunnerExample extends JUnitReportReporter
{
WebDriver driver = null;
ITestNGMethod method;
@BeforeSuite
public void driverInstantiationTest(){
ProfilesIni allProfiles = new ProfilesIni();
// Creating custom profile of Firefox and referring to same.
FirefoxProfile profile = allProfiles.getProfile("UIAutomation");
//UIAutomation refer to profile created by user.This custom profile will be invoked by
//Webdriver for performing required activity on browser.
profile.setAcceptUntrustedCertificates(false);
// Setting whether to accept untrusted Certificate or Not.
driver = new FirefoxDriver(profile);
// New instance of Firefox driver with custom profile
driver.get("https://www.example.com");
// Opening https://www.example.com. Here https://www.example.com is just for example
// Purpose
}
@Parameters({"url"}) //Parameter that is retrieved from "testng-suite.xml"
@Test(description="Clear description of what this test method is doing should be updated here")
public void exampleTest(String url){
/**
* Sample implementation part should be written here.
*
* */
}
//takeScreenShotTest is having @ AfterMethod annotation. Since i want to take screen shot after //each test case has been Run. So as to know where it terminated at last.
@AfterMethod(description="Takes screen shot")
public void takeScreenShotTest(ITestResult result) throws IOException{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\\test-output\\screenshot"+result.getMethod().getMethodName()+".png"));
}
@AfterSuite
public void closeBrowser(){
driver.quit();
}
}