Geb overwrite web driver

791 views Asked by At

I'm running my tests using gradle which is specifying JVM properties in the following manner:

-Dgeb.env=dev -Dgeb.driver=org.openqa.selenium.htmlunit.HtmlUnitDriver

This sets driver and configures environment in Geb config script. I want to enable JavaScript for HtmlUnit after specifying it in environment property. To be more general, I want to overwrite driver in the config script (I'm not interested in setting driver in every test's setup() block)

I tried putting the following code snippet in Geb config script:

if (System.getProperty("geb.driver")=="org.openqa.selenium.htmlunit.HtmlUnitDriver") {
    driver = {
        println "Setting JavaScript"
        def driver = new HtmlUnitDriver()
        driver.setJavascriptEnabled(true)
        driver
    }
}

This prints out the message but the tests fail due to

java.lang.UnsupportedOperationException: Javascript is not enabled for this HtmlUnitDriver instance
1

There are 1 answers

1
erdi On BEST ANSWER

I'm a bit surprised that you're seeing Setting JavaScript printed out but what happens is that your driver closure doesn't get called because if you use geb.driver system property then Geb creates the driver for you internally based on that. I suggest you either use the env to create the driver in your GebConfig.groovy or use a different system property than geb.driver to ask for a given driver impl to be used and manage driver creation yourself.