Capture Screenshot of Web Page In Selenium WebDriver

Most of the time we think to Capture Screenshot in WebDriver when some kind of error or exception surfaces while practicing testing, to resolve the same WebDriver has provided us one interface TakesScreenshot for capturing the screenshot of web application and This interface provides one method names as getScreenshotAs() to capture screenshot in instance of driver. This getScreenshotAs() method takes argument of type OutputType.File or OutputType.BASE64 or Output.BYTES. So that it could return captured screenshot in File type, or Base 64 string type or in raw bytes.
So this would look like this

For File type
File scrnshot= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

For Base64 string above code would be like
((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);

For BYTES
((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);

We have taken the screenshot with the help of getScreenshotsAs() method and  and now its time to copy this file somewhere in our file system or in our desktop. So for this purpose we further use copyFile() method of the FileUtils class from the org.apache.commons.io.FileUtils class.

Placing file in file system by using this line

FileUtils.copyFile(scrFile, new File(“e:\main_page.png”));

As I have told you that copyFile is a method of Class FileUtils and to call this method we need to write class.method() and in above code copyFile is taking argument from getScreenShotsAs() and new location where we want to save this captured Screenshot with name and with extension.

Now we would use write small running code that would open Google page and it would take a snap shot and that snap shot would be saved as google.png in my e: driver. One thing we need to remember whenever we work with File system or java.io then chances of exception is more so we would use try and catch in our code.
import java.io.File;
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.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Chrome {
WebDriver driver;
@BeforeMethod
public void launchChrome()
{
System.setProperty("webdriver.chrome.driver", "E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe");
driver = new FirefoxDriver();
driver.get("http://google.co.in");
}
@Test
public void googleScreenshot()
{
try {
File scrnsht =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrnsht, new
File("e:\google_page.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void kill()
{
driver.close();
}
}

Original Resource available on this place
http://stackoverflow.com/questions/13246477/how-to-capture-the-screen-shot-of-a-webpage-using-selenium-webdriver 

15 thoughts on “Capture Screenshot of Web Page In Selenium WebDriver

  1. It’s in reality a nice and helpful piece of

    info. I am satisfied that you simply shared this helpful

    info with us. Please stay us up to date like this. Thank you for
    sharing.

  2. Pingback: Selenium Best Links | Learn Testing

  3. Thanks a lot for your valuable information to on capture screenshot using selenium webdriver.

    It could be useful if you you provide more in depth information on Edit box,Check/Uncheck,Drop down,scrollpage,radion button,Link text implicit wait condition, etc.,,

      • java.lang.UnsupportedOperationException: captureEntirePageScreenshot
        at org.openqa.selenium.WebDriverCommandProcessor.execute(WebDriverCommandProcessor.java:137)
        at org.openqa.selenium.WebDriverCommandProcessor.doCommand(WebDriverCommandProcessor.java:70)
        at com.thoughtworks.selenium.DefaultSelenium.captureEntirePageScreenshot(DefaultSelenium.java:723)
        at testNG.AccountMisMatch.generateBulkReturn(AccountMisMatch.java:59)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

  4. Pingback: Handling JavaScript Alert in WebDriver | Abode QA

  5. i am using same code which working well on normal pages
    buton a page with a video running like youtube the screenshot of video is coming as black

    can u check once and suggest any solution

  6. Hi

    I am trying to take screen shot using following code but its not working
    File scht=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scht, newFile(“c:\\google_page.png”));

    newFile is showing error saying method newFile(String) is undefined. Can you kindly help me to solve this issue
    Thank you

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>