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

前端 未结 5 1950
谎友^
谎友^ 2020-12-16 03:16

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

相关标签:
5条回答
  • 2020-12-16 03:24

    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.

    0 讨论(0)
  • 2020-12-16 03:24

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

    0 讨论(0)
  • 2020-12-16 03:25

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

    0 讨论(0)
  • 2020-12-16 03:28

    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 讨论(0)
  • 2020-12-16 03:33

    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);
    }
    
    0 讨论(0)
提交回复
热议问题