|

How and Why do we need to take screenshot with Selenium Webdriver?

To reduce the manual effort to perform regression testing, we execute test scenarios on the application under test. The test flow needs to be analyzed to identify where the regression suite failed and why it failed. It would be great if the Selenium tests captured the screenshot at the time of execution so that you could see the state of the application. We can save time by re-testing every time a script fails because of automation testing.

At run time, we can use the test script to take a screenshot of the application at the time of failure. In this tutorial, we will discuss the importance of screenshots and illustrate how you can use Selenium to take a screenshot of a webpage.

Why do you need to take a screenshot in Automation testing?

As we already know, one of the primary purposes of automation testing is to reduce manual effort. Therefore, the use of a screenshot captured during automated test runs becomes very beneficial. You would rather not keep checking on your application every time the tests are run. When the test execution is complete, the script can take a Screenshot to check the application’s state. When your test case fails, screenshots can help you identify what went wrong in your script or application.

Screenshots are especially helpful when testing headless applications, where you cannot see the GUI of the application. Following are some states where you can use Selenium WebDriver to take a screenshot and store it in a file so that you can verify the application later:

keepersecurity.com
keepersecurity.com
  • When problems happen with the application
  • When an assertion fails.
  • Also, when it’s hard to find a web element on a page.
  • There is a time-out in finding a web element.

How to take a screenshot in Selenium?

The Takes Screenshot interface enables the Selenium WebDriver to capture a screenshot and store it in different ways. It contains a method, ‘getScreenshotAs()’, which captures the screenshot and stores it in the specified location.

Here’s the basic syntax for taking a screenshot with Selenium WebDriver.:

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

In the above code, we use WebDriver to make a TakeScreenshot. And call getScreenshotAs() method to create an image file by providing the parameter *OutputType.FILE.

Now we can use this file object to copy the image to our desired location using the FileUtils Class.

GotPrint
GotPrint
FileUtils.copyFile(screenshot, new File("C:\\Users\\Jayant\\Pictures\\qatraininimages\\chromedriver_win32\\homePageScreenshot.png"));

Action Tasks

To take a snapshot in Selenium, please follow the steps below:

1. Initialize the web driver

2. Open www.itlearn360.com

3. Call getScreenshotAs method to take a snapshot.

4. Store captured snapshot to local storage.

5. Close the driver.

Edureka - Month End Offer-Flat 30% OFF ON Live Course
Edureka - Month End Offer-Flat 30% OFF ON Live Course

Webdriver Script

package testing.org;
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.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class TakeScreenshots {
    public static void main(String[] args) {
        //set the location of chrome browser
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Jayant\\Pictures\\qatraininimages\\chromedriver_win32\\chromedriver.exe");
        
        // Initialize browser
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        //navigate to url
        driver.get("https://www.itlearn360.com");
        
       //Take the screenshot
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        
        //Copy the file to a location and use try catch block to handle exception
        try {
            FileUtils.copyFile(screenshot, new File("C:\\Users\\Jayant\\Pictures\\qatraininimages\\chromedriver_win32\\homePageScreenshot.png"));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        
        //closing the webdriver
        driver.close();
    }
}

The file will be generated in the location specified by the above program.

This is the way to take a snapshot with Selenium Webdriver.

Instructor-led Training

https://www.qaonlinetraining.com/programs/master-of-functional-automation-testing/

Similar Posts