Selenium Grid(RemoteWebDriver)与Cucumber jvm SharedDriver一起使用

时间:2018-01-05 20:27:23

标签: java selenium-webdriver selenium-grid cucumber-jvm

我使用SharedDriver实现了cucumber-jvm picocontainer,并在本地运行良好。我想使用配置良好的Selenium Grid,但我不知道如何修改Shareddriver类以便使用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);

    }

1 个答案:

答案 0 :(得分:1)

您可以将return语句包装到try..catch块中,并在发生异常时返回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