Setting geb.env in GebConfig

736 views
Skip to first unread message

CaptainCC

unread,
Jul 2, 2018, 2:36:05 PM7/2/18
to Geb User Mailing List
I'm using "environments" in GebConfig to set the driver when I use "-Dgeb.env=driver_name" on the command line. That's working fine.

What I WANT to do is to set the geb.env system property in the GebConfig file for running the test via an IDE, i.e.:

System.setProperty("geb.env", getProperty("geb.env", "chrome"))

This seems to work, as I get "chrome" when I do:

println System.getProperty("geb.env")

However, the property seems to be ignored by "environments" and it uses the default driver instead.

I can't figure out what is going on. The value of geb.env seems to be getting set correctly, but not actually used unless it comes from the command line.

Marcin Erdmann

unread,
Jul 2, 2018, 4:20:03 PM7/2/18
to Geb User Mailing List
You've got a chicken and egg problem. The geb.env property is read before the config script is executed so when you're setting it in GebConfig.groovy it's already too late for it to be effective. Another issue is that the config script should be just that, a passive config script, and not change the environment in any way.

There are two possible solutions I can think of:
- setting the environment variable in the IDE test run configuration, e.g. in IntelliJ: Run -> Edit configurations -> JUnit -> YourTestRunningConfiguration or  Run -> Edit configurations -> Defaults -> JUnit
- moving configuration for whatever you want to be the default (chrome in your example) to the top level of the script, outside of the environments block - it will be used by default when geb.env system property is not set

--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+unsubscribe@googlegroups.com.
To post to this group, send email to geb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/5c123096-96ff-488e-ae4d-b386e9908af5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

CaptainCC

unread,
Jul 3, 2018, 10:33:46 AM7/3/18
to Geb User Mailing List
Thank you. That makes sense. What I ended up doing is using a switch in the driver block outside of the environment section and using a local variable to choose the browser. For example:

def localBrowser = "chrome"

driver
= {
 
switch (localBrowser) {
     
case "chrome":
     
return new ChromeDriver()
 
...


Reply all
Reply to author
Forward
0 new messages