Automatic screenshot on error

1,003 views
Skip to first unread message

robbie....@nokia.com

unread,
Aug 30, 2011, 3:56:05 AM8/30/11
to seleniu...@googlegroups.com
Hi,
 
I have screenshot capturing working with RemoteWebdriver, and combined with TestNG, I can capture the screenshot as part of the MethodTeardown.  However, I’m sure I should be able to let Selenium take care of that for me and capture screenshots on error.
 
Is this true, or is using the teardown the best way of doing it?  If not, any hints as to how to set the RemoteWebdriver object to capture screenshot on selenium failure?
 
Thanks
Robbie Wareham
 
 
 

Krishnan Mahadevan

unread,
Aug 30, 2011, 4:05:20 AM8/30/11
to seleniu...@googlegroups.com
Robbie,
There's a method  void onException(Throwable throwable, WebDriver driver); that resides within the interface WebDriverEventListener.
Perhaps you could have a class that implements this interface and then hook it onto an EventFiringWebDriver and try.

Just a suggestion, because I havent personally given it a try to be able to give you a concrete answer.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

Mike

unread,
Aug 30, 2011, 11:58:05 AM8/30/11
to Selenium Users
Robbie,

I have a routine that I use for logging errors, but I am not sure if
doing it automatically is the right idea, because a thrown exception
could be expected in some cases. So I have it that my routine must be
called by the test code to have it reported.

But I have not gotten screen capture to work right yet with
RemoteWebdriver.. Could you share how you are doing it?

My understanding was that not all Webdriver versions would support
doing a screen capture. Have you found that to be true?

Mike

robbie....@nokia.com

unread,
Aug 30, 2011, 12:55:53 PM8/30/11
to seleniu...@googlegroups.com
Try this for screenshots;

After creating your RemoteWebdriver instances, add;

driver = new Augmenter().augment(driver);


then when you want to take a screenshot;

final File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("ScreenShotOnFailure.png"));

Robbie,

Mike

--

Mike

unread,
Sep 2, 2011, 2:48:29 PM9/2/11
to Selenium Users
I am still unsure just how this can work, even after looking at the
Javadoc for the Augmenter calss, but I see that I missed the step of
creating an instance of Augmenter when I tried this. I just added
that and I will have to try it, but not knowing just what Augmenter()
will do to my driver instance I decided to only do it when I am trying
to get the screen shot. Is this essentially what you meant?

private TakesScreenshot getScreenshot()
{
return (TakesScreenshot) new Augmenter().augment(webDriver);
}
public void logException(String explanation, Throwable ex)
{
// Code to report explanation and ex message removed...
File file;
try
{
file = getScreenshot().getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("ScreenSnapshot.PNG"));
Reporter.log("<img src=\"file:///" + reportDir + "\
\ScreenSnapshot.PNG\" alt=\"\"/><br />");
}
catch (Throwable ex1)
{
Reporter.log("Unable to capture screentshot to " +
"ScreenSnapshot.PNG\nGot error: " + ex1.getMessage());
} // try-catch
} // logException

Mike

unread,
Sep 2, 2011, 5:13:45 PM9/2/11
to Selenium Users
Even after changing my code to match yours I get this error:
org.openqa.selenium.remote.RemoteWebDriver$$EnhancerByCGLIB$$b8da5795
cannot be cast to org.openqa.selenium.TakesScreenshot

This is for Internet Explorer 8, in case it matters.

The code I just tried is:
file = ((TakesScreenshot) new
Augmenter().augment(webDriver)).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("ScreenSnapshot.PNG"));
Reporter.log("<img src=\"file:///" + reportDir + "\\ScreenSnapshot.PNG
\" alt=\"\"/><br />");

It throws that exception on the very first line.

I did single-step into the Augmenter code and see where it says it has
this capability at one point, so it sort of looks like it might be
able to work. Is the problem that the browser driver for IE doesn't
support it? If that is the case, which drivers do support this
ability?

Mike

On Aug 30, 9:55 am, <robbie.ware...@nokia.com> wrote:

Jim Evans

unread,
Sep 2, 2011, 5:16:39 PM9/2/11
to Selenium Users
I'm no expert on the Java bindings, so I can't help with the Augmenter
class; however, I can state unequivocally that the IE driver supports
taking screenshots.

Mike

unread,
Sep 2, 2011, 6:44:37 PM9/2/11
to Selenium Users
Well it is very interesting as to how it works. The Augmenter class
does say that it is experimental code and to use at your own risk.

I have been able to run some testing with the other browsers and I
found that Firefox, Chrome, and Opera all worked fine. I have tried
this with both IE8 and IE9 and they both fail in the same way. So I
am guessing that however Augmenter does its magic it is failing with
the IE version of the RemoteWebDriver. Sounds like a bug worth
filing, but at least now I can grab screenshots for all the other
browser types.

I still say it shouldn't be this hard and that using
WebDriverBackedSelenium should let us do this, at least for screen
shots. Being able to take a snapshot of the screen at the time of a
failure has to be one of the most important tools in an automated test
suite. Mine is going to be running unattended in the middle of the
night, so capturing that is critical to my testing.

If someone can figure out how to make this code work for IE as well
please let me know. Otherwise I will file a bug on it.

Mike

Felipe Knorr Kuhn

unread,
Sep 2, 2011, 7:28:00 PM9/2/11
to seleniu...@googlegroups.com
Although this could be somewhat overkill, you could implement AOP (aspect oriented programming) techniques to intercept method calls when specific exceptions are thrown.

It's very powerful but the learning curve is a bit steep.

FK

Mike

unread,
Sep 8, 2011, 2:39:23 PM9/8/11
to Selenium Users
I am not sure how that would apply here, but I have gotten my code to
work with every browser, almost...

It seems that the odd versions of Internet Explorer are not working
and throwing an exception. I have filed issue 2423 with details on
this and in there you can see the code I am using. The code
definitely works and IE6 and IE8 worked for me in my tests last night,
while IE7 and IE9 both failed with the same error. It seems pretty
obvious that something is broken in Internet Exploder land, and not
for the first time I might add. ;)

Here is the link to the issue I filed for anyone that wants to peruse
the code for their own use (no need to post it here, too):
http://code.google.com/p/selenium/issues/detail?id=2423&can=4&sort=-id&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary

Thanks for the suggestions provided in this thread. At least I can
now get a screenshot about 80% of the time and hopefully the code I
provided will help others do the same. I still say that using
WebDriverBackedSelenium should allow this to work, using a variation
of the same code I have provided.

Mike

Arun Kumaresan

unread,
Jan 23, 2012, 4:07:21 PM1/23/12
to Krishnan Mahadevan, Selenium Users
Here is a code to take snapshot. The Beauty of this code is that it
takes snapshot only if Test Script fails.

http://darrellgrainger.blogspot.com/2011/02/generating-screen-capture-on-exception.html

On Aug 30 2011, 12:05 am, Krishnan Mahadevan


<krishnan.mahadevan1...@gmail.com> wrote:
> Robbie,
> There's a method  void onException(Throwable throwable, WebDriver driver);
> that resides within the interface WebDriverEventListener.
> Perhaps you could have a class that implements this interface and then hook
> it onto an EventFiringWebDriver and try.
>
> Just a suggestion, because I havent personally given it a try to be able to
> give you a concrete answer.
>
> Thanks & Regards
> Krishnan Mahadevan
>
> "All the desirable things in life are either illegal, expensive, fattening
> or in love with someone else!"
>
>
>
> On Tue, Aug 30, 2011 at 1:26 PM, <robbie.ware...@nokia.com> wrote:
> >  Hi,
>
> > I have screenshot capturing working with RemoteWebdriver, and combined with
> > TestNG, I can capture the screenshot as part of the MethodTeardown.
> > However, I’m sure I should be able to let Selenium take care of that for me
> > and capture screenshots on error.
>
> > Is this true, or is using the teardown the best way of doing it?  If not,
> > any hints as to how to set the RemoteWebdriver object to capture screenshot
> > on selenium failure?
>
> > Thanks

> > *Robbie Wareham
> > *


>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Selenium Users" group.
> > To post to this group, send email to seleniu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > selenium-user...@googlegroups.com.
> > For more options, visit this group at

> >http://groups.google.com/group/selenium-users?hl=en.- Hide quoted text -
>
> - Show quoted text -

Tarun Bhadauria

unread,
Jan 23, 2012, 11:53:30 PM1/23/12
to seleniu...@googlegroups.com, Krishnan Mahadevan
Since you are already on TestNG, I would suggest using FEST. I use it in my Selenium tests and works pretty well -

Reply all
Reply to author
Forward
0 new messages