0% found this document useful (0 votes)
23 views9 pages

Tutorial_6_BackgroundKeyword.docx

This document explains the use of the 'Background' keyword in Cucumber to define pre-conditions for multiple scenarios, reducing repetition of common steps. It provides a user story involving logging into Amazon and checking order details, along with instructions for creating a feature file and implementing the Background keyword. The document concludes with a reference link for further documentation on the topic.

Uploaded by

abhimanyu thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Tutorial_6_BackgroundKeyword.docx

This document explains the use of the 'Background' keyword in Cucumber to define pre-conditions for multiple scenarios, reducing repetition of common steps. It provides a user story involving logging into Amazon and checking order details, along with instructions for creating a feature file and implementing the Background keyword. The document concludes with a reference link for further documentation on the topic.

Uploaded by

abhimanyu thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Way2Automation - Tutorial 6 – Background keyword in Cucumber

What you will Learn :


o About ‘Background’ keyword in cucumber
o User story
o Create feature file to capture scenarios
o Background keyword
o Reference
About ‘Background’ keyword in cucumber
Background keyword is used to define some pre-conditions for all the scenarios. Many a
times, we repeat the same ‘Given’ step in all the scenarios. Since ‘Given’ step is repeated
in every scenario, we can move such repeated steps to the background, by grouping
them under a ‘Background’ section.
User story
The user story or the requirement is that:
We have to launch www.amazon.in and click ‘Sign in’

Next we will enter our user id and click ‘Continue’

Next we enter our passwordand click ‘Sign-in’


Once we login, we will click ‘Your Orders’

We will see our ‘Orders’, ‘Open Orders’, ‘Cancelled Orders’

So we will write 3 scenarios over here:


When I log in and go to order details page, I want to see my previous orders
When I log in and go to order details page, I want to see my open orders
When I log in and go to order details page, I want to see my cancelled orders
Create feature file to capture scenarios
Create a feature file and mention the 3 scenarios, see below
If you carefully notice the 3 scenarios, the below steps are repeated in each scenario

It is not advisable to repeat the steps inside the scenarios.


Background keyword
We will now write all the common steps as part of ‘Background’ keyword, see below.
Delete all the common steps from the 3 scenarios. So we will be left with below. If you
notice, the feature is more readable now. Each scenario is now focussing on what exactly
we want to test as part of that scenario. All the common steps need not be written for
every scenario

So the property of ‘Background’ keyword is that, when you run it, it will be executed
before each and every scenario. So in this case, it would be executed 3 times since we
have 3 scenarios.
So save the feature file and run it
Copy the code snippets from the console window, see below

Create a step definition file and paste it. Import the missing classes
Delete all the throw exceptions

Save the step def file.


Let us run the feature file once again

Notice the console o/p


The o/p is exactly same as we were expecting. All the common steps that we wrote as
part of ‘Background’ section get automatically attached to each and every scenario.
Let us create a runner file

Save the file


Let us run it

We get the same output


Even the junit runner shows 3 scenarios getting passed

So this is how we use the ‘Background’ keyword.


Reference
You can refer https://relishapp.com/cucumber/cucumber/docs/gherkin/background for
further documentation

Thank you for reading!

You might also like