Reusable/Generic Examples table in Cucumber - cucumber

Is it possible for multiple scenarios to use the same Examples table?
So instead of having something like the following:
Scenario Outline: First Scenario
Given I am viewing "<url>"
Then I assert that the current URL "<url>"
Examples:
| url |
| https://google.com |
| https://twitter.com|
Scenario Outline: Second Scenario
Given I am viewing "<url>" with route "</contactus>"
Then I assert that "<url>" contains "contactus"
Examples:
| url |
| https://google.com |
| https://twitter.com|
I can do something like
Scenario Outline: Reusable Example
Examples:
| url |
| https://google.com |
| https://twitter.com|
Scenario: First Scenario
Given I am viewing "<url>"
Then I assert that the current URL "<url>"
Scenario: Second Scenario
Given I am viewing "<url>" with route "</contactus>"
Then I assert that "<url>" contains "contactus"
I found a similar question on StackOverflow, but merging all my scenarios in just one scenario is not an option for me. Since this question was posted in 2014, maybe there have been some advancements in the framework which I am not aware of :D
Thank you in advance.

You can use qaf-gherkin where you can move examples in external file and use it with one or more scenario. With qaf your feature file may look like below:
Scenario Outline: First Scenario
Given I am viewing "<url>"
Then I assert that the current URL "<url>"
Examples:{'datafile':'resources/testdata.txt'}
Scenario Outline: Second Scenario
Given I am viewing "<url>" with route "</contactus>"
Then I assert that "<url>" contains "contactus"
Examples:{'datafile':'resources/testdata.txt'}
And your datafile will look like:
url
https://google.com
https://twitter.com
Here is the reference.

You might use a Background to specify steps which are equal for all scenarios. (Have a look on the link for constraints)
A feature file might look like
Feature: use of reusable Given
Background: Reusable Example
Given I am viewing url
| https://google.com |
And a search phrase is entered in the search field
Scenario: First Scenario
And step for first scenario
Scenario: Second Scenario
And step for second scenario
implementing the glue code for the Given
#Given("^I am viewing url$")
public void iAmViewing(List<String> url) throws Throwable {
System.out.println("url = " + url);
}
edit After the question has been updated a Scenario Outline could work for both examples.
Feature: use of example
Scenario Outline: First Scenario
Given I am viewing "<host>" with path "<path>"
Then I assert that the current URL is "<host><path>"
Examples:
| host | path |
| https://google.com | / |
| https://twitter.com | /contactus |

Related

How to describe a scenario in gherkin retrieving an Access Token in Given clause

My question is much more conceptual than ever. I'd like to describe a good scenario using the Cucumber feature file where I have to have for each row of my Data table a new access token from the Identity Provider.
I.e
Scenario:
Given <Code Authorization>
And <Access Token>
And The client has the following information
| email | FirstName | Phone |
| xpto# | Richard | 343242|
When the client via Post /xpto
Then The API response a Json file
| code | response |
| 200 | xpto |
I'll use a Data Table for this kind of approach. However, I cannot give a static Access Token because it will expire. I should get a new one every time when my test run but It is not my test it self. The Token is just a Data that I have to have to test my scenario.
Is it ok call a REST in an Given Step? If I do this I am mixing up the objective of my scenarios.
Any thougts are welcome not for your mind but by the book. :-)
Kind Regards,
It seems that you need the token in order to set up the scenario. In that case it is fine to have that in a Given step. You can perform REST or other calls in step definitions for that Given step. For ex: It may look something like below. You can change wordings as you like but try to word it in a manner that shows initial state of the application.
Given I have a token for this scenario
And The client has the following information
| email | FirstName | Phone |
|xpto# | Richard | 343242|
...
...
Given steps are meant to establish a given state. It is considered best practice in BDD. You can find this information in official BDD docs here
Also , if you want to read more about the purpose and structure of Given , When and Then , be sure to have a look here

Cucumber Scenario Outline - Execution Flow

I am working with Cucumber and Groovy in Katalon Studio. I have the cucumber feature file where it has Multiple Scenario Outlines as mentioned below.
When I run the cucumber feature file, it should run the TestCase1 of first section in the scenario outline along with the steps and TestCase1 of second section in scenario outline.
But, it is running the first section of feature file TestCase1 and TestCase2 first. That means it is just loggining with given credentials and closing the browser.
For reference, below mentioned the step definition code also.
Cucumber Feature File:
#Login1
Feature: Title of your feature
I want to use this template for my feature file
#Login1 `**SECTION ONE**`
Scenario Outline: Login into GMP Application
Given running indicator flag
And User is on GMP Application Login Screen
When User enters the in the Login
And User enters the in the password
And User clicks on the ok button
Then User logged in successful at Home Screen
Examples:
| atid | pwd1 | runind | -> Header
| nm1013 | test01g | Y | -> TestCase1
| nm0313 | test02g | Y | -> TestCase2
#Login1 `**SECTION TWO**`
Scenario Outline: Click on the Create Inquiry Menu Item
Given User is on GMP Home Screen
When user click on the Inquiry menu item
And select the billing mode should be
And user click create inquiry item from the heading
Then it should displays create inquiry pagef
Examples:
| contract | -> Header
| GS00T07NSD0007 | -> TestCase1
| GS00T07NSD0007 | -> TestCase2
Step Definition
#Given(“running indicator flag (.*)”)
def run_indicator_flag(String ind1) {
println "Passing Indicator " + ind1
}
#And(“User is on GMP Application Login Screen”)
def user_on_GMP_Application_Login_Screen() {
boolean store2a
WebUI.openBrowser(’’)
WebUI.navigateToUrl(‘https://URL’, FailureHandling.STOP_ON_FAILURE)
}
#When(“User enters the (.*) in the Login”)
def user_enter_userid_in_the_Login(String uid) {
WebUI.setText(findTestObject(‘Object Repository/ORTC01/Page_/input_userid’),
uid, FailureHandling.STOP_ON_FAILURE)
}
#And(“User enters the (.*) in the password”)
def User_enters_the_in_the_password(String pwd5) {
WebUI.setText(findTestObject(‘Object
Repository/ORTC01/Page_/input_password’), pwd5,
FailureHandling.STOP_ON_FAILURE)
}
First of all you cannot connect scenarios in Cucumber. Each scenario is a separate test that starts from nothing, does something and then resets back to nothing.
Secondly a scenario outline is just a way to write several scenarios in a more compact form. Each set of examples in an outline causes a single scenario to be created and run. I would strongly recommend you avoid using Scenario Outlines
Good scenarios describe WHAT is being done without getting into HOW things are done. Your scenarios are full of HOW things are done which makes them complex and very difficult to work with. You should push all the HOW down into your step definitions (or better still helper methods called by your step definitions.
If you do these things you will be able to write scenarios that will look something like
Scenario: Create a billing enquiry
Given I have a bill
And I am logged in
When I enquire about my bill
Then ...
Note: How the above scenario is much shorter and has no detail about HOW you do anything.

#After is invoked multiple times at the end of Scenario Outline in Cucumber

My cucumber Gherkins look like this:
Feature: Story XYZ- Title of Story
"""
Title: Story XYZ- Title of Story
"""
Background:
Given Appointments are being created using "SOAP" API
#scenario1
Scenario Outline: Create an appointment for a New start service order
Given Appointment does not exist for order id "Test_PR_Order123"
When Request for create appointment is received for order "Test_PR_Order123" and following
| FieldName | FieldValue |
| ServiceGroupName | <ServiceGroupName> |
| SerivceGroupID | TestSG123 |
| ServiceType | <ServiceType> |
Then Appointment ID should be created
And Appointment for order "Test_PR_Order123" should have following details
| FieldName | FieldValue |
| ServiceGroupName | <ServiceGroupName> |
| SerivceGroupID | TestSG123 |
| ServiceType | <ServiceType> |
And Appointment history should exist for "Create Appointment"
Examples:
| ServiceType | ServiceGroupName |
| Service1 | ServiceGroup1 |
| Service2 | ServiceGroup2 |
#scenario22
Scenario Outline: Create an appointment for a Change Service order
Given Appointment does not exist for order id "Test_CH_Order123"
When Request for create appointment is received for order "Test_CH_Order123" and following
| FieldName | FieldValue |
| ServiceGroupName | <ServiceGroupName> |
| SerivceGroupID | TestSG123 |
| ServiceType | <ServiceType> |
Then Appointment ID should be created
And Appointment for order "Test_CH_Order123" should have following details
| FieldName | FieldValue |
| ServiceGroupName | <ServiceGroupName> |
| SerivceGroupID | TestSG123 |
| ServiceType | <ServiceType> |
And Appointment history should exist for "Create Appointment"
Examples:
| ServiceType | ServiceGroupName |
| Service1 | ServiceGroup1 |
| Service2 | ServiceGroup2 |
In above feature there is a background which will execute for each example in both Scenario Outline.
Also, in java implementation we have implemented #After and #Before hooks which will also execute for each example.
We are using spring-cucumber for data injection between steps.
Problem occurs when all examples in first scenario outline ends, #After implemented method is invoked 2 times. When 2nd time #After starts at the same time 2nd Scenario Outline examples start executing.
As a result sequential execution of scenarios is disturbed and automation start to fail.
Please suggest if this is a bug in cucumber or we are missing anything.
One of the many things you are missing is keeping scenarios simple. By using a scenario outlines and by embedding so many technical details in your Gherkin you are making things much harder for yourself. In addition you are using before and after hooks to make this work.
Another problem is that your scenarios do not make sense. They are all about making appointments for orders, but your don't at any point create the order.
Finally you have two identical scenarios that you say do different things. The first is for New, the second is for Change. There has to be some difference otherwise you would not need the second scenario.
What I would do is try and extract a single scenario out of this tangle and use that to diagnose any problems. You should be able to end up with something like
Scenario: Create an appointment for an order
Given there is an order
And appointments are made using SOAP
When a new start appointment is made for the order
Then the appointment should be created
And the appointment should have a history
There should be no reason why you can't make this scenario work without any #before or #after. When you have this working then create other scenarios whatever other cases you are trying to examine. Again you should be able to do this without doing any of the following
Using example data to differentiate between scenarios
Using outlines
Using #before and #after
When using Cucumber you want to push the complexity of automation outside of Cucumber. Either pull it up to script before Cucumber starts, or push it down to execute in helper methods that are called in a single step definition. If you keep the complexity in Cucumber and in particular try and link scenarios to each other and use #before and #after to keep state between scenarios you will not have a pleasant time using (misusing) Cucumber.
Its far more likely that your problems are caused by your code than by Cucumbers. Even if Cucumber did have a problem with outlines and hooks, you can fix your problems by just not using them. Outlines are completely unnecessary and hooks are mostly misused.

Gherkin/Cucumber: How can we use the same step definition and pass optional parameters from feature file

I am using Cucumber plugin in RestAssured to write my feature file and automate the REST service. Below is how my scenario looks like
Scenario Outline: Validate the elements in the GET response
Given I have the data setup to test "<version>" and "<order>"
When ...
Then the response should contain accurate data
Examples:
| version | order |
| V1 | O1 |
| V2 | O2 |
My step definition has the below signature for the method:
#Given("^I have the data setup to test \"([^\"]*)\" and \"([^\"]*)\"$")
public void iHaveTheDataSetupToTestAnd(String clientCharacteristicTypeCd, String clientCharacteristicDataType)
My question is I want to have another scenario like this below , where I want to leverage the same above step definition , but pass an additional parameter "special order" as optional.
Can I do that or do I need to create a new step deifnition for just the below Given step ? I was thinking of method overloading / Passing Optional parameter but not sure if it works in Gherkin. Something like this
Ex:
Scenario Outline: Validate the elements in the GET response for special order
Given I have the data setup to test "<version>" and "<order>" and "<specialorder>"
When ...
Then the response should contain accurate data
Examples:
| version | order | specialorder
| V1 | O1 | SO1
| V2 | O2 | SO2
public void iHaveTheDataSetupToTestAnd(String clientCharacteristicTypeCd, String clientCharacteristicDataType, String specialOrder)
What you want is to delegate to a helper where you support both steps. Then implement two steps that don't do more than catch the call and forward it to the helper.
This will allow you to do all kinds of interesting things without having lots of logic in the step class. Each step is often just one or two lines in my case, I catch the parameters and forward them to a helper where all the interesting things in driving the system under test happens.

Scenario hooks only valid on scenario outlines?

We're using Cucumber and Selenium with Ruby. After reading the documentation on hooks I've tried my hand at setting a few tags to set (for example) some environment variables.
Here's a contrived example that demonstrates the problem.
When I establish a Before hook like so:
Before('#tag1', '#tag2') do
puts "in the before hook!"
end
It'll take effect with a scenario defined like so:
#tag1 #tag2
Scenario Outline: This is a test scenario
Given I run my first step for "<user>"
Then I complete my test
#firstrun
Scenarios:
|user|
|fred|
#secondrun
Scenarios:
|user|
|barney|
..however if I move #tag1 and #tag2 to the individual scenarios and not the scenario outline, the hook is never called, for instance:
#secondrun #tag1 #tag2
Scenarios:
|user|
|barney|
Is it possible to 'hook in' individual scenarios, or just the outlines?
Typically with scenario outlines the table of values you're testing is tied to that, not separate scenarios.
E.g
ScenarioOutline
Given I am on gmails website
When I login as <user> with <password>
Then I am able to view my primary inbox
Example:
| user | password |
| Fred | xd13#%& |

Resources