[Cucumber-JVM] How to set environment and url from command line

4,185 views
Skip to first unread message

knoll2roll

unread,
Mar 10, 2014, 6:09:08 AM3/10/14
to cu...@googlegroups.com
Hi Guys,

I'm a bit stuck here. I'm using cucumber-jvm, selenium-webdriver and maven.

My project has a number of different environments. And I would like to set the environment (URL) from the command line using a switch or something that calls a property file. I'm really struggling to do this so if anyone knows any examples they can show me that would be great.

Also i would like to set the driver from the command line as well. How can i do this?

Robert

unread,
Mar 10, 2014, 4:56:23 PM3/10/14
to cu...@googlegroups.com
I tend to use system properties.  I typically run something like (maven command) 

mvn clean verify -Dbrowser=firefox -DbaseUrl=http://my.base.url.to.application 

And, then within the code of the test framework, I extract the system properties.  It may not be the most ideal solution, but it works for me ;-)

Roberto Lo Giacco

unread,
Mar 11, 2014, 4:46:59 AM3/11/14
to cu...@googlegroups.com
That's is definitely a good advice, another one being the use of Maven profiles to switch between the environments:

<profiles>
  <profile>
    <id>dev</id>
    <properties>
       <base.url>http://my.base.url.to.application</base.url>
    </properties>
  </profile>
  <profile>
    <id>qa</id>
    <properties>
       <base.url>http://qa.base.url.to.application</base.url>
    </properties>
  </profile>
  <profile>
    <id>firefox</id>
    <properties>
       <driver.class>org.openqa.selenium.firefox.FirefoxDriver</driver.class>
    </properties>
  </profile>
  <profile>
    <id>chrome</id>
    <properties>
       <driver.class>org.openqa.selenium.chrome.ChromeDriver</driver.class>
    </properties>
  </profile>
   ...
</profiles>

Then you can switch among those with

mvn clean verify -P qa,chrome

You can set default profiles as well, to use in case your users forget to provide one. This solution adds a level of indirection to the one suggested above, so that you don't need your users to know the exact URLs: nothing too fancy.

knoll2roll

unread,
Mar 11, 2014, 5:11:17 AM3/11/14
to cu...@googlegroups.com
Hi, many thanks for the reply, it really is appreciated.

I played around with maven profiles, but i dont think it fits with what i want to do.
the URL structure is:

http://uk + environmentName + something.com

so the url is split up into 3 parts. I basically want to set the environmentName from the command line at run time. This will allow the tests to run on dev, qa, prod etc... without having to change anything within the tests.

has anyone every done anything like that?

Much appreciated,
Knoll

knoll2roll

unread,
Mar 11, 2014, 7:46:14 AM3/11/14
to cu...@googlegroups.com
would anyone know of any example projects on GitHub or similar that have done somethign similar. turning into a blocker for using Cucumber-JVM with WebDriver for me...

Roberto Lo Giacco

unread,
Mar 11, 2014, 8:59:15 AM3/11/14
to cu...@googlegroups.com
Il giorno martedì 11 marzo 2014 10:11:17 UTC+1, knoll2roll ha scritto:
Hi, many thanks for the reply, it really is appreciated.

I played around with maven profiles, but i dont think it fits with what i want to do.
the URL structure is:

http://uk + environmentName + something.com

so the url is split up into 3 parts. I basically want to set the environmentName from the command line at run time. This will allow the tests to run on dev, qa, prod etc... without having to change anything within the tests.

has anyone every done anything like that?

I would still go for the Maven profile:

<profiles>
  <profile>
    <id>dev</id>
    <properties>
       <base.url>http://ukdev.something.com</base.url>
    </properties>
  </profile>
  <profile>
    <id>qa</id>
    <properties>
       <base.url>http://ukqa.something.com</base.url>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
       <base.url>http://ukprod.something.com</base.url>
    </properties>
  </profile>
</profiles>
 
Then you have

mvn clean verify -P dev

instead of

mvn clean verify -Denv=dev

Not much difference, but still with a level of indirection

knoll2roll

unread,
Mar 11, 2014, 9:24:58 AM3/11/14
to cu...@googlegroups.com
Just a quick question Roberto,

So if want to go to dev and extend the url in my test how would i do that?
e.g.
<profiles>
  <profile>
    <id>dev</id>
    <properties>
       <base.url>http://ukdev.something.com</base.url>
    </properties>
  </profile>
</profiles>

is defined in my pom.xml.


from my test how do i call in the base.url property. something like this:
public WebElement navigateToAnotherPage() {
    return @driver.goto('${base.url} + /pageLink/anotherPageLink');
}

does that make sense?

Kind regards,
Knoll

Roberto Lo Giacco

unread,
Mar 11, 2014, 10:26:49 AM3/11/14
to cu...@googlegroups.com
Il giorno martedì 11 marzo 2014 14:24:58 UTC+1, knoll2roll ha scritto:
Just a quick question Roberto,

So if want to go to dev and extend the url in my test how would i do that?
e.g.
<profiles>
  <profile>
    <id>dev</id>
    <properties>
       <base.url>http://ukdev.something.com</base.url>
    </properties>
  </profile>
</profiles>

is defined in my pom.xml.


from my test how do i call in the base.url property. something like this:
public WebElement navigateToAnotherPage() {
    return @driver.goto('${base.url} + /pageLink/anotherPageLink');
}

System.getProperty("base.url") will return the property value, in case you picked the dev profile it will be "http://ukdev.something.com"

knoll2roll

unread,
Mar 11, 2014, 11:37:38 AM3/11/14
to cu...@googlegroups.com
I added this into my pom.xml:
<profiles>
  <profile>
    <id>dev</id>
    <properties>
       <base.url>http://uk.dev</base.url>
    </properties>
  </profile>
</profiles>

I tried your solution so i entered this in my test:

String xxx = System.getProperty("base.url");
driver.get(xxx + "ENV.something.somethingelse.com");


but i got the following error:
Caused by: java.net.MalformedURLException: no protocol: nullENV.something.somethingelse.com

Ive tried lots of different ways to avoid this error but it doesnt seem tow ork.

I ran the test by running: mvn clean verify -P dev from the command line.

Thanks,
Knoll

Roberto Lo Giacco

unread,
Mar 11, 2014, 12:38:30 PM3/11/14
to cu...@googlegroups.com
On Tue, Mar 11, 2014 at 4:37 PM, knoll2roll <knoll...@gmail.com> wrote:
I added this into my pom.xml:
<profiles>
  <profile>
    <id>dev</id>
    <properties>
       <base.url>http://uk.dev</base.url>
    </properties>
  </profile>
</profiles>

I tried your solution so i entered this in my test:

String xxx = System.getProperty("base.url");
driver.get(xxx + "ENV.something.somethingelse.com");


but i got the following error:
Caused by: java.net.MalformedURLException: no protocol: nullENV.something.somethingelse.com

Ive tried lots of different ways to avoid this error but it doesnt seem tow ork.

I ran the test by running: mvn clean verify -P dev from the command line.

I believe the bit you are missing is the following

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<base.url>${base.url}</base.url>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

This surefire config will pass the ${base.url} maven property value as base.url system property when running your tests (I used color to distinguish the two matches. You can decide to perform your mixin at this stage if you prefer.

knoll2roll

unread,
Mar 12, 2014, 6:42:21 AM3/12/14
to cu...@googlegroups.com
So if i have this in my pom.xml:

<profiles>
  <profile>
    <id>dev</id>
    <properties>
       <base.url>http://uk.dev</base.url>
    </properties>
  </profile>
</profiles>

<build>
  <plugins>
  <plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
  <systemPropertyVariables>
  <base.url>${base.url}</base.url>
  </systemPropertyVariables>
  </configuration>
  </plugin>
  </plugins>
</build>

then why does the ${base.url} appear as an error. I dont think it is picking up the property as when i run the test using:

String xxx = System.getProperty("base.url");
driver.get(xxx + "ENV.something.somthingelse.com");
System.out.println("THIS IS THE URL FROM THE PROPERTIES FILE:  " + driver.getCurrentUrl());

xxx gets returned back as null.
as the system.out.println returns this:
THIS IS THE URL FROM THE PROPERTIES FILE:  about:blank

Any ideas why?

You have been so much help thank you,
Knoll

Roberto Lo Giacco

unread,
Mar 12, 2014, 7:30:05 AM3/12/14
to cu...@googlegroups.com
How are you executing the code snippet above? Is it within a test executed by surefire? The plugin configuration above is instructing the surefire plugin to set a system property before executing the tests. If you execute the same snippet of code outside the Maven lifecycle (for example by running the test within Eclipse) then the system property is not set and you'll get a null value. The same will happen if you don't provide a default active profile or switch on a profile.

If you place the snippet above within a method of a JUnit test case you'll get the proper value printed out.

To have the same property set within your cucumber step definitions you can use the tipical JUnit wrapper class (https://bitbucket.org/rlogiacco/atdd/src/36eb941e499499c8ef4e134c957a9136cd807e65/atdd-example/src/test/java/org/agileware/atdd/JUnitWrapper.java?at=master) to kick your automated tests.

If you are using another method to start your Cucumber tests other than the surefire plugin then you might want to replace the above surefire configuration with another Maven plugin which is capable of setting system properties in a more generalized way: http://mojo.codehaus.org/properties-maven-plugin/index.html, in particular the set-system-properties goal.

knoll2roll

unread,
Mar 12, 2014, 7:43:18 AM3/12/14
to cu...@googlegroups.com
Yes now i understand. Ill make sure I only run from the command line using:
mvn clean test -P dev.

When i so a System.out.println(xxx + "ENV.something.somethingelse.com"); the full URL is getting printed correctly. 

The problem is when i do a:
driver.get(xxx + "ENV.something.somethingelse.com");

it is then returning about:blank as the url.

So i need to figure out why the url is being formed in the System.out and not getting formed and exectued in the driver.get() method.

Really appreciate all the help you have given me over the past few days. it really is appreciated.

Kind regards,
Knoll

Sébastien LE CALLONNEC

unread,
Mar 12, 2014, 8:36:33 AM3/12/14
to cu...@googlegroups.com
Hi,

Le Mardi 11 mars 2014 11h46, knoll2roll <knoll...@gmail.com> a écrit :
would anyone know of any example projects on GitHub or similar that have done somethign similar. turning into a blocker for using Cucumber-JVM with WebDriver for me...
I wrote a blog post doing something like that using Maven some time ago:

The example is in GitHub:

Regards,
Sébastien.

Roberto Lo Giacco

unread,
Mar 12, 2014, 9:04:31 AM3/12/14
to cu...@googlegroups.com
Il giorno mercoledì 12 marzo 2014 12:43:18 UTC+1, knoll2roll ha scritto:
Yes now i understand. Ill make sure I only run from the command line using:
mvn clean test -P dev.

When i so a System.out.println(xxx + "ENV.something.somethingelse.com"); the full URL is getting printed correctly. 

The problem is when i do a:
driver.get(xxx + "ENV.something.somethingelse.com");

it is then returning about:blank as the url.

So i need to figure out why the url is being formed in the System.out and not getting formed and exectued in the driver.get() method.
 
Can't help you further without comparing the working code to the non working one.... I suggest you link a Gist with your code

knoll2roll

unread,
Mar 13, 2014, 5:55:38 AM3/13/14
to cu...@googlegroups.com
Hi Roberto,

Many thanks for your help. the problem ended up being a spelling mistake in the url that I was trying to go to.
Once that got sorted the tests ran from the command line and they all passed.

I couldnt have done it without you.

Although, for some weird reasons, I dont see any of the feature file steps getting printed on the command line. all i see is, how many tests passed and failed. weird.

Kind regards,
knoll
Reply all
Reply to author
Forward
0 new messages