To capture screenshot just place the below code in your script sometimes you might be running your script and you wanted to look what went wrong in application so you can capture the screen shot so you get a clear idea what went wrong on other side of your application.
In the below program I made it a function so that use place it in reusable section and pass paramaters such as file name and driver. It creates a folder by name screenshots and saves the screen shots as file name specified and prints you the location.
public static Object screenshot(String filename,WebDriver driver)
{
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir")+"//screenshots//"+filename+".jpg"));
System.out.println("Output screenshot stored at "+Calendar.getInstance().getTime()+ " in the path--> "+System.getProperty("user.dir")+"\\screenshots\\");
String path="Output screenshot stored at "+Calendar.getInstance().getTime()+ " in the path--> "+System.getProperty("user.dir")+"\\screenshots\\";
return path;
}
catch (IOException e)
{
e.printStackTrace();
System.out.println(filename+"Screenshot Failed to capture screenshot refer above details");
}
return null;
}
public static Object screenshot()
{
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C://"+"//screenshots//"+filename+".jpg"));
}
catch (IOException e)
{
e.printStackTrace();
}
In the below program I made it a function so that use place it in reusable section and pass paramaters such as file name and driver. It creates a folder by name screenshots and saves the screen shots as file name specified and prints you the location.
Reusable function
public static Object screenshot(String filename,WebDriver driver)
{
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir")+"//screenshots//"+filename+".jpg"));
System.out.println("Output screenshot stored at "+Calendar.getInstance().getTime()+ " in the path--> "+System.getProperty("user.dir")+"\\screenshots\\");
String path="Output screenshot stored at "+Calendar.getInstance().getTime()+ " in the path--> "+System.getProperty("user.dir")+"\\screenshots\\";
return path;
}
catch (IOException e)
{
e.printStackTrace();
System.out.println(filename+"Screenshot Failed to capture screenshot refer above details");
}
return null;
}
The below is non reusable code to capture screenshot.
public static Object screenshot()
{
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("C://"+"//screenshots//"+filename+".jpg"));
}
catch (IOException e)
{
e.printStackTrace();
}