Hello welcome to Selenium tutorials, in this post we will see how to capture Screenshot in Selenium Webdriver
In automation, it is mandatory to take the screenshot for verification so that can prove also that your test case has covered certain functionality or not.Screenshots help you a lot when your test case will fail so that you can identify what went wrong in your script or in your application.
I also implemented screenshot on failure in my framework which makes my test more robust so I would recommend you to implement the same in your framework too.
How to capture Screenshot in Selenium webdriver
For taking screenshots 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
Let’s implement the same
Code for capture Screenshot
If you use above code then it will take screenshot and will store in C:/selenium and screenshot name will be error.png
Now consider a scenario where you have to take multiple screenshots then above code will be repetitive so for this we will create a small method which captures screenshots and will call this method from our script.
So this method we can call from our test case and call multiple time.
Complete program for Screenshot in Selenium Webdriver
So in above program you can see I called captureScreenshot method three times like this you can create reusable actions in your script and you can use the same.
In automation, it is mandatory to take the screenshot for verification so that can prove also that your test case has covered certain functionality or not.Screenshots help you a lot when your test case will fail so that you can identify what went wrong in your script or in your application.
I also implemented screenshot on failure in my framework which makes my test more robust so I would recommend you to implement the same in your framework too.
How to capture Screenshot in Selenium webdriver
For taking screenshots 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
Scenario – Open Google and take screenshot
Let’s implement the same
For usage of FleUtils nee to import below maven defendency
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
Code for capture Screenshot
If you use above code then it will take screenshot and will store in C:/selenium and screenshot name will be error.png
Now consider a scenario where you have to take multiple screenshots then above code will be repetitive so for this we will create a small method which captures screenshots and will call this method from our script.
So this method we can call from our test case and call multiple time.
Complete program for Screenshot in Selenium Webdriver
So in above program you can see I called captureScreenshot method three times like this you can create reusable actions in your script and you can use the same.