How to take screenshots using selenium scripts?

We can use takeScreenShot desired capabilities for all browsers such as Firefox/Chrome/IE.


Java API Syntax : public  X getScreenshotAs(OutputType target) throws 
                               WebDriverException

Below is an simple code to take the screenshot of your browser windows.


Note: Before you run the scripts make sure you have set the breakpoints on before printing getting Absolute Path. getAbsolutePath is nothing but the screenshot is generated location.  In this case, it will be in temp folder.  If you not running the code in debugging mode, it will create the screenshot on %Temp% folder and then will be deleted automatically.


To see the Temp folder : For windows users, Go to Run command and enter %TEMP%.


CLICK HERE FOR VIDEO LINK : Selenium with JAVA - How to Take Screenshot using Selenium Scripts?

Code:

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class BrowserDesiredCapability {
 
public static void main(String args[])
{

//Launching firefox browser
WebDriver driver = new FirefoxDriver();
  
//Launching website
driver.get("http://www.techyvicky.com/");
  
//Screenshot syntax and code
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
   
//Pause your script to see the screenshot - Go to %TEMP% folder to verify
System.out.println(scrFile.getAbsolutePath());

} 
}
Socialize It and Share the post with your friends
SOCIALIZE IT →
FOLLOW US →
SHARE IT →