Use same browser window in cucumber - cucumber

In my features file, I have something like this:
Background:
Given I am on login screen
Then I log in
Scenario:
Given I am on a random account summary
when I try feature-1
Then I see some output-1
Scenario:
Given I am on account summary
When I try feature-2
Then I see some message
When cucumber executes 2nd scenario, it re-executes the background.
My question is-
Is it possible to tell Cucumber to use the same browser session/state from previous scenario?
This will help my tests save some execution time.

Capybara resets sessions before each scenario so you should login explicitly before each of them. But as you don't test login in every scenario, I'd advice to automate this process.
At first, you can make your background more declariative:
Background:
Given I'm logged in
Look at this article, it shows why it's better to do it.
Then you can:
Generate new session at back-end
Send session cookie from back-end to your test
Set cookie to Capybara. Capybara doesn't have cross-driver API for it so you can use:
Webdriver:
page.driver.browser.manage.add_cookie(name: 'name', value: 'value')
Capybara-webkit:
page.driver.browser.set_cookie('c_user=asdasdasd; domain=.domain.com')
Poltergeist:
page.driver.set_cookie('name', 'value')
RackTest doesn't seem to fully support it. As a workaround you can do:
get new_service_request_path, {}, 'HTTP_COOKIE' => 'name=value'

I think you have a number of options using Before and After hooks, as well as tagging. I'd start reading here: https://github.com/cucumber/cucumber/wiki/hooks
Something like this:
Before do
AccessRandomAccountPage() #Assumes you're logged in already
end
Or if this is specific to a set of tests, like you're "AlreadyLoggedIn" tests, you could use tagging:
Before ('#AlreadyLoggedIn') do
AccessRandomAccountPage()
end

In the your feature file, you are used the background:, it is executed for all scenario, to avoid to call every time(details of Background), please changes Background: into Scenario: and it is enough to tell Cucumber to use the same browser session from previous scenario
Scenario:
Given I am on login screen
Then I log in
Scenario:
Given I am on a random account summary
when I try feature-1
Then I see some output-1
Scenario:
Given I am on account summary
When I try feature-2
Then I see some message

Related

Background with multiple data

I have 2 username and password combination and I have a scenario after that.I want to login with username1,password1 and complete the scenario and then want to login with username2,password2 and complete the scenario.I have defined login in background.Is this possible in cucumber?Any inputs will be helpfull.TIA..
It is not possible according to cucumber documentation.
You can only have one set of Background steps per feature. If you need different Background steps for different scenarios, you’ll need to split them into different feature files.
If you want to run the same scenario with different inputs, you can use Scenario outline and use the first username & password in the first Example, and the second in the second Example.
If that is your question, I wonder why you want to rerun the same Scenario with a different username and password? Do the different users have different roles for example and both should be able to do something? Consider whether each scenario / example illustrates a particular behaviour in your application.

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 Login only one time and then run a lot of scenarios

I'm a newbie in BDD and i would like to know how can i do some step before the beginning of my scenarios but only one time.
Concretely, i would like to login in my website and do some scenarios.
Currently, i have a background which log my user and after i have some scenarios.
The problem is that for all of them, my background is repeated.
How can i do to avoid that ?
Thank you in advance.
Feature:
User's module verification.
As a user, i have to be able to manage my profil.
# Background do an user's connection and go to his profile.
Background:
Given I am logged in
# -------------------SUMMARY MANAGEMENT---------------------
#javascript #success #raz #summary
Scenario: Deletion of the summary.
When I click on the element at xpath "//*[#id='presentation']"
And I write in the element at xpath "//*[#id='profile-content-presentation']/div/div/div[2]/div/span/div/form/div/div[1]/div[1]/textarea" value ""
And I click on the element at xpath "//*[#id='profile-content-presentation']/div/div/div[2]/div/span/div/form/div/div[1]/div[2]/button[1]"
And I wait for 1 seconds
Then I should see "Aucun résumé pour l'instant"
You could use behat hooks to automatically log user in before every scenario.
You could also combine this with tags to avoid logging user in, in scenarios taged in a certain way (or do the opposite, depending on how many of each you have).
Also see Behat Mink webdriver session is destroyed after every feature in a suite

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

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

Resources