Execute only specific examples in a Scenario Outline - java

We're looking to better manage test data using Cucumber in our Java test automation framework. For a Scenario Outline, we're looking to tabulate test parameters categorized by the applicable environment in which they will run.
For example,
Scenario Outline: Login into application
Given I am on the homepage in the <environment>
When I enter my <user>
And I enter my <pass>
Then I am taken to the homepage
Examples:
|user |pass |environment|
|test |test1 |local |
|retest |retest1 |sit |
|prodtest|prodtest1|production |
So, when the above scenario is executing in, for example, the SIT environment, only the 2nd example will be picked up, and not the first and third.
Can this level of execution be accomplished?

You can get this done by splitting up your examples table into two and using tags on them... Then run the test with the tags to filter in cucumberoptions.
#others
Examples:
|user |pass |environment|
|test |test1 |local |
|prodtest|prodtest1|production |
#sit
Examples:
|user |pass |environment|
|retest |retest1 |sit |

That is not what scenario outlines are designed for.
You can write separate scenario's and then use tags on each one that you can then pass in at runtime which tag you want to run.

Related

How to extract Scenario outline Examples values in After hook

For reporting purposes I need to determine Scenario outline Examples in Cucumber (JVM6).
Looks like there is no obvious way to do it other than doing it directly in StepDefs.
Lets assume I have scenario like this:
Scenario Outline: Name
When I call cat by name "<catName>"
Then It responses
Examples:
| catName |
| Fluffy |
| Snowy |
I would like to do something like:
#After
public void report(Scenario scenario) {
List<Examples> examples = scenario.getExamples();
}
Unfortunately adding examples value extraction StepDefs is not an option. As well as adding new technical step.

Unable to write a simple step passing a single string as a parameter

I work with JBehave on a daily basis, but have been tasked with working on a project that uses Cucumber. In order to add a custom reporting class functionality to that project, I need to add two steps, one at the start of the feature (story) and another at the start of the scenario. I merely want to pass to the application a description of the feature/story and the scenario to be passed to the reporting module. I know that cucumber can access the scenario name through code, but that would only resolve one of the two lines - I would still need to have another one that passes the description of the feature/story.
What I've tried in the feature file:
Feature: Ecolab BDD Test Automation Demo
Scenario Outline: User can login and logout from the landing page
Given story "EcolabWebDemo_TestCases - Ecolab BDD Test Automation Demo"
Given scenario "User can login and logout from the landing page"
Given I am on the Ecolab landing page
The corresponding code for the two added Given statements at the beginning above:
#Given("^story {string}$") // \"(\\S+)\"
public void givenStory(String storyName) {
test.initStory(storyName); // will show on report in Features column
}
#Given("^scenario {string}$") // \"(\\S+)\"
public void givenScenario(String scenarioName) {
test.initScenario(scenarioName);
}
The commented regex patterns afterwards are the suggested ones I should try but do not seem to work either.
The current configuration at least seems to "find" the steps but reports:
cucumber.runtime.CucumberException:
java.util.regex.PatternSyntaxException: Illegal repetition near index
13 ^the scenario {string}$
So that's obviously not the solution. The regex used instead of {string} simply does not find a match and does not run.
regex is absolute Greek to me, not sure why it can't just be simple like the {string} option implied it would be in the cucumber documentation. I've been searching on-line for guidance for the better part of two days to no avail, I'm apparently not even sure what to be searching for.
Based on Grasshopper's suggestion, I updated the version of Cucumber from 1.2.0 to 1.2.5. I was prepared to change the pom.xml to use the 3.x versions but tried the latest of the specified libraries first, and it did report after an attempted run what the correct regex should be for the two steps I added.
#Given("^story \"([^\"]*)\"$")
and
#Given("^scenario \"([^\"]*)\"$")
Now that the project has a version that seems to recognize strings and also reports the missing steps, the project now runs as intended.
Thanks for your help, Grasshopper.

Feature file with scenario outline and quotes in data

I have Scenario in a feature file where i am given a message to verify with quotes. Please look in the following example:
Scenario Outline: Testing xyz
Give you are a valid user
When you log in to the System as Richard
Then you will be welcomed with "<Msg>"
Examples:
| Msg |
| Welcome to this application "Richard" |
Sample stub method for the #Then part:
#Then
public void Welcome to this application Richard(String arg){
System.out.println(arg);
}
OUTPUT:
Welcome to this application
Notice that the arg parameter does not contain word "Richard" with quotation. But business rules say the message should have the quotes around the name.
Can any one help me to write the feature file so that i get the word "Richard" in my parameter with the quotes?
I am using Java + Selenium + Cucumber

how to run junit tests on a examples file

I have a method under test which gets as input 2 strings and returns a double.
Instead of writing a separate UT for each of them like this:
public void test1() throws Exception {
double answer = nameSimilarity.similarity("aaaa", "abbba");
assertThat(answer, greaterThan(THRESHHOLD));
}
I want to write a write an input batch file like this:
string1 - string 2 - expect result to be greater than THRESHHOLD
aaaa - abbba - True
cccc - abbba - True
cccc - zzzzz - False
how do you suggest I'll read the file, parse it and run a unit test on each row?
Is there any built in such functionality in junit with any convention ?
You can take a look behaviour driven development cucumber, which can support such kind of testing sample data called "Data Tables".
For example:
Scenario Outline: Email confirmation
Given I have a user account with my name "Jojo Binks"
When an Admin grants me <Role> rights
Then I should receive an email with the body:
"""
Dear Jojo Binks,
You have been granted <Role> rights. You are <details>. Please be responsible.
-The Admins
"""
Examples:
| Role | details |
| Manager | now able to manage your employee accounts |
| Admin | able to manage any user account on the system |
there is no such functionality in junit. also, in Junit you normally run each test case separately and give each a different name. Junit discourages running many tests in the same test case (it will fail if only one out of 20 tests succeed or 19 of them fail).
Your test case should have the general structure of:
public void testTooManyCases() {
while get a line and !eof // catch exceptions and in finally close the file
parse the line
//e.g: String[] parts = line.split(","); //use comma to separate in the input file
//calculate the answer for the info in the line
//e.g: double answer = nameSimilarity.similarity(parts[0], parts[1]);
//assert
//e.g.: assertTrue((answer > THRESHHOLD) == new Boolean(parts[2]).booleanValue());
}
your file should have in each line something like:
aaaa,abbba,true
cccc,zzzzz,false

Filtering with multiple metafilters in JBehave

Situation:
In my current project we are running all kinds of different JBehave stories. Every ".story" file is related to a product and a flow.
Example:
xyz-cellphone-call.story would be the story describing making a phonecall with a cellphone.
xyz-phone-call.story would be the story describing making a phonecall with a fixed-line phone.
xyz-cellphone-browse.story would be the story describing browsing the internet with a cellphone.
My question:
In Jbehave you can add metaFilters to filter on the stories based on meta tags. Assume the tags are #product & #action. (#product cellphone, #action call).
Would it be possible to pass a filter to run the JBehave stories concerning both the phone & cellphone stories, if yes, what would be the syntax?
I've tried adding the following filters (none of which work):
+product cellphone +product phone
+product cellphone|phone
+product cellphone,phone
Same for actions.
Is it possible to filter on multiple meta-tags?
Yes it is possible.
In the API docs you will find this information:
A filter is uniquely identified by its String representation which is
parsed and matched by the MetaFilter.MetaMatcher to determine if the
Meta is allowed or not.
The MetaFilter.DefaultMetaMatcher interprets the filter as a sequence
of any name-value properties (separated by a space), prefixed by "+"
for inclusion and "-" for exclusion. E.g.:
MetaFilter filter = new MetaFilter("+author Mauro -theme smoke testing
+map *API -skip"); filter.allow(new Meta(asList("map someAPI")));
The use of the MetaFilter.GroovyMetaMatcher is triggered by the prefix
"groovy:" and allows the filter to be interpreted as a Groovy
expression.
MetaFilter filter = new MetaFilter("groovy: (a == '11' | a == '22')
&& b == '33'");
So probably if you play with the conditions, you will get your run configuration customized.
Try this example:
mvn clean install -P -Djbehave.meta.filter="myCustomRunConf:(+product && +action)"
More info amout the MetaFilter class in the API docs:
http://jbehave.org/reference/stable/javadoc/core/org/jbehave/core/embedder/MetaFilter.html
I guess there is easier solution for you using groovy
http://jbehave.org/reference/stable/meta-filtering.html
In your case it would be
-Dmetafilter="groovy: "product=='cellphone' && action=='call'"
I tried it as "-Dmetafilter=groovy:t2 && t3" for this feature file
Meta:
#t1
Narrative:
As a user
I want to blah-blah-blah
Scenario: test 1
Meta:
#t2
Given I am on home page
Scenario: test 2
Meta:
#t2
#t3
Given I am on home page
Scenario: test 3
Meta:
#t3
Given I am on home page
Only test 2 scenario is executed in this case
How about:
mvn clean install -P -Djbehave.meta.filter = "+product cellphone&&phone"

Categories