Skip to main content

Program to Capture Screenshots using WebDriver

Program to Capture Screenshots using WebDriver

As a QA its very important to take screenshot when we execute a test script. When we execute 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);

Lets see the complete program-

package webdriver;import java.io.File;
import java.io.IOException;
import java.util.List;
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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class login {public static void main(String[] args) throws IOException
{
FirefoxDriver driver = new FirefoxDriver();
driver.get(“”https://www.google.com/””);
List<WebElement>links=driver.findElements(By.tagName(“”a””));
for(int i=0;i<links.size();i++)
{
if(links.get(i).isDisplayed())
{
String linkname=links.get(i).getText();
links.get(i).click();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(“”D://linkname.png””));
driver.navigate().back();
}

}
}
}

Screenshot