Saturday, September 15, 2012

How to take screenshot of a page using webdriver?

How to take screenshot of a page using webdriver?

 Here is a sample code to get the screenshot of the current browser window using webdriver ...

The file name with the specific path should mentioned. Either it should be dynamic or we can hard-code the file name ( every time we run this code, the file will be overriden).


    public static void snapshot() throws IOException{
       
        File fi = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(fi, new File("file name with the path should be mentioned here... "));
    }

It is very simple and we call this method when ever we need (Pass or Fail cases) to take the screenshots of the pages.

Hope this is helpful.. :)

No comments:

Post a Comment