Selenium Interview : How will you capture screenshot in Selenium webDriver?

How will you take screenshot in Selenium webDriver?

Answer: The screenshot can be taken by using getScreenshotAs() method.

The sample code is as follows:

public class myTestSuite {

@Test
public void testcase1()
{
// Open Firefox
WebDriver driver=new FirefoxDriver();

// Maximize the window
driver.manage().window().maximize();

// Open the url
driver.get(“http://www.google.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:/screenshot/testcase1.png”));
}

catch (IOException e)
{
System.out.println(e.getMessage());

}
}

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *