Friday, September 13, 2019

Post#35.Capturing Screenshot in Selenium Part-1 (Capture Web page Screenshot)


Take Screenshot in Selenium

Test cases may fail while executing the test suite in selenium automation. When manual tester face any discrepancy in the verification i.e. when there is a difference in actual and expected values, manual testers tend to take screenshot as proof for failure.
We can take screenshot of a webpage in selenium using getScreenshotAs() method, Webdriver interface doesn't provide a method to take a screenshot.

There is an interface called TakesScreenshot which provides getScreenshotAs method and which selenium uses to take a screenshot.


Our browser classes (Like ChromeDriver, FirefoxDriver ...) extends RemoteWebdriver and RemoteWedriver implements TakesScreenshot Interface along with Webdriver Interface.

So we have screenshot method implementations in our driver classes. This process is called Multiple Inheritance in java i.e Class(browser) implementing two Interfaces(Webdriver and TakesScreenshot).

We can Take two kind of screenshots:
  1. Webpage Screenshot
  2. WebElement Screenshot

Syntax : getScreenshotAs(OutputType target)

We can store the captured screenshot in different formats, all the formats are present in OutputTypeinterface.

Formats available in OutputType Interface:
1.      OutputType.BASE64
2.      OutputType.BYTES
3.      OutputType.FILE
We can convert all the above output types into png, jpg etc  type.

Take Webpage Screenshot

We can take screen shot of a webpage using getScreenshotAs method from the TakesScreenshot, but we cannot initialize TakesScreenshot as it is an interface

So to take a screenshot of the page we have to cast our driver object to TakesScreenshot interface type by adding below code.
((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
In the above, code we are using getScreenShotAs method after casting the driver object to TakesScreenshot interface type.
Selenium WebDriver tries to capture the screen in below sequence, if first fails, webdriver tries to take second and so on..
  • Tries to take a screenshot of Entire page
  • Tries to screenshot of the current window
  • The Visible portion of the current frame
  • The screenshot of the entire display containing the browser
Program to take a screenshot in selenium webdriver with java

import java.io.File;
import java.util.concurrent.TimeUnit;
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 PageScreenShot {
              public static void main(String[] args) throws Exception {
// set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/PATH/geckodriver.exe");
WebDriver driver =new FirefoxDriver();
driver.manage().window().maximize();
// set time limit to find the element
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
 // Go to URL
// Take a ScreenShot
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\pageScreenshot.png"), true);

 // Close Driver
 driver.quit();
              }
}

No comments:

Post a Comment