Capture Screenshot in Selenium using Java
In this post, We will see how to capture the screenshot in selenium. Sometimes when our UI automation scenarios got failed, then might be we would required to capture the screenshot to verify wether our application loaded successfully or Not.
In selenium it's very easy to capture screenshot using in-built method getScreenshotAs() of TakesScreenshot interface.
We can capture the screenshot in selenium in 4 simple steps-
Step 1) Convert web driver object to TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
Step 2) Call getScreenshotAs method to create image file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
Step 3) Save image in specified format
File dest = new File("/home/automation/Desktop/Demo/demo_data.jpg");
Step 4) Copy and move source file to destination file
FileUtils.moveFile(SrcFile, dest);
Example:-
TakesScreenshot scrShot = ((TakesScreenshot)driver);
File src1 = scrShot.getScreenshotAs(OutputType.FILE);
File dest = new File("/home/automation/Desktop/Demo/demo_data.jpg");
FileUtils.moveFile(src1, dest);
Writing a Java Program to Capture Screenshot in every 1000 ms of Interval
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
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.apache.commons.io.FileUtils;
public class Capture_SnapShot {
private static WebDriver driver = null;
private long interval = 1000;
public void capture(boolean isRunning) {
while(isRunning) {
try {
File src = getScreenShot();
saveFile(src);
Thread.sleep(interval);
}catch (InterruptedException e) {
System.err.println("Interrupted while sleeping");
}
}
System.out.println("SnapShot not capturing");
}
private File getScreenShot() {
return ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
}
private void saveFile(File src) {
long length = src.length();
File dest = new File("/home/netstorm/Desktop/Demo/demo"+System.currentTimeMillis()+".jpg");
try {
FileUtils.moveFile(src, dest);
}catch(IOException e) {
System.err.println("Failed to save file with name: " + dest);
}
}
public static void main(String arg []){
System.setProperty("webdriver.gecko.driver", "/home/automation/Back_Up/geckodriver");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Capture_SnapShot cap = new Capture_SnapShot();
cap.capture(true);
driver.get("https://dummytourstravel.com");
cap.capture(false);
driver.findElement(By.xpath(".//*[@id='download-button' and text()='Get Started']")).click();
driver.findElement(By.xpath(".//input[@name='username']")).sendKeys("admin");
driver.findElement(By.xpath(".//input[@name='password']")).sendKeys("Admin");
cap.capture(true);
driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div/div[1]/div[2]/div/form/div[3]/button")).click();
cap.capture(false);
}
}
In above example , When we call cap.capture(true); method it will start capturing image in every 1000 ms of interval and when we call cap.capture(false); then image capturing process will stop
So Hope this example will helpful....
Post a Comment