[cucumber] passing maven parameters (-D) to cucumber scenario

1,038 views
Skip to first unread message

King Raj

unread,
Nov 18, 2013, 5:14:16 PM11/18/13
to cu...@googlegroups.com
I have written Login Scenario in a feature file, I need to run the same test for UK, US & AUS domain or run for UK & US. I want this domain value to be configurable in the test parameter I pass.

Is there any way in cucumber to pass maven parameter to Scenario's?

Regards,
Arsan



Richard Paul

unread,
Nov 19, 2013, 3:37:40 AM11/19/13
to cu...@googlegroups.com
Hi Arsan,

Assuming you are using Java, accessing parameters passed to the JVM is simply a matter of using the Java API System.getProperty("my.property");
Often I'll encapsulate these properties in a configuration object which can be injected as a collaborator to step definitions/other object that require it.

e.g.

public class EnvironmentConfiguration {
   
public Locale getLocale() {
       
String locale = System.getProperty("myapp.locale");
       
// convert to locale and return
   
}
}


In a step definition that required it (assuming Java's PicoContainer bindings) I'd then do

class LoginStepdefs {


   
public class LoginStepdefs(EnvironmentConfiguration environmentConfiguration) {
       
this.environmentConfiguration = environmentConfiguration;
   
}


   
@Given("some step")
   
public void some_step() {
       
this.environmentConguration.getLocale();  // do something with it
   
}
   
}

I hope that helps,
Richard

King Arsan

unread,
Nov 19, 2013, 6:08:35 AM11/19/13
to cu...@googlegroups.com
Hi Richard,

Thanks for your reply. That looks good for running the test for single domain. Apologies for not having clear example.

My current feature file looks like this

--------------------------------------------------------------

Scenario Outline : Verify Login journey

Given I am in <Domain> login page
When I enter my email te...@gmail.com to login
Then I can see the home page

Examples:

| Domain |      
|    UK    |
|    US    |
|   AUS   |

I don't want to hard code the domain values in the feature file. If I need to run the test for UK domain, then it's very tough.

I want the domain values to be passed as parameter to the Scenario to run multiple times. 

Any other thoughts?

Regards,
Arsan

Paolo Ambrosio

unread,
Nov 19, 2013, 8:28:34 AM11/19/13
to cu...@googlegroups.com
Please reply in line and prefix the subject with [JVM] next time, as
stated at the end of each email or on the group homepage.

On Tue, Nov 19, 2013 at 11:08 AM, King Arsan <rajku...@gmail.com> wrote:

> Hi Richard,
>
> Thanks for your reply. That looks good for running the test for single
> domain. Apologies for not having clear example.
>
> My current feature file looks like this
>
> --------------------------------------------------------------
>
> Scenario Outline : Verify Login journey
>
> Given I am in <Domain> login page
> When I enter my email te...@gmail.com to login
> Then I can see the home page
>
> Examples:
>
> | Domain |
> | UK |
> | US |
> | AUS |
>
> I don't want to hard code the domain values in the feature file. If I need
> to run the test for UK domain, then it's very tough.

Scenario outlines are meant to run scenarios in slightly different
situations, not to parametrize them. I believe Richard's solution is
the best way of testing all three domains (or even only one). The only
thing I would change is to use a single system property to load all
the others from a property file (e.g. passing DOMAIN=US would load the
us.properties file, etc.). On top of it, you can write two sets of
scenarios:

1) Scenarios that express common functionalities on every domain (not annotated)
2) Specific features to a domain (annotated with @us, @uk and/or @aus)

A couple of sample scenarios follow.


Scenario: Successful login journey
Given I am not logged in
When I go to the login page
And I enter my credentials correctly
Then I can see the home page

@uk
Scenario Outline: Queen's birthday login advert
Given it's <time> the Queen's birthday
When I go to the home page
Then I should <see_or_not_see> the Queen's birthday offer banner

Examples:
|time|see_or_not_see|
|15 days before|not see|
|14 days before|see|
||see|
|1 day after|not see|


As Richard wrote, in the step definitions you would know where to
point the browser to for the "I go to the login page" step based on a
JVM variable.

In the continuous integration server, you would then be able to run
all three suites at once, decreasing feedback times. Adding a new
domain would be as simple as writing a new set of properties for it
and configuring the CI server.


Paolo
> --
> -- Rules --
>
> 1) Please prefix the subject with [Ruby], [JVM] or [JS].
> 2) Please use interleaved answers
> http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
> 3) If you have a question, don't reply to an existing message. Start a new
> topic instead.
>
> You received this message because you are subscribed to the Google Groups
> Cukes group. To post to this group, send email to cu...@googlegroups.com. To
> unsubscribe from this group, send email to
> cukes+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/cukes?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Cukes" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cukes+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages