Capture Screenshots in Selenium


Capturing Screenshot

Capture screenshots in SELENIUM using getScreenshotAs() method of TakesScreenshot interface.

Steps to take screenshot:
1. Create an object of specific browser related class (e.g. FirefoxDriver) and then upcast it
to WebDriver object (Example: WebDriver driver=new FirefoxDriver).
2. Typecast the same upcasted driver object to TakesScreenshot interface type TakesScreenshot ts = (TakesScreenshot) driver.

3. Using the type casted object, we call getScreenshotAs (OutputType.FILE) which in turn
returns the source file object.
File srcFile = ts.getScreenshotAs(OutputType.FILE);


4. Using the File IO operations (i.e. FileUtils class), we store the screenshots to desired
location in the project.
Files.copy(srcFile, “F:/screenshot/google.png”);

Example:

package snapshot;
import org.openqa.SELENIUM.WebDriver;
import org.openqa.SELENIUM.chrome.ChromeDriver;
import org.openqa.SELENIUM.firefox.FirefoxDriver;
public class SuperClass
{
static {
System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
System.setProperty("webdriver.ie.driver", "./drivers/IEDriverServer.exe");
}
//public static WebDriver driver = new FirefoxDriver();
public static WebDriver driver = new ChromeDriver();
}
package testpackage;
import java.io.File;
import org.openqa.SELENIUM.OutputType;
import org.openqa.SELENIUM.TakesScreenshot;
import org.openqa.SELENIUM.WebDriver;
import org.openqa.SELENIUM.chrome.ChromeDriver;
import com.google.common.io.Files;
public class Screenshots extends SuperClass {
public static void main(String[] args) throws Exception {
driver.get("https://www.Google.co.in");
TakesScreenshot ts = (TakesScreenshot) driver;
File srcFile = ts.getScreenshotAs(OutputType.FILE);
File destFile = new File("F:/ screenshot / Google.png");
Files.copy(srcFile, destFile);
driver.close();
}
}

Explanation
We take the screenshot of a page by using  getScreenshotAs() methodof TakesScreenshot interface. It is a non- static method so we need a reference of TakesScreenshot interface to call it. There is no such method which returns an instance of TakesScreenshot interface, so we typecast our driver object to TakesScreenshot interface and using this object reference “ts” wecall getScreenshotAs() method. As an argument to this method we pass (OutputType.File) to which format of file we want the screenshot type i.e. File type. So, we specify the output type as File.

File srcFile = ts.getScreenshotAs(OutputType.FILE);
This line of code will store the screenshot in some temporary files or folder location, which is
not a recommended place to store the file. Whenever the system becomes slow we tend to
delete all temporary files and folders and if we store the screenshot in temporary location, we may lose the screenshot, we don’t take screenshot to lose it so we want to store the screenshot in some desired location based on our choice. We do this by creating an object of File class and as an argument to the constructor we pass the location (path) where we want to store the screenshot.

File destFile = new File("F:/SCREENSHOT/Google.png");

This line of code will create a blank file; it will not have the screenshot of the particular page.
We copy the screenshot from temporary location and paste in our desired location. We do this by using the following line of code.

Files.copy(srcFile, destFile);

Files is a class from java, it has a static method ‘copy’ as an argument to this copy method we
pass two file objects( srcFile, destFile). This method will copy the content of the screenshot
from the temporary location(srcFile) and paste in the desired location(destFile). This is how we take the screenshot of a page



Post a Comment

0 Comments