Execute certain Examples from Scenario Outlines - cucumber

I have to execute certain Examples from Scenario Outlines.
Let's see the following example:
Feature: Temp
Scenario Outline: Test.Something.On.<environment>
When action is performed on "<environment>"
Examples:
|environment|
|lab |
|prod |
I would like to execute only the example with lab from the upper presented Examples.
I tried the followings:
Filter by line number: mvn clean test -Dcucumber.options="src/test/resources/features/Temp.feature:8" - this way only the lab scenario was executed, however this is not a long term solution;
Filter by name: mvn clean test -Dcucumber.options="name lab" - it works for Scenario but not for Scenario Outline even if the name of the scenario will be Test.Something.On.lab;
decompose the Scenario Outline in Scenarios and tag the proper scenarios with #lab and #prod - I hate code duplication and the subsequent maintenance, therefore I hope there is an another solution for this.
Any suggestions?
Big thanks.
Another sub-keywords: maven, java, cucumber-java8, cucumber-junit, junit

Use two examples in yoour scenario outline and tag each example in a similar way as you have done in your third option. That should give you the behaviour you are asking for.

Related

How to add pre-scenario for Scenario Outline in Jira Xray?

I want to add pre-steps to Scenario Outline as Scenario, but I don't want to add pre-condition. How can I do that in Jira Xray?
Some illustrative example:
Scenario:
Given: Open website
Then: Check URL
Scenario Outline:
When I click this <button>
Then Something happens with <this> element
I want to have some "background" for Scenario Outline because this is something that repeats in several tests but it turns out that for every step in S.Outline Background is repeating so I want to create a normal Scenario as "artificial" background. How to do that in Xray? Can I add something like pre-step? Pre-condition always creates "Background" in Gherkin.
I don't want to have several Scenario Outline scenarios with a lot of repeated steps.
This is more a Gherkin related question than Xray specific. There are no dependencies between Scenarios in Gherkin.
The way to have some initial Gherkin statements that apply to all to several Scenarios, is by having a Background in the corresponding .feature file.
In Xray this means that you need to have a Precondition, of Cucumber type, with those Gherkin statements. Then you need to associate it all Tests (Scenario/Scenario Outline types) that you want. Whenever exporting those tests, a .feature file will be created with a Background corresponding to the previous Precondition.
The rules for generating .feature files based on Test and Preconditions issues in Xray is detailed here.
Note: in Cucumber libraries there also hooks, that contain code that can be executed before/after tests. However, this is used for something that isn't described as behaviour and more for easing implementation, and doing things like test setup/cleanup.

Tag filter system - Cucumber

I am working on a project where there may be several configurations (site for customer A , site for customer B,...). Each configuration has potentially different interactions (it is rare).
So I wrote my code with scenarios for certain configurations.
#config1 #config2 #config3
Scenario: A
Given hello
#config1
Scenario: B
Given hello
Scenario: C
Given hello
The problem is that I can’t find a solution to say when I’m on config "config3" that I want scenario A and C.
I’ve tested a lot of combinations with ~#config3 or not #config3. But I can’t do what I want to do.
Is that even possible?
A big thanks for your help.
One thing is that you are trying to combine a scenario with tag and without a tag which might not even possible as per my knowledge.
One solution to this is that you can have a tag on scenario c and combine that with other tags.
Let us assume you have #tagc on scenario c. Then you can use that with other tags to make it work.
if you want to run scenario 1 and 3: (#config3 or #tagc) Scenario 1 and 2: (#config1)
Reference: https://cucumber.io/docs/cucumber/api/#tags

Can we call one scenario inside another scenario in cucumber?

I started working with behavior driven tool cucumber. Its a fun tool to use. While i was working on a problem. I came across that most of time, I am not reusing my code.
That's why I want to call a scenario from another scenario. I have searched but found nothing helpful. Can I do that ?
Another same question posted here on github
This may be what you're looking for: https://github.com/cucumber/cucumber/wiki/Calling-Steps-from-Step-Definitions
So there are a couple of things you can do. If you have a step you want to reuse like the following:
Given /^I log in as (.*)$/ do |name|
# ...
end
You can call it within another step like so:
Given /^(.*) is logged in$/ do |name|
step "I log in as #{name}"
end
You can also do the following within a step definition:
steps %Q{
Given I log in as #{name}
}
I came to the same question - and found this post. Maybe it is on purpose that you cannot call Scenarios from other Scenarios. The framework is based on that you think about creating practical Stepdefinitions, so they can be used many times. The basis is to think before creating steps...
I created own Steps vor Login-Method, Pagetransitions to search-page or new File etc..
So in many Scenarios i reuse these Steps - and then add new ones (that can be reused, too).
Now you can think about how big one step should be. You can size it as one action in the testobject or use it as a routine to come to a certain startpoint of your Test over multiple actions. E.G. Given Go to Startpage of creating a security request
Java Code:
#Given ("^Go to Startpage of creating a security request$")
public void GoToStartpageOfCreatingASecurityRequest(){
//logic to get to the demanded point in testobject...
}
So as any other framework cucumber has its limits but they are intended and you have ways to work around it. ;)
Do not forget to use assertions in your test. Wether you use JUNIT or TestNG (I use TestNG). ;)

How to test for same feature with multiple backgrounds in cucumber

I've got a feature (a .feature file) that are working fine in cucumber.
The background of all the scenarios in the feature just sets up a user, and then logs in as a supervisor, e.g.
Background:
Given I am logged in as a supervisor with an existing supervisee
...loads of scenarios
However the design/goals of the application has changed and the same scenarios should all work whether you are logged in as a supervisor or as the user. This is not true for most of the rest of the application where the design is not symmetrical for supervisors/users.
Is there any sane way to avoid copying and pasting the whole of the feature file with a different background? It doesn't seem like there's a way to either parameterize background (e.g. with an Either: Or: stanza) or alternatively a way to pull in an external file with a load of scenarios. Ideas?
Background:
Given I am logged in as an existing supervisee
...same loads of scenarios
Here's some fantasy gherkin syntax (that doesn't exist)
Background Outline:
Given I am logged in as a <user>
Backgrounds:
| user |
| supervisor with an existing supervisee |
| an existing supervisee |
...loads of scenarios
Alternatively different fantasy Gherkin syntax :
Background:
Given I am logged in as an existing supervisee
Include Scenarios:
supervisor.features
If it was me, I would just suck up the duplication:
http://dannorth.net/2008/06/30/let-your-examples-flow/
An alternative would be to use a tag on the feature that indicates you want to run the scenarios against both user groups. Then use an Around hook to run the scenario twice, once for each type of user.
We've talked about things like Background Outlines before, but the conclusion we came to was that it wouldn't be worth the extra complexity to implement it.

Cucumber: What is the best practice for quoting/not quoting arguments

In Cucumber you define steps which define your BDD syntax; for instance, your test might have:
When I navigate to step 3
and then you might define a step:
When /^I navigate to step (\d+)$/ do |step_number|
# navigate to step ${step_number}
end
Now, all of the above works perfectly fine as is (or at least I think it does). However, you can also do this instead:
When I navigate to step "3"
with a regex:
When /^I navigate to step "(\d+)"$/ do |step_number|
In "The RSpec Book: Behaviour-Driven Development with Rspec, Cucmber, and Friends", author David Chelimsky writes "There are two common styles for steps ... Discuss the pros and concs with your team". On my team a few people have already started using quotes, but it makes invoking steps manually more awkward because you have to escape the quotes inside the step step names (when those step names are themselves wrapped in quotes). However, having quotes makes it more clear where variables are in the Cucumber text.
So, what I'm wondering is: is there any sort of community consensus on what the "right" style is here? Or lacking that ...
Has anyone ever done a benefit comparison between the two styles?
Has anyone used either style extensively?
Ideally I'd like to find out as much as I can before we write a million tests with the "wrong" style ;-)
Seeing as no one is replying to you, I decided to comment - maybe you'll find my opinion helpful.
For example I've been using both styles extensively on a project where there was no "should do this way" in this matter. I think I ended using "(\d+)" style more, because of, like you said:
having quotes makes it more clear where variables are in
As for constructing steps that are composed of other steps, I usually did:
Then /^I fill in my profile information with: "(.*)\/(.*)\/(.*)"$/ do |display_name, picture, description|
And %{I fill in "user_display_name" with "#{display}"}
And %{attach the file "#{picture}" to "user_picture"}
And %{I fill in "user_short_description" with "#{description}"}
end
Hope that helps, I am open for discussion :)

Resources