Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get screenshot from RemoteWebDriver server instead of local FirefoxDriver?

I am running Selenium WebDriver test on a remote PC from my laptop(java client), by using RemoteWebDriver. But RemoteWebDriver haven't provided screenshot API to directly get a screenshot of remote PC. Googled a lot but found seems need use to Json API to get it from remoteWebDriver server directly. Anyone can give me some instruction about how to do that? Thanks.

like image 826
Wahaha2001 Avatar asked Jul 06 '11 03:07

Wahaha2001


2 Answers

The RemoteWebDriver must be augmented before you can use the screenshot capability. As you have no doubt already found, attempting to cast without augmenting results in an exception.

WebDriver driver = new RemoteWebDriver( ... );
driver           = new Augmenter().augment( driver );
( (TakesScreenshot)driver ).getScreenshotAs( ... );

like image 135
Mike Kwan Avatar answered Nov 15 '22 17:11

Mike Kwan


I was able to get this working...here is what you need to do:

1) Create a new class file in a Util directory or somewhere

package com.util;

import java.net.URL;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteWebDriver;

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;

    }

}

2) Then...where ever you start the RemoteWeDriver, replace with this code:

driver = new ScreenShotRemoteWebDriver(new URL(-PUT YOUR HUB URL HERE-),cap);

Your screenshots will be stored locally.

Hope this helps.

like image 20
zerotres Avatar answered Nov 15 '22 18:11

zerotres