How to Take a Screenshot in Selenium WebDriver Using Java?
Selenium WebDriver is a collection of open-source APIs which are used to automate the testing of a web application. In order to capture a screenshot in Selenium, one has to utilize the method Takes Screenshot. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used to automate web application testing to verify that it works as expected. It supports many browsers. Here we will be taking the example of chrome.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
File file = ((TakesScreenshot) driver) ;
Here, we will learn how to take a screenshot in the selenium web driver and highlight the element using Java Binding.
Screenshots are required for bug analysis especially in the case of test case failure. Whenever a test case fails, we need some attachment to verify that failure. Selenium can automatically take a screenshot during execution, and we can also mark a border to highlight that element.
Steps to take a screenshot in selenium web driver:
1. Take a screenshot and store it in a file format
2. Copy screenshot to a location using CopyFile method
FileUtils.copyFile(File, new File(“location where you want to save the image” +FileName+ “.jpeg”));
Refer to the complete code below for the implementation part