TakesScreenShot interface in the WebDriver library is implemented by all of the different variants of WebDriver, such as Firefox Driver, Internet Explorer Driver, Chrome Driver, and so on.
TakesScreenShot capability is enabled in all of the browsers by default.
The API syntax for
getScreenshotAs() is as follows:
public <X> X getScreenshotAs(OutputType<X> target)
OutputType is another interface of the WebDriver lib. We can ask WebDriver to give your screenshot in three different formats; they are:
BYTES (raw data),
If you choose the
FILE format, it writes the data into a
.png file, which will be deleted once the JVM is killed. So, you should always copy that file into a safe location so that it can be used for later reference.
The return type is a specific output that depends on the selected
OutputType.BYTES will return a byte array,
OutputType.FILE will return a file object.
Depending on the browser used, the output screenshot will be one of the following in the order of preference:
- The entire page
- The current window
- A visible portion of the current frame
- The screenshot of the entire display containing the browser
- using Firefox Driver,
getScreenshotAs()takes the screenshot of the entire page,
- using Chrome Driver,
getScreenshotAs()returns only the visible portion of the current frame.
File scrFile = ((TakesScreenShot)driver).getScreenshotAs(OutputType.FILE);
getScreenshotAs() method to take the screenshot of the webpage and save it to a file format.
getAbsolutePath() method returns the path of the saved image, which you can open and examine.