Here is the very basic program to take screenshot of the current page using Selenium Webdriver script.
Java Source code:
package com.helloselenium.selenium.test; 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; 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(); } }
Detailed explanation for the above program is as follows:
Following code are the required packages for Selenium Webdriver.
All required packages are available in downloaded selenium server and selenium java client jar file.
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;
WebDriver driver = new FirefoxDriver();
driver.get("http://helloselenium.blogspot.com");
driver.navigate().to("http://helloselenium.blogspot.com");
Following code is to take the screenshot of the current page. In the below code we are storing the taken screenshot as a file.
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C:\\Screenshots\\test.png"));
driver.quit();
driver.close();
2 Comments
scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
ReplyDeleteCould you explain this line in detail..What is this interface is doing..
Basically in the above line of code TakesScreenshot is used to capture a screenshot, similarly as PrintScreen command of OS and OutputType.FILE is used to set an output of captured screen. Basically we used output type as file because later on we have to save that output as a screenshot image file.
DeleteWhat would you like to add in my list? I look forward to reading your comments below.