Selenium Grid (RemoteWebDriver) 与 Cucumber jvm SharedDriver 的使用

Selenium Grid (RemoteWebDriver) usage with Cucumber jvm SharedDriver

我使用 SharedDriver 实现了 cucumber-jvm picocontainer,并且在本地运行良好。我想使用配置良好的 Selenium Grid,但我不知道应该如何修改 Shareddriver class 才能使用 RemoteWebDriver 而不是 WebDriver 并连接到 Selenium GRID。

new RemoteWebDriver(new("http://..../wd/hub"), capability); 不起作用,因为我需要抛出 MalFormedExceptionREAL_DRIVER 是一个静态字段。

有什么想法吗?谢谢!

public class SharedDriver extends EventFiringWebDriver {

    private static final WebDriver REAL_DRIVER = WebDriverFactory.internetExplorerWebDriver();
    private static final Thread CLOSE_THREAD = new Thread() {
        @Override
        public void run() {
            REAL_DRIVER.close();
        }
    };

    static {
        Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
    }


    public SharedDriver() {
        super(REAL_DRIVER);
    }

    @Override
    public void close() {
        if(Thread.currentThread() != CLOSE_THREAD) {
            throw new UnsupportedOperationException(
                    "WebDriver should not close!"
            );
        }
        super.close();
    }

    @Before
    public void deleteAllCookies() {
        manage().deleteAllCookies();
    }

    @After
    public void embedScreenshot(Scenario scenario) {
        ...
    }

}

WebDriverFactory:

class WebDriverFactory {
    static {
        System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
    }

    static WebDriver internetExplorerWebDriver() {
        DesiredCapabilities returnCapabilities = DesiredCapabilities.internetExplorer();
            System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
        returnCapabilities.setCapability("requireWindowFocus", true);
        returnCapabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false);
        returnCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
        returnCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        returnCapabilities.setCapability("ignoreZoomSetting", true);
        return new InternetExplorerDriver(returnCapabilities);

    }

您可以将 return 语句包装到 try..catch 块中,并且 return null 以防抛出异常。

class WebDriverFactory {
static {
    System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
}

static WebDriver internetExplorerWebDriver() {
    DesiredCapabilities returnCapabilities = DesiredCapabilities.internetExplorer();
        System.setProperty("webdriver.ie.driver", "src/test/resources/webDrivers/IEDriverServer.exe");
    returnCapabilities.setCapability("requireWindowFocus", true);
    returnCapabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false);
    returnCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
    returnCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    returnCapabilities.setCapability("ignoreZoomSetting", true);

    try {
        return new RemoteWebDriver(new URL("http://www.google.com"), returnCapabilities);
    } catch (MalformedURLException e) {
        return null;
    }

}

然后,检查 REAL_DRIVER 值是否不是 null