Pages

Monday 19 November 2012

Take Screenshot using Selenium Webdriver

Why Screenshot is required ?
While doing manual testing we often need to take the screenshot in case we come up with any issue. We can do the same thing for the Automation testing as well. While running a script there might be a chance of fail of the script. So in that case we have to take the screen shot for that failed script in order to report as an issue.
I am giving a simple example how to take the screenshot after opening "google.com" in firefox browser :-

     1. WebDriver driver = new FirefoxDriver(); // Opening the Firefox browser       
      2. driver.get("http://www.google.com/");
      3. File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); // This
          will take the screen shot and will store in srcFile file.
      4. FileUtils.copyFile(scrFile, new File("File Path Location")); // Here you can change the
           path location you want.

Note : File path should contain the file name at the end. e.g. : c:\\temp\\screenshot.png.

After executing the code :
Just go to the path specified and you will able to find a screenshot.


No comments:

Post a Comment