Taking screenshot while testing is very important for automation so that you can point out and catch the exact bug. In this post we will learn about how to take screen shot when selenium test case fails. It is more important to have a screenshot when the selenium test fail but you can add the code if you want to capture screen shot when selenium test passes.
Once you know how to use it then you can use it anywhere you want. So lets get started . . .
Once you know how to use it then you can use it anywhere you want. So lets get started . . .
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class ScreenshootGoogle {
@Test
public void TestJavaS1()
{
// Open chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Maximize the window
driver.manage().window().maximize();
// Pass the url
driver.get("http://www.qaautomated.com");
// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
// now copy the screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/error.png"));
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class ScreenshootGoogle { @Test public void TestJavaS1() { // Open chrome browser System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe"); WebDriver driver=new ChromeDriver(); // Maximize the window driver.manage().window().maximize(); // Pass the url driver.get("http://www.qaautomated.com"); // Take screenshot and store as a file format File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); try { // now copy the screenshot to desired location using copyFile //method FileUtils.copyFile(src, new File("C:/selenium/error.png")); } catch (IOException e) { System.out.println(e.getMessage()); } }
Hello,
ReplyDeleteThe Article on Take ScreenShot with Selenium WebDriver is informative give detail information about it.Thanks for Sharing the information about program taking a ScreenShot with Selenium WebDriver. Software Testing Services
This comment has been removed by a blog administrator.
ReplyDeleteNice information Thanks a lot for such a great information.
ReplyDelete