How to add action in then section of cucumber? - cucumber

I'm new to cucumber and I wonder how can I test the following scenario:
user, that is not signed in, clicks on "buy" link on my site
popup with login/passwords shows
user fills login and password fields and then click 'Sign in'
page reloads
user clicks again on "buy" link and see popup with order details
I have problem with defining "Then" section, because for me it should be:
Then
When I click "buy"
I should see "order details"
Is this possible with cucumber?

Scenario should have a single testing goal. With your current scenario you have 2 goals so it may be better to write 2 scenarios:
Possibility of login after clicking buy
Ability to see order details after clicking buy.
I don't understand why you want to do this using 1 scenario.
That said, Cucumber doesn't distinguish between Given, When and Then at step definition layer so you can write like this:
Given ...
When ...
Then ...
When ...
Then ...

The feature file would look like this (simplified):
Feature: View Order Details
As a user I can process an order
Scenario: Process Online Order
Given I am logged in
When I click "buy"
Then I should see the order details

Related

Populate data back to form in Kentico

Here's what I'd like to do but not sure how to. I have a form that's like a typical doctor/school form, where the form has 2 sections: [1] Section 1 at the top is for user (with standard fields like First Name, Last Name etc.), and [2] Section 2 at the bottom is for Admin/Office use only (with fields like Reviewed by, Approved/Not Approved etc.)
what I was able to do is to process Section 1 where I got ALL users' submission and display all their data on a webpage using a RepeaterWithCustomQuery. That's pretty basic. But in order to do what described above, I guess that I'll need to pull the submitted data and populate them back to Section 1 of the form (maybe as readonly data at this time) and then the Office/Admin staff can fill in Section 2.
I hope I made sense and hope that someone can point me to the right direction. I only use Portal Engine, no access to file system or backend.
I think I understand your issue you want to have some sort of an editor for biz form data. Similar to what you have in the admin. And the problem is that you don't have access to backend. :( Such thing is available for custom table data (there is web part), but not for biz form data. There is no ready to use web part. Here is old topic on that https://devnet.kentico.com/questions/how-to-edit-the-information-of-a-record-using-the-bizform-layout.
you want to have something like the admin page for editing form records:
/CMSModules/BizForms/Tools/BizForm_Edit_EditRecord.aspx?formID=7&formRecordID=1
but customized :(. I'd say without back end access the only options I see:
Create a new role "Biz Form Editor" (or use existing) that has rights only to edit biz
form data. So all your people who do "validation" part must have a
Kentico account with role "Biz Form Editor".
Add link above to your repeater with appropriate record id.
P.S. There are special code names for alternative forms (https://docs.kentico.com/k8/configuring-kentico/creating-alternative-forms/code-names-of-automatically-used-alternative-forms). If you create an alternative form with special name update the system will automatically load it when you edit the record.
Not sure that I understood correctly your question. But what you need is to use alternative forms. So the idea is that one form is for "registration" and the 2nd one for "validation".
So in your registration form you show only firstName, lastName etc and you don't show "validation" fields. In the validation form you show firstName, lastname etc as label and show textboxes for validation fields.

How to write Tests with multiple steps in cucumber

If you have a simple form, you enter your name, Gender and save it. When you select the Gender drop-down it will list [Male | Female].
So when writing test scenarios for the above, the typical way is to Write it in steps.
Precondition : User Loged in and Form Opened
Steps | Expected Result
------------------------------------------------------------------------
1 : User Enters the name | Entered name should be there
2 : User Clicks on Gender Drop-down | Male and Female should be listed
3 : Users Selects a Gender | Selected Gender should be there
4 : Clicks on Save | Information get saved.
Following is one way to represent this is Cucumber.
Given User Loged in and Form Opened
When User Enters the name
Then Entered name should be there
When User Clicks on Gender Drop-down
Then Male and Female should be listed
When Users Selects a Gender
Then Selected Gender should be there
When Clicks on Save
Then Information get saved.
Now How can we represent this in Cucumber? As I read you are not suppose to have multiple actions within the same scenario. If you add multiple testcases for each step above, the Testcases will grow exponentially. What is the best way to handle this?
My approach would be to think about documenting in Gherkin (using principles of BDD) - the application behavior and not test cases.
In fact, this article provides some good context around misconceptions of BDD.
With that said, even though you are trying to validate the above criteria in a single scenario, I would recommend at least splitting into 2, to keep your acceptance tests succinct, readable and maintainable.
Here's my attempt, at reformulating:
Scenario: Gender Dropdown values
Given a user is logged in
When the user clicks the Gender dropdown
Then Male and Female options are displayed
#EndtoEnd
Scenario Outline: Saving User information
Given a user is logged in
When the user enters a <Name> and selects a <Gender>
And Clicks on Save
Then the information gets saved
Examples:
|Name|Gender|
|Keith|Male|
|Jenn|Female|
||Female| # Negative
|Rob|| # Negative
Additionally, you can also think about throwing in some negative test scenarios as well.
If you need to test on cucumber with multiple data, you can go ahead and use the scenario outline feature, where you load the data in the examples after the scenarios.
Scenario Outline: User enters details and stores
Given User Loged in and Form Opened
When User Enters the <Name>
And Users Enters the <Gender>
And Clicks on Save
Then Information get saved.
Then I Go back (In case view info)
Then I assert for <Name>
Then I assert for <Gender>
Examples:
|Name| |Gender|
|x| |Male|
|y| |female|
Cucumber is not about testing how you do things, its about specifying why you do things. This scenario is all about how the user interacts with the form, its all about clicking on this and entering that, none of this has any place in scenarios.
Your scenario and question doesn't even tell us what you are trying to do, so I'll just have to make something up. Lets say you are trying to add a friend
Feature: Adding a friend
Scenario: Add Alison
Given I am logged in
When I add Alison to my friends
Then I should see Alison is my friend
and thats pretty much that. Note how none of the details about how you add a friend are captured in the scenario, thats not what Cucumber is for.
Now on the technical side lets explore how we get this scenario to actually add a friend. First of all the step definition
When 'I add Alison to my friends' do
add_a_friend name: 'Alison' # perhaps add a gender param here
end
Notice how even the step definition doesn't know how to fill in the form.
Finally a helper method to actually do the work (done in Ruby cos its clearer)
module FriendStepHelper
def add_a_friend(name:)
# here is where we fill in the form and choose the gender
end
end
World FriendStepHelper # makes add_a_friend visible to step def
So technically this is how you represent your problem in Cucumber you write WHY you doing things in using your scenarios and you push down HOW you do things to helper methods used by your step definitions

Cucumber scenario with same buttons?

This is my first time doing cucumber, so maybe what im asking isn't wrong but it just seems like there is a better way:
So right now im going through a survey-like website as my first automation task with cucumber. It works just fine, but like most surveys theirs a lot of "Fill then out, then click Next"
so in my Feature file I have a lot of:
Then I will click 'Next'
Which matches with the step (Since the button all have the same text):
Then(/^I will click 'Next'$/) do
click_button('Next')
end
So this one step definition defines like 8-9 lines in my feature file...which I guess is ok, but my feature file just looks...ugly? Im not used to the Gherkin language AT ALL as this is my first time, but it's basically turned into a lot of:
THEN SELECT THAT THING FROM DROPDOWN
THEN CLICK THIS RADIO BUTTON
THEN SELECT THIS BUTTON
THEN CLICK NEXT
Like over and over....I mean it's human readable, but it just looks messy? is this right?
Also im not really sure when I should use "explicits" in my feature file? Like should I explicity say Im going to select 'X' (or "X", not sure if it matters) from a dropdown? or Click the 'Next' button or select the 'yes' radio button for example?
You are writing what are called imperative steps and it is indeed the wrong way to go about cucumber/gherkin.
Your steps should be saying "WHAT" you are doing and not "HOW" you are doing.
Not knowing the details of your form, let's make up a flow.
Page 1. Name
Page 2. Demographics
Page 3. Favorite football team
Page 4. Favorite basketball team
Page 5. Results page showing how popular their teams with other survey takers
Given I enter the "Are you a Bandwagon Fan Survey"
When I enter my name
And I enter my demographics
And I choose a favorite football team
And I choose a favorite basketball team
Then I should be directed to the result page
And I should see my teams ranked on a graph
The details for entering your name and clicking next should be hidden inside that stepdef. A person trying to test your app doesn't care what the name is, or what the field ID is or anything like that. They just want to know the work flow.
If you are doing scenarios to get the name page to error you simply modify your gherkin to say something like.
When I enter a name too long
Then I see a name too long error message
When I do not enter a name
Then I see a name missing error message
Again, you leave the details of too long or what the error message is to the step def (or below) unless it is critically important to the test scenario. I usually set up factory girls to handle my data and just use the text of the scenario to pull the current data. so I'll have an error message factory and within that a "name_missing" and a "name_too_long" sub-factory that provide the correct values. If I need to change or look something up, everything is in the same place...but above all it's not cluttering my gherkin.
Yes I think you want to send parameters to step definition
Then I will click 'Next'
Step definition will be
Then(/^I will click "([^"]*))*"$/) do |text|
click_button(text)
end
also above will work for
Then I will click 'OK'

Does if else concept available in feature file (Gherkin language)?

Is there anyway where we can use if/else concept in feature file? For example:
Scenario: User should be able to check login page
Given I am on login page
When I click on SignIn button
Then I should be in home page
If yes
Then I will create a new profile
Else
Then I will logout from page
Not that I am aware of. Gherkin (and cucumber) are best used when they specify discreet business cases though, and should be repeatable, else they get hard to follow and test. It looks like you have two stories here at least:
Scenario: A new user should be asked to sign in
Given I am a new user
And I navigate to the login page
When I click on SignIn button
I should not be able to get to the home page
Scenario: An existing user should be able to log in
Given I am an existing user
And I navigate to the login page
And I submit valid credentials
When I click on SignIn button
I should be taken to the home page
No you can't and you shouldn't. Feature files are for business behaviour, not programming.
From your scenario I think you are trying to deal with different behaviour, depending on whether you are registered or not. To do this you would write two scenarios
Given I am registered
When I
Then I should ....
Given I am a new user
When I ...
Then I should be asked to register
Notice how these scenarios don't describe 'how' anything is done. Anything like `I click on foo' in feature is a smell and should be avoided.
You would like to twist your scenario as below
Scenario: User should be able to check login page
Given I am on login page
When I click on SignIn button
Then I should be in home page
And check if home page exist and store result as existence
If existence is true
Then I will create a new profile
And Else
Then I will logout from page
In the step definition for And check if home page exist and store result as existence is Check home page exist or not by checking the driver.findElement returns null or not and based on it store it as true or false in thread context instance with key as existence
In the step definition for If existence is true retrievie the thread contenxt instance data for key existence as asserts it if its true or false and store it with key conditionresult in thread context instance.
In the step definition for Then I will create a new profile before creating the profile retrieve thread context instance data for key conditionresult and check if value is true then create the new profile and set again thread context instance for conditionresult as null with in the condition loop.
In the step definition for And else retrieve the thread context instance for conditionresult and check if its false and if yes set again thread context instance for conditionresult as true
In the step definition for Then I will logout from page retrieve the thread context instance for conditionresult and check if its true then perform logout and set again thread context instance for conditionresult as null with in the condition loop.
What about if we are using Gherkin in a smoke test type situation and we need to ensure something exists in the database using only the UI?
Scenario: I need to create one (and only one) Box before I run the rest of my smoke tests
Given I login as Admin
When I am on the Box list Page
Then the test passes if the Box named "QA SmokeTest" exists
When I click the Add New Box Button
And enter the details for a New Box
And press Save New Box
Then the test passes if the Box named "QA SmokeTest" exists
The re-use of the same Then step twice is essentially an if-else that will make sure my Box exists so that I can run my other tests in the smoke test suite that require a Box.
But that is dependent on being able to stop the Scenario execution in the test runner or doing something extraneous like:
ScenarioContext.Current["TestPassed"] = true;
and then in each of the steps
if(ScenarioContext.Current.Get<bool>("TestPassed")) return;
You can use parameter in feature file and implement the If else in the Code based on the parameter passed.

BDD - Cucumber: Is possible to disable Background logic for only one scenario in feature?

In a feature file have a Background and several Scenarios, but now need a Scenario related to same feature that don't have to run background logic, is possible to disable for only a scenario?
UPDATE - Add Example:
Feature: Sign Up
In order to access to protected parts of site
A user
Should sign up
Background:
Given I am on sign up page
And I am not logged in
Scenario: User sign up succesfully
When I sign up with valid fields
Then I should view dashboard page
Scenario: User altredy sign up
When I sign up with altredy registred user e-mail
Then I should view altredy sign up message and link to forgot password page
Scenario: User try to sign up with missing/wrong data
When I will try to sign up with missing/wrong data
Then I should error message
Scenario: User altredy sign in
#here disable background
Given I am logged in
When I am on sign up page
Then i should be redirect to dashboard page
Solution 1.
You could put that only Scenario in other Feature file where there's no background or it has it's own background
Solution 2.
Remove the background from the feature file and then put its logic on the step definitions, something like
Given 'I am on sign up page' do
some code here
end
Given 'I am not logged in' do
some code here
end
then on each first step
Given 'I sign up with valid fields' do
step 'I am on sign up page'
step 'I am not logged in'
the rest of your code for this step
end
Given 'I sign up with altredy registred user e-mail' do
step 'I am on sign up page'
step 'I am not logged in'
the rest of your code for this step
end
Given 'I will try to sign up with missing/wrong data' do
step 'I am on sign up page'
step 'I am not logged in'
the rest of your code for this step
end
This is not pretty though you would repeat those at least 3 times.
Solution 3.
You could get rid of that Scenario and paste it's steps in the first Scenario, something like
Scenario: User sign up succesfully
When I sign up with valid fields
Then I should view dashboard page #this is your Given I am logged in step
When I am on sign up page
Then i should be redirect to dashboard page
I would get rid of the background clause entirely - it's unnecessary detail. Your scenarios make perfect sense without it.
You can visit the signup page, and verify the user isn't already signed in, as part of your "When I sign up with valid fields" step definition.
Why don't you create a step definition that closes that session?
something like this:
Then(/^I close the browser$/) do
page.driver.quit
end
Then you do whatever you need in your subsequent steps
If you have this need then you have identified a new feature. So instead of trying to be clever with cucumber you can just take the simpler approach of being clever with naming your features.
With your example the scenario User already signed in clearly has nothing to do with signup, it belongs in a different feature e.g.
features
- signup.feature
- signed_up.feature
Other names for signed_up might be default_navigation, first_sign_in etc. etc.
Wanting a different background for a different part of a feature is always an indicator that you have two different bits of behaviour you are exploring.
Maybe you can name your feature files using numbers in which 0_exaple1.feature file you can run those test cases where you are using the Background and 1_exaple.feature file you can run those cases which you want to run without background and provide the same tag in both the feature file and give that tag in runner file
your runner file is something like this:
#CucumberOptions(
features = {"src/test/resources/features"},
plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"},
glue = {"com.company.comapnies.stepdefinitions"},
tags = "#test"
)
and under the feature folder, your file should be saved like this
feature
|_
0_example.feature
1_example.feature
and your 0_example.feature file like this:
Feature: Sign Up
In order to access to protected parts of site
A user
Should sign up
Background:
Given I am on sign up page
And I am not logged in
#test
Scenario: User sign up succesfully
When I sign up with valid fields
Then I should view dashboard page
and 1_example.feature file like:
Feature: Sign Up
In order to access protected parts of the site
A user
Should sign up
#test
Scenario: Run this scenario without background
Given: Launch the browser
And: Do something
So when you run your test runner file then it will automatically pick the #test tag, search in 0_example.feature first, and then, 1_example.feature file will start executing.
This is the only thing you can run in your framework as of now to prevent background runs.
Cheers!!!

Resources