- 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;
- public class takeScreenshot{
- public static void main(String[] args) {
- WebDriver driver = new FirefoxDriver();
- driver.get("http://helloselenium.blogspot.com");
- File scrFile;
- scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
- try {
- FileUtils.copyFile(scrFile, new File("C:\\Screenshots\\test.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- driver.quit();
- }
- }
Following code is to initialize the Firefox driver.
Following code is to open the hello selenium blog in browser.
You can also use the following code is to open the hello selenium blog in browser.
Following code is to take the screenshot of the current page. In the below code we are storing the taken screenshot as a file.
Following code is to save the taken screenshot on a location. "C:\\Screenshots\\" is the path of location and "test.png" is the filename to save the taken screenshot as a file. This code needs exception handling which can be handled using throws or try catch.
Following code is to quit the Firefox driver.
You can also use the following code is to close the Firefox driver.
- WebDriver driver = new FirefoxDriver();
- driver.get("http://helloselenium.blogspot.com");
- driver.navigate().to("http://helloselenium.blogspot.com");
- scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
- FileUtils.copyFile(scrFile, new File("C:\\Screenshots\\test.png"));
- driver.quit();
- driver.close();
No comments:
Post a Comment