EventFiringWebDriver does not support taking screenshots

923 views
Skip to first unread message

HPK

unread,
Aug 31, 2011, 3:48:24 AM8/31/11
to Selenium Users
Hi,
I am using EventFiringWebDriver in my selenium2 implementation. But I
am not able to take screenshots with it. Below is the code trying to
take a remote screenshot and store it in Base64 format.I get the
error
'java.lang.UnsupportedOperationException: Underlying driver instance
does not support taking screenshots'.Underlying Driver instance is
EventFiringWebDriver.Is there something I am missing.
byte[] decodedScreenshot =
Base64.decodeBase64(((TakesScreenshot)
(driver)).getScreenshotAs(OutputType.BASE64).getBytes());

Thanks

sabf

unread,
Aug 31, 2011, 4:37:37 AM8/31/11
to seleniu...@googlegroups.com
i use this to take screenshots

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

String screenshot = <Path to directory>and name of screenshot eg. "c:\\work\\myscreenshot.png"

File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(screenshot));

cjayswal

unread,
Aug 31, 2011, 7:21:13 AM8/31/11
to Selenium Users
Check http://blog.infostretch.com/?p=1138 where possible solution to
issue in take screenshot with remote web driver is provided.

HPK

unread,
Aug 31, 2011, 8:04:25 AM8/31/11
to Selenium Users
@ cjayswal
Thanks for the link. But I am using EventFiringWebDriver .I was able
to take screen shots until I used Remotewebdriver. But currently I am
using EventFiringWebDriver for generating logs.Since I have switched
to EventFiringWebDriver , I am not able to take screen shots.

@sabf
I am getting the same error as it is a similar implementation of
'getScreenshotAs'

Has anybody successfully implemented 'getScreenshotAs' with
EventFiringWebDriver ?

Thanks


On Aug 31, 12:21 pm, cjayswal <cjays...@gmail.com> wrote:
> Checkhttp://blog.infostretch.com/?p=1138where possible solution to

Krishnan Mahadevan

unread,
Aug 31, 2011, 9:48:41 AM8/31/11
to seleniu...@googlegroups.com
Please check if this helps : http://groups.google.com/group/webdriver/msg/3f6d3cef6e178cdd?hl=en&


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.


cjayswal

unread,
Sep 1, 2011, 4:58:30 AM9/1/11
to Selenium Users
I am able to capture screen shot with EventFiringWebDriver.
Here is the test code:
EventFiringWebDriver wd = new EventFiringWebDriver(new
InternetExplorerDriver());
or
EventFiringWebDriver wd = new EventFiringWebDriver(new
FirefoxDriver());

wd.get("http://www.google.com");
byte[] screenShotBytes = wd.getScreenshotAs(OutputType.BYTES);
I was able to reproduce the problem when make use of remote web
driver.
EventFiringWebDriver wd =
new EventFiringWebDriver(new RemoteWebDriver(new URL(
"http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox()));

Exception in thread "main" java.lang.UnsupportedOperationException:
Underlying driver instance does not support taking screenshots
at
org.openqa.selenium.support.events.EventFiringWebDriver.getScreenshotAs(EventFiringWebDriver.java:
254)
at Test1.main(Unknown Source)

If this is the case then i am sure use the way provided in
http://blog.infostretch.com/?p=1138. Instead of RemoteWebDriver create
RemoteWebDriver implementation as discussed in blog and use it when
creating EventFiringWebDriver instance.
For example

EventFiringWebDriver wd =
new EventFiringWebDriver(new MyRemoteWebDriver(new URL(
"http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox()));

I did test it with ISFW provided implementation and got success.
EventFiringWebDriver wd =
new EventFiringWebDriver(new IsRemoteWebDriver(new URL(
"http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox()));
wd.get("http://www.google.com");
// byte[] screenShotBytes = wd.getScreenshotAs(OutputType.BYTES);

// File screenShot = wd.getScreenshotAs(OutputType.FILE);
String screenShotAsString = wd.getScreenshotAs(OutputType.BASE64);
try {
System.out.println(FileUtil.saveImageFile(screenShotAsString,
"TMP", "."));
} catch (Exception e) {
e.printStackTrace();
}

On Aug 31, 4:21 pm, cjayswal <cjays...@gmail.com> wrote:
> Checkhttp://blog.infostretch.com/?p=1138where possible solution to

Krishnan Mahadevan

unread,
Sep 1, 2011, 6:58:33 AM9/1/11
to seleniu...@googlegroups.com
Or try out Daniel's recommendation  from this thread : http://groups.google.com/group/webdriver/browse_thread/thread/77712549d97ef231?hl=en&tvc=2

Thanks & Regards
Krishnan Mahadevan

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



Krishnan Mahadevan

unread,
Sep 13, 2012, 8:39:15 AM9/13/12
to seleniu...@googlegroups.com
Samar,

Looking at the implementation of EventFiringWebDriver I see the below :

  public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
    if (driver instanceof TakesScreenshot) {
      return ((TakesScreenshot) driver).getScreenshotAs(target);
    }

    throw new UnsupportedOperationException(
        "Underlying driver instance does not support taking screenshots");
  }

Since you are seeing the exception UnsupportedOperationException() being thrown, I am guessing that you ended up passing in an instance of RemoteWebDriver to EventFiringWebDriver.

AFAIK, RemoteWebDriver doesnot implement TakesScreenshot [Not sure why it was chosen to be so]. 

So what you are seeing is "as per design".

Thanks & Regards
Krishnan Mahadevan

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



On Tue, Sep 11, 2012 at 12:12 AM, Samar Sood <samars...@gmail.com> wrote:
I am facing same issue on taking screen shots.

getting below error.

java.lang.UnsupportedOperationException: Underlying driver instance does not support taking screenshots
        at org.openqa.selenium.support.events.EventFiringWebDriver.getScreenshotAs(EventFiringWebDriver.java:262)

I am getting this exception while calling getScreenshotAs()

File scrFile =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

I tried almost every solution on this group but still not able to save screenshots.

-Samar

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/rWvdbDmWEVIJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mike Riley

unread,
Sep 13, 2012, 4:21:43 PM9/13/12
to seleniu...@googlegroups.com
It will if you use the Augmenter class:
new Augmenter().augment(driver);

Mike

Krishnan Mahadevan

unread,
Sep 14, 2012, 10:06:04 AM9/14/12
to seleniu...@googlegroups.com
Mike,

Since I have never used the Augmenter myself, I decided to give a shot at this.

But I haven't been able to figure out how to get this to work with the EventFiringWebDriver [ which is instantiated with a reference to a RemoteWebDriver instance]

Perhaps you could shed some light here ?

The problem I am having is in figuring out how do I add a addDriverAugmentation

Here's what I have as code so far.

public class ScreenshotUsingEventFiringWebDriver {
private EventFiringWebDriver efd = null; 
@Test
public void f() throws WebDriverException, IOException {
Augmenter aug = new Augmenter();
//Cant figure out what should be provided as the second argument.
//This is because the org.openqa.selenium.remote.AddTakesScreenshot which implements a
//AugmenterProvider interface has its visibility set as default, which means 
//this class is NOT visible for me outside the package and as such I cant instantiate it.
//aug.addDriverAugmentation(CapabilityType.TAKES_SCREENSHOT,)
TakesScreenshot screenshot = (TakesScreenshot)aug.augment(efd);
FileUtils.copyFile(screenshot.getScreenshotAs(OutputType.FILE),new File("src/test/resources.krishnan.jpg"));
}

@BeforeClass
public void beforeClass() throws MalformedURLException {
DesiredCapabilities dc = new DesiredCapabilities();
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
RemoteWebDriver rwd = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),dc);
efd = new EventFiringWebDriver(rwd);
}

@AfterClass
public void afterClass() {
efd.quit();
}

}

Running the code as is, triggers an UnSupportedOperationException [which I would expect, because based on what I seen in the RemoteWebDriver implementation, it is expected to throw this exception when a user wants to take a screenshot]


FAILED: f
java.lang.UnsupportedOperationException: Underlying driver instance does not support taking screenshots
at org.openqa.selenium.support.events.EventFiringWebDriver.getScreenshotAs(EventFiringWebDriver.java:265)
at raw.code.ScreenshotUsingEventFiringWebDriver.f(ScreenshotUsingEventFiringWebDriver.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/xdcRWIlR5DgJ.

Krishnan Mahadevan

unread,
Sep 14, 2012, 10:41:08 AM9/14/12
to seleniu...@googlegroups.com
Ok, I think I figured this out on my own.


The idea was to apply the Augmenter and augment it on the RemoteWebDriver and NOT on the EventFiringWebDriver or the wrappedWebDriver.

Trying to use the Augmenter class on either the
EventFiringWebDriver directly or on the 
WrappedWebDriver [org.openqa.selenium.support.events.EventFiringWebDriver.getWrappedDriver()]

would trigger errors.

So the approach to be used is, pass the instance of RemoteWebDriver itself to the WebDriverEventListener implementing class while it is being instantiated and registered into the EventFiringWebDriver and then within the listener's onException method, use the Augmenter to augment the RemoteWebDriver's instance and then take screenshots with that.

The main hint was from Daniel Wagner's response in this thread : https://groups.google.com/forum/?hl=en&fromgroups#!topic/webdriver/d3ElSdl-8jE

For the benefit of anyone who might be looking for a sample, here's a full fledged sample code that shows how to do this : https://gist.github.com/3722268

Good learning !!

Thanks & Regards
Krishnan Mahadevan

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



Krishnan Mahadevan

unread,
Sep 14, 2012, 10:48:48 AM9/14/12
to seleniu...@googlegroups.com
This is turning out to be even more interesting.

Turns out that none of all this workarounds is even required [As stated by me in my previous note]
So the approach to be used is, pass the instance of RemoteWebDriver itself to the WebDriverEventListener implementing class while it is being instantiated and registered into the EventFiringWebDriver and then within the listener's onException method, use the Augmenter to augment the RemoteWebDriver's instance and then take screenshots with that.

All that one has to do is apply the augmenter on the incoming WebDriver parameter in the onException method and everything is all hunky dory !

I updated my gist post with the correct code that would do this !

Thanks & Regards
Krishnan Mahadevan

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




Mike Riley

unread,
Sep 16, 2012, 12:41:16 AM9/16/12
to seleniu...@googlegroups.com
I have only been using the RemoteWebDriver instance up until now.  Next time I use Selenium I will probably explore using the Event Firing version.  So I am glad you figured it out.

Mike

somesh bansal

unread,
May 17, 2013, 4:22:49 AM5/17/13
to seleniu...@googlegroups.com

somesh bansal

unread,
May 17, 2013, 4:23:13 AM5/17/13
to seleniu...@googlegroups.com
Hello Krishnan,

I followed your links https://gist.github.com/3722268 & as able to setup      & capture screenshot 

Noticed: As 'onException' is called for every exception instance thrown. I am using explicit wait & polling happens on random basis so it captures multiple screenshot. I want it should behave like -- onTestFailure (TestListenerAdapter)

Eg: code is like

@Test
public void foo() throws WebDriverException, IOException {
// Now lets trigger an exception by looking for a Non Existing element.
new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(By.name("foo")));
efd.findElement(By.name("foo")).click();
}

In this case element takes time to become enabled, so this executes 'onException' on every random polling

Please recommend, as how to handle this

Regards
Somesh

On Wednesday, August 31, 2011 1:18:24 PM UTC+5:30, HPK wrote:

Krishnan Mahadevan

unread,
May 17, 2013, 6:45:45 AM5/17/13
to Selenium Users
In that case you shouldnt even be putting your screen capture mechanism in onException event, but instead be putting it under the onTestFailure() event. But then you would also need to figure out a mechanism as to how do you relay back your WebDriver instance to your listener.

A good way to do that is to seggregate WebDriver initialization logic such that it populates a ThreadLocal variable which can then be queried by other methods via a static method from anywhere else.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

Somesh Bansal

unread,
May 21, 2013, 12:16:25 AM5/21/13
to seleniu...@googlegroups.com
Hello Krishnan,

Apology for being late. Thanks for your input, you had always been very supportive. I do tell my colleagues in case you have any doubt / query.. just ask Krishnan. he will surely help / respond

I will try thread logic & let you know my progress. It would be great if you can share any link which got the implementation

Thanks 
Somesh





Krishnan Mahadevan

unread,
May 21, 2013, 1:30:37 AM5/21/13
to Selenium Users
Somesh,

Take a look at this sample that Francois created in his Selenium/TestNG tutorial github project : https://github.com/freynaud/testng-support/blob/master/src/main/java/org/openqa/selenium/support/testng/WebTestSession.java

This should give you a fair idea on where to start.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


Reply all
Reply to author
Forward
0 new messages