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 {

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

// Maximize the window

// Open the url

// 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)


Be First to Comment

Leave a Reply

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