Take screenshot with RemoteWebDriver

7,798 views
Skip to first unread message

Leo Arias F.

unread,
Apr 27, 2011, 12:34:25 PM4/27/11
to Selenium Users
Hello,

I'm trying to capture screensthos with RemoteWebDriver, but I get
"RemoteWebDriver cannot be cast to
org.openqa.selenium.TakesScreenshot"

When I start the driver, I use:
capabilities.setBrowserName("firefox");
capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
driver = new RemoteWebDriver(url, capabilities);

And, in order to get the screenshot:
File scrFile =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

But the cast is not valid. What am I doing wrong?

thanks,
pura vida.

Lutfi Dughman

unread,
Apr 27, 2011, 12:40:55 PM4/27/11
to seleniu...@googlegroups.com
according to eclipse (using type hierarchy) RemoteWebDriver does NOT implement 'TakesScreenshot' interface but the following DO implement the interface


AndroidDriver
ChromeDriver
EventFiringWebDriver
FirefoxDriver
IPhoneDriver

Also please in mind im checking again selenium2.01. but i think it applies to the version you are using.


--
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.


Leo Arias F.

unread,
Apr 27, 2011, 1:34:07 PM4/27/11
to Selenium Users
On Apr 27, 10:40 am, Lutfi Dughman <lutf...@gmail.com> wrote:
> according to eclipse (using type hierarchy) RemoteWebDriver does NOT
> implement 'TakesScreenshot' interface but the following DO implement the
> interface

But then, it is not possible to take an screenshot from the
RemoteWebDriver?
It is really handy to pass the browser I want as a capability, because
it is read from a properties file. I would like to avoid doing
something like:

if (browserName.equals("firefox") {
new FirefoxDriver;
} else if (browserName.equals("chrome") {
new ChromeDriver;
} else if ...

But it seems like the only way to be able to capture the screenshot.
Isn't it?

Lutfi Dughman

unread,
Apr 27, 2011, 2:00:18 PM4/27/11
to seleniu...@googlegroups.com
since there are only 3 main drivers that extend RemoteWebDriver, i'ld so go with it.

you dont have to create a new instance (as you do in the your example), simply cast then call the getScreenshot().

maybe someone else has a better idea??


--

Luke Inman-Semerau

unread,
Apr 27, 2011, 6:45:31 PM4/27/11
to seleniu...@googlegroups.com
I don't think the casting will work, he'll have to instantiate the individual instances... RemoteWebDriver is a concrete class (WebDriver is an interface). Sounds like it is a miss in development that RemoteWebDriver doesn't also implement TakeScreenShot... looks like it should too, since almost every WebDriver class (Firefox, IE, Chrome, iphone) has basically the same code for getScreenShotAs  and they all extend RemoteWebDriver. Maybe intentional though?

Frank

unread,
May 15, 2011, 10:39:51 PM5/15/11
to Selenium Users
I attempted to use the Augmenter class to enable me to take
screenshots from RemoteWebDriver:

WebDriver driver = new
RemoteWebDriver(remoteAddress,desiredCapabilities);
driver = new Augmenter().augment(driver);
File srcFile =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("remote_webdriver.png"));

The screenshot command is executed seemingly successfully on the
remote box (no errors are thrown), and the "remote_webdriver.png" is
created on the box running the client code. When I try to open the
file (both on OS X and Win7), I get an error stating that the file is
corrupted. This seems to indicate that the image encoder may be broken
in the 2.0b3 release. In an attempt to see if the nightlies had fixed
the issue, I built the standalone server from trunk, but there was no
change.

Until this encoding issue is fixed, does anyone know of a workaround?
Is there perhaps a way to pass a remoteAddress parameter into
FirefoxDriver, as is the case with RemoteWebDriver?

On Apr 27, 1:00 pm, Lutfi Dughman <lutf...@gmail.com> wrote:
> since there are only 3 main drivers that extend RemoteWebDriver, i'ld so go
> with it.
>
> you dont have to create a new instance (as you do in the your example),
> simply cast then call the getScreenshot().
>
> maybe someone else has a better idea??
>

Donald Ngo

unread,
May 16, 2011, 6:27:33 PM5/16/11
to Selenium Users
What is the recommended solution if we are using the RemoteWebDriver
for 2.0 to run our tests against remote machines then? Can we not take
screenshots?

Thanks,

Donald

Mike

unread,
May 23, 2011, 11:09:21 AM5/23/11
to Selenium Users
You can take screenshots with the 'workaround' given by Frank. Oddly
enough the images are not corrupt for me and show the screenshot. Only
thing is that sometimes the resizing seems to work a bit weirdly
giving a black rectangle at the bottom of the screen. The 'workaround'
works with Firefox and Internet Explorer. You will get a casting error
with Chrome when casting to TakesScreenshot. This is presumably
because Chrome has yet to implement TakesScreenshot.

micsky

unread,
May 24, 2011, 11:36:15 AM5/24/11
to Selenium Users
What I do:

File file = getFile();
if ( file.getParentFile().exists() &&
file.getParentFile().isDirectory() ) {
log.debug("Log directory exists = '{}'",
file.getParentFile().getAbsolutePath());
try {
switch (browserType) {
case FIREFOX :
FileUtils.copyFile(((FirefoxDriver)
WebDriverRunner.getInstance().getWebDriver()).getScreenshotAs(OutputType.FILE),
file);
break;
case INTERNET_EXPLORER :
final Rectangle captureSize = new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
final Robot robot = new Robot();
BufferedImage bufferedImage =
robot.createScreenCapture(captureSize);
ImageIO.write(bufferedImage, "png", file);
break;
case CHROME :
FileUtils.copyFile(((ChromeDriver)
WebDriverRunner.getInstance().getWebDriver()).getScreenshotAs(OutputType.FILE),
file);
break;
default:
log.error("Unable to take screen capture.
Unsupported browser type: '{}'", browserType.toString());
}

log.debug("Successfully took webpage screen capture
saving to file = '"+ file.getAbsolutePath() +"'");
return formatReturn(file.getName());
} catch (Exception ex) {
log.error("Failed to take screen capture, Caused By:
"+ ex.getMessage());
}
} else {
log.error("Unable to take screen capture. Log directory
does not exist = '"+ file.getParentFile().getAbsolutePath() +"'");

Mike

unread,
May 24, 2011, 2:27:37 PM5/24/11
to Selenium Users
This method is less ideal since Java Robot provides less functionality
than WebDriver's screenshot facilities. For example, underlyingly,
InternetExplorerWindow uses PrintWindow() natively which works even if
the screen is not foreground.

Luke Inman-Semerau

unread,
May 24, 2011, 4:33:03 PM5/24/11
to seleniu...@googlegroups.com
I got this to work by extending the RemoteWebDriver class and implementing the TakesScreenshot class. Substitute that IP address with your remote server... tested running on Ubuntu remotely connecting to a Mac.

public class RemoteScreenShotTest {
public class ScreenShotRemoteWebDriver extends RemoteWebDriver implements TakesScreenshot {
public ScreenShotRemoteWebDriver(URL url, DesiredCapabilities dc) {
super(url, dc);
}

@Override
public <X> X getScreenshotAs(OutputType<X> target)
throws WebDriverException {
if ((Boolean) getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT)) {
   return target.convertFromBase64Png(execute(DriverCommand.SCREENSHOT).getValue().toString());
}
return null;
}
}
@Test(groups = "unit")
public void remoteWebDriverScreenshotTest() throws Exception {
     
       DesiredCapabilities dc = new DesiredCapabilities();
       dc.setBrowserName("firefox");
       dc.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
       ScreenShotRemoteWebDriver rwd = new ScreenShotRemoteWebDriver(new URL("http://10.0.96.62:4444/wd/hub"), dc);
       
       
       File f = rwd.getScreenshotAs(OutputType.FILE);
       File o = new File("target/screenshot.png");
       FileUtils.copyFile(f, o);
       
       rwd.quit();
}
}


Mango Fever

unread,
May 31, 2011, 12:09:14 AM5/31/11
to Selenium Users
It works!

Thanks, Luke.

On May 25, 5:33 am, Luke Inman-Semerau <luke.seme...@gmail.com> wrote:
> I got this to work by extending the RemoteWebDriver class and implementing
> the TakesScreenshot class. Substitute that IP address with your remote
> server... tested running on Ubuntu remotely connecting to a Mac.
>
> public class RemoteScreenShotTest {
>  public class ScreenShotRemoteWebDriver extends RemoteWebDriver implements
> TakesScreenshot {
> public ScreenShotRemoteWebDriver(URL url, DesiredCapabilities dc) {
> super(url, dc);
>
> }
>
> @Override
> public <X> X getScreenshotAs(OutputType<X> target)
> throws WebDriverException {
> if ((Boolean)
> getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT)) {
>     return
> target.convertFromBase64Png(execute(DriverCommand.SCREENSHOT).getValue().to String());}
> return null;
> }
> }
>
>  @Test(groups = "unit")
> public void remoteWebDriverScreenshotTest() throws Exception {
>
>        DesiredCapabilities dc = new DesiredCapabilities();
>        dc.setBrowserName("firefox");
>        dc.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
>        ScreenShotRemoteWebDriver rwd = new ScreenShotRemoteWebDriver(new
> URL("http://10.0.96.62:4444/wd/hub"), dc);
>
>        rwd.get("http://groups.google.com/group/selenium-users/browse_thread/thread/34...
Reply all
Reply to author
Forward
0 new messages