8

Does Webdriver 2.28 automatically take a screenshot on exception/fail/error?

If it does, where can I find the screenshot? Which directory is the default?

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
Tingting
  • 157
  • 1
  • 8
  • According to the WebDriver documentation, the RemoteWebDriver exceptions often have a screenshot attached. It can be accessed via an accessor on the exception. http://code.google.com/p/selenium/wiki/RemoteWebDriver – greggian Oct 11 '13 at 12:39

3 Answers3

11

No, it doesn't do it automatically. Two options you can try:

  1. Use WebDriverEventListener that is attachable to a EventFiringWebDriver which you can simply wrap around your usual driver. This will take a screenshot for every Exception thrown by the underlying WebDriver, but not if you fail an assertTrue() check.

    EventFiringWebDriver driver = new EventFiringWebDriver(new InternetExplorerDriver());
    WebDriverEventListener errorListener = new AbstractWebDriverEventListener() {
        @Override
        public void onException(Throwable throwable, WebDriver driver) {
            takeScreenshot("some name");
        }
    };
    driver.register(errorListener);
    
  2. If you're using JUnit, use the @Rule and TestRule. This will take a screenshot if the test fails for whatever reason.

    @Rule
    public TestRule testWatcher = new TestWatcher() {
        @Override
        public void failed(Throwable t, Description test) {
            takeScreenshot("some name");
        }
    };
    

The takeScreenshot() method being this in both cases:

public void takeScreenshot(String screenshotName) {
    if (driver instanceof TakesScreenshot) {
        File tempFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(tempFile, new File("screenshots/" + screenshotName + ".png"));
        } catch (IOException e) {
            // TODO handle exception
        }
    }
}

...where the FileUtils.copyFile() method being the one in Apache Commons IO (which is also shipped with Selenium).

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • 1
    Is there possible to take screenshot without using the above `takeScreenshot` method and the `createScreenCapture` method of `java.awt.Robot` class in Selenium. – Manigandan Feb 06 '13 at 04:29
  • @Manigandan Sure, you can create a screenshot that way, too. The downside is that the screenshot taken by WebDriver spans over the whole page. That means that if the page is very long and doesn't fit into one screen, WebDriver will still screenshot the whole page, whereas Robot will not. – Petr Janeček Feb 06 '13 at 04:52
  • I asked, i need to take screenshot without using the above two methods. Is there any way to do? – Manigandan Feb 06 '13 at 04:59
  • @Manigandan Oh, sorry, I didn't understand that. Um. What's wrong about those two? Why can't you / don't you want to use them? I'm not aware of any other possibility, though I'm sure there are some - I just never needed anything else. Look, for example, here: http://stackoverflow.com/questions/2912007/java-how-to-take-a-screenshot-fast and at similar questions. – Petr Janeček Feb 06 '13 at 05:12
  • I was used `takeScreenshot` method to take screenshots for my test cases. It works fine except the `IE Browser`. While using the above method the IE is re-sizing the window while taking screenshot. During re-sizing some times i am not able to do the preferred action. So my test case fails. To overcome that i go for `Robot class`. But i faced problem there too. That is i am not able to get the screenshot when the browser is minimized. So for the above reasons i am searching some other solutions. – Manigandan Feb 06 '13 at 05:30
  • @Manigandan Yeah, I don't think you'll be able to take a screen from an outside minimized application in a sensible way. That said, good luck :). – Petr Janeček Feb 06 '13 at 06:05
  • @Slanec when you say whenever an exception is thrown by underlying webdriver, do you mean to say, the event won't fire for other exceptions such as NullPointerException? – Code Enthusiastic Mar 06 '13 at 12:49
  • 1
    @CodeEnthusiastic Yes, that's exactly it. The first solution uses `EventFiringWebDriver` which is a decorator around any other `WebDriver` and which is able to catch only the excepctions thrown by such. I personally use both those solutions combined - on a `WebDriver` error, I take a screenshot. On these and on any other (like NPE), I log the problem. – Petr Janeček Mar 06 '13 at 14:02
2

WebDriver does not take screenshot itself. But you cat take by this way : ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);

Then save the screenshot anywhere you want.

Also you cat use something like Thucydides, wich may take screenshot on each action or on error and put it in a pretty report.

Pazonec
  • 1,549
  • 1
  • 11
  • 35
1

The short answer is No. WebDriver is an API to interact with the browser. You can make screenshots with it but you should know when to do it. So it's not done automatically as WebDriver doesn't know anything about testing.

If you are using TestNG as testing library you can implement Listener whose methods will be executed on different events (failure, success or other). In these methods you can implement the required logic (e.g. making screenshots).

Igor Khrol
  • 1,758
  • 13
  • 18