I'm starting with Cucumber and Selenium.
I've created a test case where selenium runs a form and inputs default values, and clicks on default buttons, let's call this test case 1, but now I need test case 2, where I should run same steps, but with different inputs, imagine this:
I have a form where I have a selection, if the user is a Male or Female, depending on the button clicked, my form changes, so if I choose Male, I get for example an input asking if I'm "tall" (just an example), but if I choose Female, I get an input asking if "I have a boyfriend". Depending on my journey on the form the final result is different, taking this into account:
How could I create multiple test cases, without repeating code, that would test each option?
Example:
Test case A: Male Input
Test case B: Female Input
I need Test case A to finish the form, and next execute Test Case B, remember that my form has inputs that are the same in both scenarios, so I will have to use multiple times the same:
Scenario A:
Given I have to choose Male or Female
When I click ('one of them')
Then I will click other stuff
Is this even possible?
Welcome to Stackoverflow!
Scenario Outline is something best suits your scenario.
Scenario Outline: Validate Selection
Given I am in the required page
When I selection <choice> from the option
Then I will click other stuff
Examples:
|choice|
|Male |
|Female|
Best example from the docs here. Link
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
Related
I am using an outline examples in my test,
something like :
Feature: feature name
Scenario Outline: outline scenario
Given I go to
When I click on button
Then I should have
Examples:
| website | title |
| google | google welcome|
| yahoo | yahoo welcome |
| Astalavista | altalavista hello I m dead |
so far it is great !!! but if the first one is doing okay and the second one is having an issue the third is not played ...
do you know how to continue all the test in case of outline scenario ?
thanks a lot for your help !
There is something called failFast in the cucumberOpts.
If the value is true then it means fail at the first failure and do not proceed.
If the value is false then the execution will continue for all the scenario in the feature file even if one scenario fails.
So try by making it as false.
Thanks,
Naveen
In my feature file, using the same scenario I am checking more than one requirements. I have written the scenario like below:
Scenario: My first requirement ID
My second requirement ID
My third requirement ID
Etc
After execution, the extend report shows only the result as
Scenario: My first requirement ID
How can I get all the three I D,s in extent report.
NOTE:Each of my scenario title is lengthy.
Can you explain your scenario text a little bit more? According to the documentation, the scenario should describe in human terms what we expect the software to do. It is quite unusual to include expected data in that scenario text. Are you using the ID from an enum? If that is the case, it would be better to spell out the enum in human readable terms. Scenario: UserType is Administrator for example. Another option would be to use a Scenario Outline, something like
Scenario Outline: My generic requirement statement
Given Id <whateverId> is provided
When I do <activity>
Then I expect to see <result>
Examples:
| whateverId | activity | result |
| 12 | firstMethod | MyResult |
| 20 | secondActivity | anotherResult |
| 42 | thirdExample | thirdResult |
The variable names provided in the outline in angle brackets become the column headers in the examples grid. Just be sure to indent the grid below the Examples: line and also include the pipe | on both the left and right boundaries of the grid. Hopefully that helps.
Feature: showing off behave style
Scenario Outline: wow
Given I put "<brand_one/brand_two>" in a blender
When we implement a test
Examples: Brand
| brand_one|brand_two|
| first Frog | second Frog|
I want to select brand_one or brand_two at run time.
On executing feature file i just want to execute method taking value of brand_one (i.e first Frog) or brand_two (i.e seconf Frog) at run time. But only execute one desired brand and not the other one. Please help me out to select this on run time. Thanks
You could use tags for this:
Feature: showing off behave style
Scenario Outline: wow
Given I put "<brand>" in a blender
When we implement a test
#FrogOne
Examples: Brand
| brand|
| first Frog |
#FrogTwo
Examples: Brand
| brand|
| second Frog|
Then choose it at run time like so
behave -t FrogOne
or
behave --tags FrogOne
I have some documents to upload it. For that, I wrote a scenario. I need to run the same feature file multiple times. How can I run the feature file Multiple times?
Instead of using multiple feature file use scenario outline which will execute the same scenario multiple time as per the example table you will provide to it
Example:
Scenario Outline: Create ABC
Given I open the application
When I enter username as <username>
And I enter password as <password>
Then I enter title as <title>
And press submit
Examples:
| username | password | title |
| Rob | xyz1 | title1 |
| Bob | xyz1 | title2 |
So here the same scenario will run 2 times as example table have 2 data, you can add as many data table and cucumber will execute that number of times
How do I write a scenario outline to test a calculation based on one variable which outputs 10 different variables?
I've tried various options and getting various errors including:
Unable to find option "<frequency>" (Capybara::ElementNotFound)
and
(Cucumber::ArityMismatchError)
The code below gives the Capybara::ElementNotFound error
Scenario Outline:
When I select "<frequency>" frequency
And I press recalculate
Then I should see total amount and percentages recalculated by <frequency> frequency on the results page
Examples:
| frequency |
| weekly |
| daily |
Examples:
| tax | sub-total | total |
| 38.25 | 114.74 | 191.24 |
| 3.19 | 9.56 | 15.94 |
Step definitions
When(/^I select "([^"]*)" frequency$/) do |frequency|
select "<frequency>", from: "frequency"
end
Then(/^I should see total amount and percentages recalculated by <frequency> frequency on the results page$/) do |table|
expect(results_page).to have_content("<tax>")
expect(results_page).to have_content("<sub_total>")
expect(results_page).to have_content("<total>")
end
form markup
<form action="change_result_frequency" method="post">
<label for="frequency">Frequency</label>
<select name="frequency" id="frequency">
<option value="yearly">yearly</option>
<option value="monthly">monthly</option>
<option selected="selected" value="weekly">weekly</option>
<option value="daily">daily</option>
</select>
<input type="submit" name="commit" value="Recalculate">
</form>
I am new to cucumber and capybara so I'm unsure how to write scenario outlines with data tables. What am I doing wrong?
What you are doing wrong is trying to write the detail about how your calculation works in your feature. Instead what you should be trying to do is use your feature to explain what you are doing (it has something to do with frequency, but otherwise I have no idea). When you take this approach you don't bother to specify the actual results in your scenarios for a number of reasons
The values of the results aren't that relevant to this sort of test.
Putting the results in the scenario is difficult, error prone (typos) and vastly increases maintenance costs
I'll explain point 1 a bit more.
What you should be doing in this scenario is driving the development of the change frequency functionality that you are working. This comprises of two parts
i) that you have a UI for the user to change the frequency, and that in response to this action your UI shows the results of the frequency change.
ii) that when you change the frequency the correct results are calculated
The first part, should be driven by a scenario in cucumber, so you can write something like
Given ...
When I change the frequency
Then I should see a new set of results
The second part SHOULD NOT be tested by writing scenarios in Cucumber. Instead you should be writing unit tests for the thing that does the frequency calculations. Writing unit tests allows you to
write much faster tests
write more tests so you can deal with edge cases
write your tests in a programming language so you can easily generate and use values of any type
The biggest mistake I see new users to Cucumber make nowadays is to use scenario outlines and example tables. I'd recommend you stay away from them. Every time you want to use one stop and have a think. Ask the questions
What am I testing here and why is it important
Am I trying to prove that something works? If so shouldn't I be using unit tests for the proof.
Good luck :)
You should only have one examples table with your Scenario Outline and you need to access the "variables" in the steps in your outline. So something like the following (with your step definitions updated accordingly)
Scenario Outline:
When I select "<frequency>" frequency
And I press recalculate
Then I should see <tax>, <sub-total>, and <total> recalculated on the results page
Examples:
| frequency | tax | sub-total | total |
| weekly | 38.25 | 114.74 | 191.24 |
| daily | 3.19 | 9.56 | 15.94 |