开发者

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

开发者 https://www.devze.com 2023-03-17 07:33 出处:网络
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

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 instr开发者_开发知识库uction about how to do that? Thanks.


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( ... );


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.


I think this is the best way:

public<T> Object getScreenshotAs(OutputType<T> outputType) {
    Augmenter augmenter = new Augmenter(); 
    TakesScreenshot ts = (TakesScreenshot) augmenter.augment(driver);
    return ts.getScreenshotAs(outputType);
}


Here's another solution: https://groups.google.com/d/topic/selenium-users/NLHXlhPrADs/discussion


The RemoteWebDriver does not implement the TakesScreenShot and the methods described here to cast the instance of RemoteWebDriver to TakesScreenShot will cause a ClassCastException. I'll see if I can find a solution as I'm interested in doing the same.

The Augmenter will "enhance the interfaces implemented by this instance of WebDriver iff that instance is a RemoteWebDriver". Depending on how the RemoteWebDriver is configured and how the selenium server is running on the remote host, it may be possible to get a screen shot in using the org.openqa.selenium.remote.Augmenter.

I've configured the RemoteWebDriver to use DesiredCapabilities.htmlUnit() capabilities and this gives an ClassCastError. If the RemoteWebDriver is configured with the capabilities of a driver that implements the TakesScreenshot interface, then a ClassCastException may not occur, though I have yet to test this.

From the TakesScreenshot interface, the known implementing drivers are: AndroidDriver, AndroidWebDriver, ChromeDriver, EventFiringWebDriver, FirefoxDriver, InternetExplorerDriver, IPhoneDriver, IPhoneSimulatorDriver, and SafariDriver

0

精彩评论

暂无评论...
验证码 换一张
取 消