0

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

    }
Eugene S
  • 6,709
  • 8
  • 57
  • 91
brobee
  • 231
  • 1
  • 5
  • 25

1 Answers1

1

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.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
  • Strange, I tried to solve in this way, but it works now! Thanks! – brobee Jan 08 '18 at 19:34
  • I will ask another question, I do not want to devote it, but unfortunately it doesn't work however there is no compilation error. – brobee Jan 12 '18 at 16:57
  • Hi Eugene, please take a look at it: https://stackoverflow.com/questions/48231987/cucumber-jvs-selenium-grid-selenium-sessionnotcreatedexception-unable-to-cre CucumberJVM SharedDriver doesn't work if I wanna use RemoteWebdriver instead of WebDriver. Any suggestion would be appreciated! – brobee Jan 13 '18 at 20:26