0% found this document useful (0 votes)
18 views

Lesson 01 Introduction To Cucumber

Test

Uploaded by

shravani N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lesson 01 Introduction To Cucumber

Test

Uploaded by

shravani N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Automation Testing

Introduction to Cucumber
A Day in the Life of an Automation Test Engineer

Sam is working in an organization as an Automation Testing


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.

To achieve the above with some additional features, he will learn


the basics of Cucumber, including BDD, Gherkins, and key features
that will help him find the solution for the given scenario.
Learning Objectives

By the end of this lesson, you will be able to:

Comprehend Cucumber and its uses

Describe Gherkins and its use

Identify Step Definition

Illustrate Cucumber installation


What Is Cucumber?
Introduction to Cucumber

Cucumber is a testing tool that supports Business Driven Development (BDD)


and it provides a way to write tests that anyone, regardless of tech expertise
can understand.

Before developers write their code,


users write scenarios or acceptance
tests that describe the system's
behavior from the customer's
perspective for review and approval
by the product owners.
Why Cucumber?

These are the reasons why users should use the Cucumber framework:

• It supports different languages like Java.net and Ruby.

• It acts as a bridge between business and technical language.

• It serves the purpose of an end-to-end test framework,


unlike other tools.

• It provides code reusability.


How Does Cucumber Work?
What is Gherkin?
Introduction to Gherkin

Gherkin is a business readable language that allows you to describe business


behavior without getting entangled in the implementation details.
Why Gherkin?

These are the reasons why users should use the Gherkin language:

• It is easy to understand even for non-programmers.

• Programmers can use it as a solid starting point for their tests.

• The business requirements are the focus of Gherkin Testing.

• Gherkin Test cases link acceptance tests directly to automated tests.


Gherkin Syntax

Demo-1

Feature: Title of the Scenario


Given [Preconditions or Initial
Context]
When [Event or Trigger]
Then [Expected output]
Gherkin Keywords

Feature
Background
and Scenario
Scenario outline

But Given

And When

Then
Gherkin Keywords

Feature and Scenario

A feature is a self-contained unit or functionality that needs to be tested.

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

Given and When

Given keyword refers to the pre-condition of the test.

When usually refers to the actions of a user that is to be executed.


Gherkin Keywords

Then and And

Then keyword refers to the outcome of the previous step or upcoming action.

And keyword is used to add more conditions to your steps.


Gherkin Keywords

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.

Given User is on Home Page


And Login Link is displayed
When User Navigates to Login form
And User enters email and Password
Then Login Successfully will be displayed
And Logout Link should be displayed
Gherkin Keywords

But

But keyword is used to add the negative conditions.

Scenario: Unsuccessful Login with Invalid entries


Given user navigates to the website simplilearn.com
And user logs in through Login Window by using Username as "USER" and Password as "1234erty"
But user entered wrong password
Then login must be unsuccessful.
Gherkin Keywords

Background

The steps that are common to all the tests in the feature file are defined by the
Background keyword.

Feature file without Background Feature file with Background

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: Calculator As a user I want to use a calculator to


add numbers So that I don't need to add myself Scenario
Outline:
Add two numbers <num1> & <num2>
Given I have a calculator
When I add <num1> and <num2>
Then the result should be <total>
Examples: | num1 | num2 | total |
| -2 | 3 | 1 |
| 10 | 15 | 25 |
| 99 | -99 | 0 |
|-1 | -10 | -11 |
Basic Terms of Cucumber

These are the basic terms used in the Cucumber framework:

• 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.

• The extension of the feature file is."feature.”


Basic Terms of Cucumber

Tags
.

Tags are used to organize features and scenarios.

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
.

Here is the syntax for single and multiple tests:

Single test Multiple test

@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

Given User is on Home page


When User enters username as "Admin"
And User enters password as "admin123"
Then User should be able to login successfully

@InValidCredentials
Scenario: Login with invalid credentials

Given User is on Home page


When User enters username as "username"
And User enters password as "password"
Then Login will be unsuccessful with error message "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.

When testing an application like this, we should stop testing if


the application fails the first test and continue testing only if the
AND first test was successful.
Basic Terms of Cucumber

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

The syntax of hook:

@Before setup ()

@Before setup ()

logic

} @

Scenario

Given

When

And

Then

@After cleanup (){

logic

}
Why Hook?

During testing, there may be situations where we must take a few conventional preparatory actions before running
the test scenario:

To understand the types of prerequisites that may be encountered during testing,


consider the following prerequisites:

• 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:

• To stop the web driver


• To close the DB connections
• To clear the test data
• To clear the browser cookies
• To log out from the application
• Printing reports or logs
• Taking the screenshots of error
Hook

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:

The hook can also be used with Tag.


We can use it before and after with a specific test.

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

This is an example of step definition in cucumber:

Step 1:

Given (/^ I am on simplilearn.com$/) do

Browser.goto "http://simplilearn.com" -This will visit simplilearn.com on browser

end

Step 2:
When (/^ click on course menu$/) do

Browser.text (:name, " course" ).click – This will click "course menu"

end

Step 3:

Then (/^ I should see course page$/) do

Browser.goto "http://simplilearn.com/category/course/" - It will visit "course


page"

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

This is an example of comments in cucumber:

Feature: annotation

#This is how background can be used to eliminate duplicate steps


Background:
User navigates to Facebook
Given I am on Facebook login page

#Scenario with AND


Scenario:
When I enter username as "TOM"
And I enter password as "JERRY"
Then Login should fail
Why Comments?

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.

• They also help while debugging the code.


What is a Data Table ?
Data Table

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:

Scenario Outline Data Table

• 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

This is an example of a data table in cucumber:

Given the user on the user registration page.


When user enter invalid data on the page
| Fields|| Values|
| First Name | User Name |
| Last Name | User Last Name |
| Email Address | [email protected] |
| Re-enter Email Address | [email protected] |
| Password |PASSWORD|
| Birth-date | 02|
Then the user registration should be successful.
How to Create a Step Definition?

These are the steps to create a step definition:

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?

These are the steps 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?

These are the steps 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 set up a Cucumber development environment:

• Download and install Jdk and Jre


• Download and install Eclipse
• Download and install Maven
• Configure Cucumber with Maven
• Install Plugins
• Update dependencies
Development Environment Setup

These are the steps to install the Cucumber Plugins for Eclipse:

Step 1 • Launch Eclipse IDE

Step 2 • Click on Help

• Then click
Step 3 “Install new
software”
Development Environment Setup

Step 4 • Click on Add

• 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

This is what a POM.xml file looks like:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-


instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MavenDemo</groupId>
<artifactId>MavenDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>

Step 6:
How to add dependencies to the POM.xml file?
Development Environment Setup

These are the steps to add dependencies to a POM.xml file:

● Please visit website MVN Repository –


https://mvnrepository.com/

● Type for Selenium Java in the search box

● Click on the latest version


Development Environment Setup

Copy the content as shown below and paste it in the pom.xml of the project.
Development Environment Setup

The updated pom.xml will look like this.

● Clean and build the project. Click on Project and then select Clean. This will
add Maven dependencies folder to the project.
Development Environment Setup

Adding selenium dependency :

● This will indicate to Maven that Selenium jar files need to be downloaded from
the central repository to the local repository.

● Open the pom.xml in the edit mode


<dependency>

<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
● Create dependencies tag () inside the
<version>3.141.59</version> project tag
</dependency>

● Inside the dependencies tag, create a


dependency tag ()
Development Environment Setup

Adding cucumber-java 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

Adding cucumber-junit dependency:

● 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

Adding cucumber-junit dependency:

● 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

Adding JUnit dependency:

● 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

The image shows the Maven Project that is called CucumberDemo:


Development Environment Setup

This is the image of the pom.xml created:


Development Environment Setup

After adding the above mentioned dependencies, the pom.xml looks like
this:
Development Environment Setup

Entire jar files get added to the Maven dependency:

Congratulations!! We are done with the setup of Cucumber in Eclipse, Happy


Learning.
Key Takeaways

Business and technical language are connected by Cucumber.

Gherkin is easy to understand even for non-programmers.

Tags are used to organize Feature and Scenario.

A feature file is used to write acceptance steps for


automation testing.
Key Takeaways

The hook is the block of code that can be defined with each
Scenario.

Step definition is a java method class with an annotation above


it.

Hook is defined by using annotation @After and @Before.

Testing through multiple tags can be done by using And, OR


operators.

You might also like