- What is BluePrism? Know about the benefits of BluePrism Certification
- What is Silk test Workbench? : Comprehensive Guide
- RPA Developer Salary
- What is Selenium and Why Should You Learn Selenium | Everything You Need to Know
- Robotic Process Automation with Blue Prism | A Definitive Guide with Best Practices [ OverView ]
- An Overview of Patch Workflows | Benefits and Special Features [ For Freshers and Experience ]
- Selenium Commands : Selenese | Everything You Need to Know [ OverView ]
- Robotic Process Automation Developer Salary [ For Freshers and Experience ]
- List of Basic Powershell Commands | Every Developer Should Know [ OverView ]
- Tosca vs Selenium | Differences and Which Should You Learn?
- Chef vs Puppet: Major Differences and Similarities
- what is TestNG Annotations in Selenium Webdriver?
- Blue Prism Certification Guide
- RPA Tools Comparison
- Ansible vs Chef
- How to Download & Install Selenium Ide?
- RPA vs Selenium
- RPA Blue Prism Certification Guide
- Robotic Process Automation Career Scope
- How to Take A Screenshot in Selenium WebDriver?
- Robot Class in Selenium Webdriver
- CSS Selectors in Selenium
- Vector Class in Java
- Page Object Model in Selenium With Page Factory
- How to Handle Alert and Pop-ups in Selenium WebDriver?
- Selenium Webdriver
- What is Selenium?
- How to Handle Actions Class in Selenium WebDriver?
- Waits in Selenium WebDriver
- Select Class in Selenium WebDriver
- What is BluePrism? Know about the benefits of BluePrism Certification
- What is Silk test Workbench? : Comprehensive Guide
- RPA Developer Salary
- What is Selenium and Why Should You Learn Selenium | Everything You Need to Know
- Robotic Process Automation with Blue Prism | A Definitive Guide with Best Practices [ OverView ]
- An Overview of Patch Workflows | Benefits and Special Features [ For Freshers and Experience ]
- Selenium Commands : Selenese | Everything You Need to Know [ OverView ]
- Robotic Process Automation Developer Salary [ For Freshers and Experience ]
- List of Basic Powershell Commands | Every Developer Should Know [ OverView ]
- Tosca vs Selenium | Differences and Which Should You Learn?
- Chef vs Puppet: Major Differences and Similarities
- what is TestNG Annotations in Selenium Webdriver?
- Blue Prism Certification Guide
- RPA Tools Comparison
- Ansible vs Chef
- How to Download & Install Selenium Ide?
- RPA vs Selenium
- RPA Blue Prism Certification Guide
- Robotic Process Automation Career Scope
- How to Take A Screenshot in Selenium WebDriver?
- Robot Class in Selenium Webdriver
- CSS Selectors in Selenium
- Vector Class in Java
- Page Object Model in Selenium With Page Factory
- How to Handle Alert and Pop-ups in Selenium WebDriver?
- Selenium Webdriver
- What is Selenium?
- How to Handle Actions Class in Selenium WebDriver?
- Waits in Selenium WebDriver
- Select Class in Selenium WebDriver
How to Take A Screenshot in Selenium WebDriver?
Last updated on 22nd Sep 2020, Artciles, Automation, Blog
Taking Screenshot using Webdriver
It’s very important to take a screenshot when we execute a test script. When we execute a huge number of test scripts, and if some test fails, we need to check why the test has failed.
It helps us to debug and identify the problem by seeing the screen shot.
In selenium webdriver, we can take the screen shot using the below command.
- File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Check the framework example of Taking ScreenShot for ONLY Failed Tests using TestNG
The below example explains how to take the screenshot when the test fails.
Subscribe For Free Demo
Error: Contact form not found.
- import java.io.File;
- import org.apache.commons.io.FileUtils;
- import org.openqa.selenium.By;
- import org.openqa.selenium.OutputType;
- import org.openqa.selenium.TakesScreenshot;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.testng.annotations.Test;
- public class takeScreenShotExample{
- public WebDriver driver;
- @Test
- public void openBrowser() throws Exception {
- driver = new FirefoxDriver();
- driver.manage().window().maximize();
- driver.get(“http://www.google.com”);
- try{
//the below statement will throw an exception as the element is not found, Catch block will get executed and takes the screenshot.
- driver.findElement(By.id(“testing”)).sendKeys(“test”);
- //if we remove the comment below, it will not return an exception and the screen shot method will not get executed.
- //driver.findElement(By.id(“gbqfq”)).sendKeys(“test”);
- }
- catch (Exception e){
- System.out.println(“I’m in exception”);
- //calls the method to take the screenshot.
- getscreenshot();
- }
- }
- public void getscreenshot() throws Exception
- {
- File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
- //The below method will save the screenshot in d drive with
- FileUtils.copyFile(scrFile, new File(“D:\\screenshot.png”));
- }
- }
Explanation Of The Above Code
- WebDriver driver = new ChromeDriver();
In the above statement we have upcasted the chromedriver object to webdriver, but for capture the screenshot we need to downcast the chromedriver object driver to TakesScreenshot interface.
- TakesScreenshot ts= (TakesScreenshot)driver;
- File screenshotSRC= ts.getScreenshotAs(OutputType.FILE);
In the above statement, the OutputType is an interface which can provide options to take the screenshot in different types such as FILE, BASE64, BYTES, and class. But out of those types, the file type is mostly used.
- path=System.getProperty(“user.dir”)+”/ScreenCapturesPNG/” +testStepsName+System.currentTimeMillis()+”.png”;
When webdriver takes the screenshot, it stores in the temp folder. Once the execution is over the file also got deleted. That’s why we need to copy the screenshot in a permanent folder, so for that in the above statement we are mentioned the permanent folder which is ScreenCapturesPNG with the file name.
- File screenshotDest= new File(path);
In this statement, we are creating a file where we mentioned where it should be stored with a specific name.
How Do You Take Screenshot of The Entire Page?
For taking the full page screenshot, we can take the help of the third-party tools like AShot to take the whole page screenshot or a specific element screenshot. Now we are going to discuss the AShot tools, and these tools have a rich feature like:
- Take a specific area of a page
- Do scroll and take a screenshot
- In case you have an infinite scroll page, you can set timeouts
- Ignore are during the screenshot
- Change orientation or change resolution
