Friday, 4 July 2014

How to take a screenshot in Selenium Webdriver

code will help you how to take a snapshot in selenium webdriver.

Also one thing noticed is that sometimes “import org.openqa.selenium. TakesScreenshot;” this import don’t appear and you have to manually add this line to your code to make this work. 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;
@BeforeMethod
public void launchChrome()
{
System.setProperty(“webdriver.chrome.driver”, “E:\Harsimran-PC\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe”);
driver = new ChromeDriver();
driver.get(“http://google.co.in”);
}
@Test
public void googleScreenshot()
{
try {
File scrnsht =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// This is where magic happens FileUtils.copyFile(scrnsht, new
File(“e:\google_page.png”));
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void kill()
{
driver.close();
}
}

No comments:

Post a Comment