Cucumber reuse example - cucumber

I would like to use the Examples in the code below in the "When I sign up with the following information" line without having to specify every single parameter and without having to repeat the table in the examples.
Is this possible?
Scenario Outline: signing up a user that already exists
Given I am registered as "<username>" with password "<password>"
When I sign up with the following information
Then I should be on the sign up page
And I should see "user already registered"
Examples:
| email | username | name | password | password_confirmation | city | mobile_numbers |
| foo#bar.com | existent | some user | temp123 | temp123 | foobar | 70 707070 |

When you generate the cucumber project folder, the structure will contain a ruby file called env.rb within the support directory inside the features folder (along with the step_definitions folder). Here you can define your methods, variables, etc. that will serve as a common source for all your other tests inside the same project.
Since Cucumber is a Ruby gem, the data is defined in Ruby's format. You can define them as methods or variables. I prefer to define them as methods (in ruby, methods return their last line). So your methods will look like
def email
"foo#bar.com" # returns this when email is called
end
def username
"existent"
end
and so on...
You can optimize it to suit your needs. You will call them in your ste_definition files just like any normal ruby function would be called.

Related

Gherkin - real or representative scenarios?

Lets say that my app works with some books with real titles "The Old Man and the Sea", "War and Peace", etc., when creating scenarios, should I use real title like:
Given I have a book "War and Peace" persisted
When ...
or should I do something like:
Given I have a book "Book1" persisted
When ...
Option 2 is more generic, but artificial example. And If I use first option, person who is reading the test has to have domain knowledge, and he will also have some presumptions about the scenario as soon as he reads the title of the book.
Also, is there some simpler way for me to create data table without repeating data (in this case page where I have always to repeat 1,1,2,2,2,2...)? example:
When we receive book with following content:
| Page | Line | Text |
| 1 | 1 | a |
| 1 | 2 | b |
| 2 | 1 | a |
| 2 | 2 | b |
is this standard way to do it:
When we receive a book
And page 1 has content
| Line | Text |
| 1 | a |
| 2 | b |
And page 2 has content
| Line | Text |
| 1 | a |
| 2 | b |
First of all start with the name of the scenario, this name should be meaningful and should be like a summary about what is about the test.
Once you have the name then the other steps should describe a business flow that of course should contain domain language, because for example if i don't know nothing about healthcare, banking etc then why would I understand a test about a specific domain subject?, the scenarios are for a specific group of people (the ones that are working in the specific domain).
One of the BDD role is to help in understanding better the specifications and the application on all levels (technical to non-technical, but on the same business domain), to improve communication.
Now for your specific issue.
Given I have a book "War and Peace" persisted does not offer to much info since the title of the book says nothing about the test data; is a new book that just was added/created?, is a type of book technical/poetry or just some book?
What was useful for me is use a name for the the data that says something about the data used in the test.
If you don't have different types of books you can use any name, else a more complete name would be more useful.
As for the table, that represents a data set and you need to tell what to check and where; depending by case you could group some checks, if you can read all data at once or not, or if you need to specify the texts/pages.
One option would be to hide the data set and say something like:
Given I have a book "War and Peace" persisted
Then the book contains the expected content for "War and Peace"
in the first step "War and Peace" - gets/creates a specific book that is identified by this title
in the second step "War and Peace" - identifies a set of data for the expected result using the same name since is the expected for that specific data set, this set of data can be list/array/map ... depending of what programming language you are using.
Don't think to much to the details, just define the scenario in human readable language using outside-in approach, then see if you can refine it and after start the implementation.
Always use a description for the feature and a meaningful title for each scenario

How can I display multiple line scenario text in extend reports?

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.

How to take dynamic value of Examples to run for Scenario_Outline in behave

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

Cucumber Scenario outline best practice

Below is the list of steps that should be performed as part of my scenario.
Verify the file generated at S3 location with Redshift DB
Verify the duplicate between the original script with latest script
Verify the newly added column in latest script
Verify that old column data is intact in original script and latest script
And I have the below feature to cover the same.
Feature:Add # of times PDP browsed to search and sort output files User Story CDH-3311
#regression
Scenario Outline: :Validation of file being generated at S3 location after job run
Given user has the json file with <country> and <brand>
Then user execute the Generic-extract-Batch job
Then user verify the file is generated successfully at S3 location
Then user verify the data in Redshift db with generated file
Then user verify the duplicate data in latest sql script
And user verify the duplicate data in original sql script
And user verify PDP_VIEWS column in latest sql script
And user verify <old coulmn> data of original script
And user compare it with the latest sql script
Examples:
| country | brand | old column |
| US | test1 | test6 |
| US | test2 | STORE |
| US | test3 | test7 |
| US | test4 | SALESUNITSCORE |
| US | test5 | TOTALSCORE  |
Kindly verify that the outline adhere to best practices and is the correct representation of the things that needs to be done for the above mentioned tests
Not sure about the business flow, but make sure there are not spaces in the examples table columns. so the last column should be old_column.

Is there any way to run same cucumber feature file multiple times?

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

Resources