0

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
mordka
  • 392
  • 3
  • 11
  • take a look at http://stackoverflow.com/q/20095893/2504101 – olyv Nov 12 '14 at 11:35
  • thanks @olyv but in my case HtmlUnit works fine. I just wanted to set it in the way I do for all drivers like chrome, firefox, ie, phantomjs (by specifying class name in the property). But unfortunately htmlunit doesn't set JavaScript by default during initialization. – mordka Nov 13 '14 at 13:30

1 Answers1

1

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.

erdi
  • 6,944
  • 18
  • 28
  • You're right, I found a place in Geb source code, which is responsible for that: `properties.getProperty("geb.driver") ?: readValue("driver", null)` I decided to workaround this by passing empty `geb.driver` property and setting driver using closure in Geb config script. This allows me to set `geb.driver` for all drivers except of HtmlUnit. – mordka Nov 13 '14 at 13:20