Search code examples
javaselenium-webdriverselenium-gridcucumber-jvm

Selenium Grid (RemoteWebDriver) usage with Cucumber jvm SharedDriver


I implemented cucumber-jvm picocontainer with SharedDriver and works well locally. I would like to use Selenium Grid which has been configured well but I don't know how should I modify Shareddriver class in order to use RemoteWebDriver instead of WebDriver and connect to Selenium GRID.

new RemoteWebDriver(new("http://..../wd/hub"), capability); doesn't work because I need to throw MalFormedException and REAL_DRIVER is a static field.

Any idea? Thanks!

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

    }

Solution

  • You can wrap the return statement into a try..catch block and return null in case of exception is being thrown.

    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;
        }
    
    }
    

    then, check that the REAL_DRIVER value is not null.