Is it possible to reuse a feature as the "Given" for another feature? - cucumber

Is it possible to reuse a feature as the "Given" for another feature?
Or am I trying to do something I shouldn't be trying to do
basically my features look like:
Scenario: Creating a basic account with valid details (happy path)
Given I am on the "signup" page
And I enter all the right details #this is shortened of about 20 steps for your reading ease
When I press the button labelled "Sign up"
Then I should see the text "Thanks for signing up"
And I should have an email from "confirmation#mysite.com" titled "Confirm your account Michael"
And the database should contain a record for the user marked as unconfirmed
Scenario: Confirming account via email
Given I have created a basic account
When I open the confirmation email and visit the url to confirm the account
Then I should be logged in
And the database should contain a record for the user makred as confirmed
I clear my DB after every feature as they should all be able to be run individually...
Am I going about this the wrong way?
Thanks

The Problem
What you're actually attempting is to reuse a scenario. This is no longer supported in Cucumber.
Aside from other problems with this approach, your tests will be slower and interdependent, since you will be:
driving account creation through a browser, and
making all your tests dependent on the account-creation test passing.
Don't do that.
The Cucumber Way
Generally, you should write your tests to work independently, although you can certainly reuse step definitions. So, in the general case, you might want to add shared steps like:
Given that user account "Test User" does not exist
Given that user account "Test User" exists
which can then be included in your scenarios as needed. The nice thing about this approach is that the steps can create or delete users programmatically.
Alternatively, if most of your tests will be on existing accounts, set up the default data set with the right user fixtures already in place. For the limited subset where you will be testing account creation, just add a scenario background that drives user deletion.

In case you are using Javascript, I've created a package named reuse-cucumber-scenarios for calling a scenario by doing:
Given the scenario "#scenario_tag"
.
Given the scenario "#scenario_tag" with parameters
"""
{
"1": ["step1_param1", "step1_param2"],
"2": ["step2_param1", "step2_param2", "step2_param3"],
"3": ["step3_param1", "step3_param2", "step3_param3"],
}
"""
or creating gherkin variables...
Given the variable "$variable_name" is equal to
"""
#JSON object
"""
or creating scenario functions and calling them by doing...
Given the scenario "#$scenario_function_tag" where variable "$variable_name" is "value_to_replace"
and more...

Currently, you can use the following approach:
Background:
Given Some common setup
And Some more setup
Scenario: one
When setup1
Then something1
Scenario: two
When setup2
Then something2

Related

How to implement 'if' in Gherkin

I am trying to convert Selenium test to Gherkin. Is there way to implement if statements in Gherkin?
Example : assume the code is written in the below format. I am just writing description as below. Please understand the part after double slash is the actual Selenium code:
// launch the application
// login to application
// navigate to page
String str;
if(str== "XYZ")
{
// verify title
}
//verify text field 1
//verify test field 2
//verify select box
For this I am trying to write code in Gherkin as follows
Given user launches the application
When user login with valid credentials
and navigate to required page
When String str is "XYZ"
Then verify title
And verify text field 1
And verify test field 2
And verify select box
but this code is incorrect because if the str is not equal to "XYZ" we want that title should not be verified but other verification like text field1,2 and select box should be verified.
You don't implement if in Gherkin.
Gherkin is about communication and those you want to communicate with, non coders, don't know what an if statement is. They don't care either.
The solution? Two scenarios to cover both cases.
Ideally, this level of detail would not be in your Gherkin scenario. The best approach is describe business use cases, not low level details. This is what Gherkin is designed for: communicating with non-technical stakeholders so that you can work out if you are building the right thing in the first place. Here is what I would write:
Given the user is logged in
And the user is on the required page
When they enter data that requires the optional fields to be validated
And they enter invalid data in the optional fields
Then the form shows an error on the optional fields
The low level details don't matter (that the string is specifically "XYZ" or that it is the title field is not important), so these should be hidden in the step definition and/or unit tests.
In order to continue to check the other fields, you can just add another step after this:
When they enter invalid data in all of the other fields
Then each other field has an error message attached to it.
Again, there is no need to specify the actual fields, or separate them into their own steps. The idea is to express the high level business value of the scenario, i.e. that the form is validated when it should be.
The advantage to keeping things high level is that when the form changes (as it eventually probably will), then this scenario can remain untouched. Which is correct as the business case is the same: it should validate when it's supposed to. All the changes will be in the step definitions. This means that there is no reason to have another discussion with your stakeholders about whether your scenarios are still testing the right thing.
You can write the scenario, somewhat like this:
Given the user launches the application
When user login with valid credentials
And navigates to required page
Then he should see the page datails
Inside the Then step you manage all the logic.
Then(/^he should see the page details$/) do
if condition
...
else
...
end
end
Gherkin is not a programming language to use if or else conditions. It is a part of BDD framework, that is implemented, to make the stakeholders and other non technical resources understand what the test process is about. Hence, it is always recommended, you keep the gherkin as simple and as generic as possible.
Strictly speaking you should create an alternative statement along the lines of:
Given user launches the application
When user login with valid credentials
and navigate to required page
When String str is NOT "XYZ"

Getting output of one scenario as input to another scenario in cucumber

I am using cucumber in combination with selenium for testing the java web application. The following is the Scenario that we have
get generate PIN page
enter user name
enter password
click on submit button
Now it generates a PIN in the database depending on so many calculations. now i need that particular PIN, to give it as an input to a different scenario. how can i achieve this? Thanks in advance.
I would assume that you can access the PIN within the database after the above scenario. That being the case, I would add one more step to the scenario that acquires - and confirms - that the PIN was indeed generated. At that point, you can store the PIN in a local variable and then use it within the next scenario.
So your first scenario would look like this:
Get generate PIN page
Enter user name
Enter password
Click on submit button
Confirm PIN number in database
The last step would not be done within Selenium, but via an API call or some other means to acquire the PIN from the database. It would confirm the PIN (e.g.; regex=/^\d{4}$/) and then store it in a local variable, say something like #customer_pin (assuming you're using Ruby).
Your next scenario would start off something like this:
Get generate login page
Enter customer ID
Enter customer PIN
etc
When you hit the "Enter customer PIN" step, you pull it from the locally stored variable (#customer_pin).
My advice is that when executing this second scenario, you confirm that you have a legitimate PIN within your locally stored variable, in case someone should run this scenario out of sequence. You could do this by using a global variable and running a "Before" hook in your features/support/env.rb file like this:
Before do
$dunit ||= false
return if $dunit
$customer_pin = nil
$dunit = true
end
In this case, I use $customer_pin instead of #customer_pin in order to make the variable globally accessible. Then after running your first scenario, $customer_pin would be assigned to a legitimate value so that it can be used in any subsequent scenarios. Subsequent scenarios would use the regex expression to confirm it has a legitimate value, and raise/throw an exception if not.
I would divide your problem into two.
One that verifies the pin generation as this may be important for your stakeholders.
One that implement a backdoor to support other cases where a valid PIN is needed. Maybe an API that is able to generate or retrieve a valid pin number. Maybe create and store the PIN in the database without touching the system from the outside. I would use this way to retrieve a PIN whenever I need a valid PIN for other scenarios.
The technical solution on how to get a valid PIN isn't too important. What is important is to decouple the execution order of the scenarios. The execution order of the scenarios is undefined. Each scenario must be able to be executed in isolation and in random order.
Coupling scenarios is a well known anti pattern described here and here.
To Solve this kind of situation you have to use Cucumber Background feature, This is run before each step. and will generate a PIN based on given inputs and then generated PIN will be available across the scenarios.
Find feature file definition based on your requirements.
Background:
Given I Get generate PIN page
Then I Enter user name
And Enter password
And I Click on submit button
And I Confirm PIN number in database
#TC01_GetUserInformationByPinTest #NoBrowser
Scenario: Get User information by generated PIN from background.
Given I Get User Information by using generated PIN
And I verified that given username is same as response Data
I believe this will help you to solve your issue.

what is parameterisation concept in HP ALM ?

i need help on HP ALM Parameterization concept. what is use of Parameterization concept in HP ALM. how can we implement this concept ? give me any examples
In terms of manual test cases, parametrization comes very handy when you have scenarios that have pretty much same steps or at least same structure.
When you create a new test case, you will have a button to parameters on the header menu, you can list all the parameters you think you would need. For a simple login test on multiple pages ( home page, account page ) with similar layout, you will have username, password and url as parameters. It also allows you to give default value. You can add more parameter as you write steps in the step description window.
once you have the complete test case created, go to test configuration tab of the test case and add test configuration by clicking plus button. There will be one by default. You can create as many test configuration as you want. For above example you can add three; one for home page, one for account page and one for search page. The test configuration will help you have the test instances ready while pulling the test cases into test lab without having to drag the same test cases multiple times.
The test configuration allows you to define the actual/default value for each of configuration added reducing the time it takes to fill this info during execution.
Test execution - after you pull the test cases into lab, during the manual run a pop up comes to show the values for each of parameter being used for that test case. Te values can be added or updated.
Please find a video I found on YouTube.
https://youtu.be/vCrJcHrosio
I don't have ALM to show with diagrams. Hope this helps.
Create a test testcase to test this feature.
Thanks
Happy exploring !!
Parameters help the user to assign a value to a variable so as to execute the same test with different sets of data.
For example, the user name and password can be two parameters, for a login related test, which would be assigned with a value.
How to use parameters:
Create test case with steps in Test Plan module under selected Cycle and proper folder.
Select the test step against which you would like to add the parameter. The 'Parameter' Icon will be enabled. Click on the same. The 'Parameter' dialog box will open. Now, click on 'New Parameter' button. Add the parameter (here: username or password). Add the default and/or actual values, description for the same and save.
You can also add different test configurations to use these parameters for different use case scenarios.
More details can be found here: http://alm-help.saas.hpe.com/en/12.53/online_help/Content/UG/ui_menu_test_parameters.htm

How to reuse a cucumber feature

I have a cucumber feature called "A" located at "C:\cucumber tests"
Now I want to create another cucumber feature call "B".
The first part of the steps of feature "B" will be exactly the same as in feature "A". I don't want simply to copy all the steps in feature "A" to feature "B"
Is there anyway to call feature "A" in feature "B"?
I appreciate it.
ruby version:
I don't believe you can call an entire scenario, but you can call steps from a step using the steps method.
Given /^(.*) is logged in$/ do |name|
steps %Q{
Given the user #{name} exists
Given I log in as #{name}
}
end
This will at least allow you to reuse your stepdefs, but not your scenarios.
For more information see Cucumber Docs
If you are willing to put the features in the same file, you can use the background to run the same steps for each.
Background: Login the user
Given I have an admin account
And we have a user named "john"
And I am logged in
Scenario: Reset Password
When I reset the users password
Then the server sends an email to "john"
Scenario: Lock User
When I lock the account for "john"
Then "john" cannot log in

How do we make a crystal report print only specific fields based off of the user running the report?

We use a construction software, Viewpoint. We need a report to print the GL Accounts associated only with the user running the report. I was wondering if anyone had any ideas on how I could easily and efficiently achieve this? My "last-resort alternative" is just creating separate reports for each GL that pulls only that GL's info.
It depends on how the accounts are associated to the user. If there is a link, you can simply create a user parameter that asks for the user name. Then in the record select of the report you only pull accounts that match the user name in the parameter.
So, for example your parameter is called {?userName}. In the Record Select the formula would something like {Accounts.Salesman} = {?userName}, where {Accounts.Salesman} would be the field that links the user with the account.
You can get the current user name and use it to filter the records in your report. There are 2 ways to do this :
create a parameter, hide it and automatically fill the value. You
will need a special viewer for this scenario. Let me know if this is
what would you prefer I can give you some links. If you leave the
parameter visible the user can insert any value and view any records
use an UFL . For example this one:
http://www.viksoe.dk/code/u2lwin32.htm provides a function LoginName
which will return currently logged user. There are other UFLs which
support similar functionality, check if your company is already
using something . This one is free. You need to register the UFL on
each system which will run the report . Once registered the
available functions will become available for formulas.
I personally prefer the viewer solution because it is more flexible and will cover other scenarios too. However if you need just the username UFL will be a better approach.

Resources