Lesson 01 Introduction To Cucumber
Lesson 01 Introduction To Cucumber
Introduction to Cucumber
A Day in the Life of an Automation Test Engineer
He has been asked to use Cucumber to run the test cases. To learn
more about it, he has to first set up an environment to work with
Cucumber.
These are the reasons why users should use the Cucumber framework:
These are the reasons why users should use the Gherkin language:
Demo-1
Feature
Background
and Scenario
Scenario outline
But Given
And When
Then
Gherkin Keywords
Each feature has the required number of tests to ensure that the feature is fully
functional. A scenario is a name given to each test.
Gherkin Keywords
Then keyword refers to the outcome of the previous step or upcoming action.
Example
Feature: Login
Scenario: Login verification
Given user navigates to the website simplilearn.com
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.
But
Background
The steps that are common to all the tests in the feature file are defined by the
Background keyword.
ProgramsBuzz.com
Gherkin Keywords
Scenario Outline
The scenario outline basically replaces the variables/keywords with the value from the
table. Each row in the table is considered to be a scenario.
• Feature Files
• Feature
.
• Scenario
• Tags
.
• Gherkin Language
• Step Definition
.
Basic Terms of Cucumber
Feature Files
.
• A feature file is a shared file that contains the features, scenarios, and
feature descriptions that will be tested.
Tags
.
Purpose of Tags:
• Using tags in Cucumber allows us to create reports for scenarios
under the same tag when we have many scenarios in the feature file
and want to keep them all in one group.
• Cucumber runs every scenario inside the feature file by default, but if
we need to run or skip any scenario inside a certain test, we can
declare scenarios inside a tag.
Basic Terms of Cucumber
Tags syntax
.
@TestName @TestName@TestName
Scenario: Mention the Scenario Scenario: Mention the scenario
Where,
@ is the symbol to declare a tag
TestName is the name of a specific test
Scenario is scenario
Basic Terms of Cucumber
Tags Example
.
@ValidCredentials
Scenario: Login with valid credentials
@InValidCredentials
Scenario: Login with invalid credentials
Note: To declare only those scenarios which are declared under @ValidCredentials, we will write:
tags={“@ValidCredentials”}
Basic Terms of Cucumber
Tags
.
The testing through multiple tags can be done by using two operators:
The next test should be examined if the first test for the
application was unsuccessful. If the subsequent test also fails, a
OR
subsequent test should be examined, and so on.
Tags
.
Syntax of OR and AND:
Tags= {"@FirstTest,@SecondTest"}
OR
AND
tags= {"@FirstTest", "@SecondTest"}
Basic Terms of Cucumber
Tags
.
Example of OR:
qaautomtion.expert
Basic Terms of Cucumber
Tags
.
Example of AND:
qaautomtion.expert
What is Hook?
Hook
The hook makes it easier for users to manage the workflow of the code and reduces code
duplication.
The hook is the block of code that can be defined with each scenario in the step
definition file by using the annotation @Before and @After.
Hook
@Before setup ()
@Before setup ()
logic
} @
Scenario
Given
When
And
Then
logic
}
Why Hook?
During testing, there may be situations where we must take a few conventional preparatory actions before running
the test scenario:
• To start a webdriver
• Setup database connections
• Set up a test data
• Set up browser cookies
• Navigation to a certain page
Why Hook?
During testing, there may be situations where we must take a few conventional preparatory actions before running
the test scenario:
Similar to that, there are always some steps that must be taken after testing:
Hook annotations:
The cucumber only accepts two hooks, in contrast to the TestNG annotations:
•@Before
As the name suggests, we can use the @Before hook with the function/method
after which we need to start the web driver.
•@After
As the name suggests, we can use the @After hook with the function/method
after which we need to close the webdriver.
Hook
Hook Example:
toolsqa.com
Hook
Tagged Hooks:
Syntax:
Before After
@After('@RegressionTest,
@Before ('@RegressionTest’)
@SmokeTest’)
toolsqa.com
What are Step Definitions?
Introduction to Step Definition
Step definition maps the test case steps in the feature files (introduced by
Given/When/Then) to the code.
Introduction to Step Definition
Step 1:
end
Step 2:
When (/^ click on course menu$/) do
Browser.text (:name, " course" ).click – This will click "course menu"
end
Step 3:
end
What are Comments ?
Comments
A comment is essentially a chunk of code intended just for documentation and not for
execution.
Simply place the "#" symbol at the beginning of the statement to include comments.
Comments
Feature: annotation
These are the reasons why users should use comments in cucumber:
• To create files that are simple to read and understand, whether they
are a feature file or a step definition file.
A data table is used when users need to test numerous input parameters.
Data Table vs Scenario Outline
These are the few differences between a data table and a scenario outline:
• It uses Example keyword to define the • No keyword is used to define the test
test data for the Scenario. data.
• It works for the whole test. • It works only for the single step, under
which it is defined.
• Cucumber automatically runs the
complete test the number of times • The test data must first be understood
equal to the number of data in the by a separate piece of code before it
test set. runs once or many times, but just for
single step not for complete test.
Data Table
Step 01:
Click on the File menu in Eclipse > Then select
the option New > Next click on Other
Step 02:
Click on Maven Project from the Maven folder
> Then click on Next
Step 03:
Proceed with the further steps
Step 04:
Select maven-achetype-quickstart template >
Then click on Next
How to Create a Step Definition?
Step 05:
Add GroupId as Automation, Artifact Id as
Cucumber, and proceed
Step 06:
The scripts relevant to cucumber should be
written in the src/test/java folder, and the
project should be built with a structure similar
to a Cucumber project.
Step 07:
Create a new package called stepDefinitions
inside the src/test/java folder
How to Create a Step Definition?
Step 08:
Create a Java class file within the
stepDefinations package > Right-click the
stepDefinations package > then select
option New->Class
Step 09:
Give a Java class file name, say stepDefination
> Then click on Finish
How to Create a Step Definition?
Step 01:
Click on the File menu in Eclipse > Then select the option New > Next click on
Other
How to Create a Step Definition?
Step 02:
Click on Maven Project from the Maven folder > Then click on Next
How to Create a Step Definition?
Step 03:
Proceed with the further steps
Step 04:
Select the maven-achetype-quickstart template > Then click on Next
How to Create a Step Definition?
Step 05:
Add GroupId as Automation, Artifact Id as Cucumber, and proceed
Step 06:
A project should get created with a Cucumber-type project structure. The
cucumber-related scripts should be written within the src/test/java folder.
How to Create a Step Definition?
Step 07:
Create a new package called stepDefinitions inside the src/test/java folder
How to Create a Step Definition?
Step 08:
Create a Java class file within the stepDefinitions package > Right-click the
stepDefinations package > Then select the option New->Class
How to Create a Step Definition?
Step 09:
Give a Java class file name, say stepDefinition > Then click on Finish
Development Environment Setup
Development Environment Setup
These are the steps to install the Cucumber Plugins for Eclipse:
• Then click
Step 3 “Install new
software”
Development Environment Setup
• Write Cucumber
Step 5 in the name
repository
• Type this link
Step 6 “https://cucumber.github.io/cuc
umber-eclipse/update-
site/main/.”
Development Environment Setup
Step 7
• Click on
Next
Development Environment Setup
Step 8
• Click on
Next
Development Environment Setup
After the final step, this is how the interface will look.
Development Environment Setup
These are the steps to create a Maven Project in the Eclipse IDE:
Step 1:
Click on New and then select Project.
Development Environment Setup
Step 2:
Select Maven project and click on Next.
Development Environment Setup
Step 3:
Select “Create a simple project”, check box, and then click on
Next.
Development Environment Setup
Step 4:
Mention the Group Id, Artifact Id, and click the Finish button.
Development Environment Setup
Step 5:
The structure of the image looks as shown in the image.
Development Environment Setup
Step 6:
How to add dependencies to the POM.xml file?
Development Environment Setup
Copy the content as shown below and paste it in the pom.xml of the project.
Development Environment Setup
● Clean and build the project. Click on Project and then select Clean. This will
add Maven dependencies folder to the project.
Development Environment Setup
● This will indicate to Maven that Selenium jar files need to be downloaded from
the central repository to the local repository.
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
● Create dependencies tag () inside the
<version>3.141.59</version> project tag
</dependency>
● This will indicate Maven on which Cucumber files need to be downloaded from
the central repository to the local repository.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.8.1</version> ● Create one more dependency tag
</dependency>
Development Environment Setup
● This will indicate Maven on which Cucumber JUnit files need to be downloaded
from the central repository to the local repository.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.8.1</version> ● Create one more dependency tag
<scope>test</scope>
</dependency
Development Environment Setup
● This will indicate Maven on which Cucumber JUnit files need to be downloaded
from the central repository to the local repository.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.8.1</version> ● Create one more dependency tag
<scope>test</scope>
</dependency
Development Environment Setup
● This will indicate Maven on which JUnit files need to be downloaded from the
central repository to the local repository.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version> ● Create one more dependency tag
<scope>test</scope>
</dependency>
Development Environment Setup
After adding the above mentioned dependencies, the pom.xml looks like
this:
Development Environment Setup
The hook is the block of code that can be defined with each
Scenario.