Skip to main content

Take Screenshot in Selenium WebDriver

When we execute huge number of test scripts, and if some test fails, we need to check why the test has failed. So, its very important to take screenshot when we execute a test script.

Advantage of  taking screenshot in Selenium test script :-
  1. It helps us to debug and identify the problem by seeing the screen shot.
  2. Save Time 
  3. We can attach same image in Extent report which actually make my reporting better.
Selenium has provided TakesScreenShot interface in this interface you can use getScreenshotAs method which will capture the entire screenshot in form of file then using FileUtils we can copy screenshots from one location to another location.

Capture Screenshot in Selenium Webdriver :

1.File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
2.String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);


getScreenshotAs() 

This method is used to capture the screenshot and store it in the specified location. For WebDriver extending TakesScreenshot, this makes a best effort depending on the browser to return the following in order of preference:
  • Entire page
  • Current window
  • Visible portion of the current frame
  • The screenshot of the entire display containing the browser

OutputType 

getScreenshotAs() method takes OutputType.FILE  as parameter. OutputType is an interface in selenium webdriver API. OutputType  defines the output type for a screenshot. We can store screenshot in three form 

1. BASE64 - Obtain the screenshot as base64 data.
Ex :-

String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);

2. BYTES - Obtain the screenshot as raw bytes.
Ex.

3. FILE  - Obtain the screenshot into a temporary file that will be deleted once the JVM exits.
Ex :-

File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

The below example explains how to take the screen shot when the test fails.

 @Test
 public void TakeScreenshot()
{
// Open Firefox
 WebDriver driver=new FirefoxDriver();
// Maximize the window
driver.manage().window().maximize();
// Pass the url
driver.get("http://www.google.com");
// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
 // now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/Screenshot1.png"));
}
catch (IOException e)
 {
  System.out.println(e.getMessage());
 }
 }

Comments

Popular posts from this blog

Appium architecture (How Appium works internally)

Appium Architecture  A ppium is a cross-platform automation tool, API of it supports both OS (Android and iOS) test scripts.It is tested on simulators (iOS,), emulators (Android), and real devices (iOS, Android)   Appium is an HTTP server written in Node.js that creates and handles WebDriver sessions.The Appium web server follows the same approach as the Selenium WebDriver, which receives HTTP requests from client libraries through JSON and then handles those requests in different ways   JSON wire protocol The JSON wire protocol  ( JSONWP ) is a transport mechanism created by WebDriver developers. This wire protocol is a specific set of predefined, standardized endpoints exposed via a RESTful API.   Appium implements the Mobile JSONWP, the extension to the Selenium JSONWP, and it controls the different mobile device behaviors, such as installing/uninstalling apps over the session Let’s have a look at some of the endpoints from the API whi...

Handle radio button & Check Boxes in Selenium

The main difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options. Selenium WebDriver supports Radio Button and Radio Group controls using the WebElement class. We can select and deselect the radio buttons using the click() method of the WebElement class and check whether a radio button is selected or deselected using the isSelected() method. Before performing the click on the Radio buttons or check boxes we will have to verify follwing scenarios :- If Radio button or Checkbox is displayed on the webpage If Radio button or Checkbox is enabled on the webpage Check the default selection of the Radio button or Checkbox We use predefined methods present in selenium to handle check Box and Radio  button :- isDisplayed() isEnabled() isSelected() 1.  isDisplayed () this method returns a Boolean value, i...

Read data from Properties file in Selenium

Java properties file is used to store project configuration data or settings. In this blog, we will show you how to read and write to/from a properties file. In this blog we are going to read username and password from properties file(.config)  Step 1 :- Create config.properties file in Java project  Navigate to File menu -> New -> Click on File Step 2:- Write some data in config.properties file Step 3 :- Create Java class to read properties file  Example how preform login using properties file in Seleniuim  : import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class ReadProper  {   public static Properties prop;   public static void mai...