Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Cucumber Testing

 Cucumber is a widely used tool for Behaviour Driven Development


because it provides an easily understandable testing script for
system acceptance and automation testing.
 In the Cucumber testing, the test cases are written in a simple
English text, which anybody can understand without any technical
knowledge. This simple English text is called the Gherkin
language.

Gherkin offers the following specific keywords to write the


common test scripts in the feature file
 Feature
 Scenario
 Given
 When
 Then
 But
 And
 Background

Feature

Each feature file of Cucumber testing starts with a feature keyword. It is


a standalone unit or functionality to be tested. For example, login
feature, payment transfer feature, registration feature, etc.

Scenario
Each feature contains the required number of tests to test the feature.
Each test is named as a Scenario.
For example, feature login functionality can contain two scenarios, first for a
successful login and second for unsuccessful login.

Feature: Login

Scenario: Successful Login with Valid entries


Given user navigates to the website javatpoint.com
And user logs in through Login Window by using Username as "USER" and
Password as "PASSWORD"
Then login must be successful.

Scenario: Unsuccessful Login with Invalid entries


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

Given

This keyword refers to the pre-condition of the test. For example, to


access any web application, the first requirement or precondition is to
navigate its home page, because, from the home page, we can navigate
to other links such as signup, login, etc.
When
It usually refers to the actions of a user that is to be executed.

Then
This keyword refers to the outcome of the previous step or upcoming
action.
But
This keyword is used to add negative conditions.

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

Background
This keyword defines the steps common to all tests in the feature file.
For example, Navigation to Home Page, click on the Login, Enter
Username and Password, Click on Submit button are the common steps
in almost all web applications.

To Implement Cucumber Framework


 Create Maven Project

 Add Maven Dependencies


1. Cucumber Java (io.cucumber)
2. Cucumber JUnit (io.cucumber)
3. JUnit
4. Selenium Java

 Download Cucumber plugin from Eclipse Marketplace.

 Create a folder in src/test/resources to store Feature files


 To Create Feature file
Right click on the Folder Select file and give filename. feature as extension.
Example: Login.feature
 Add the Content in feature file by using Gherkin language.
Example:
Feature: As a user, I want to create credentials
@ValidCase @Smoke
Scenario: Successful login using valid account
Given Login form in login page
When Submit email and password
Then Success login to home page with displaying account button

In Cucumber we have Scenario Outline which is used to pass multiple


data.
Scenario Outline: Successful login using valid account
Given Login form in login page
When Submit <email> and <password >
Then Success login to home page with displaying account button

Examples:
| email | password |
| name1 | pass 1 |
| name2 | pass 2 |

Tags
It is a concept used in cucumber. We can add tags to features and
scenarios.
By using @ symbol, we can add tags.
Ex:
@Login Test
Feature: As a user, I want to create credentials
@Smoke Test
Scenario: Successful login using valid account
Given Login form in login page
When Submit email and password
Then Success login to home page with displaying account button

These tags are used to run specific Features or Scenarios.


To comment on anything in feature file hash (#) symbol is used.
# Author
# Date
# Description

Step Generator
In cucumber we have Step Generator used to implement the script
based on Feature file.
After completion of feature file Right click and run as cucumber Feature
Then it will give us Script based on Feature file in console.
Copy the Script and implement it in Step Generator.
In step definition we have Regular Expressions which are ^ and $
^ indicates Starting
$ indicates Ending

Example:
@Given("^login page$")
public void login_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Submit emailid and password$")
public void submit_emailid_and_password() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^Success login to home page$")
public void success_login_to_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Test Runner
Test Runner is used to run multiple feature files and generate reports in
different formats.
We can run required features or scenarios from test runner.

Example:
@RunWith(Cucumber.class)
@CucumberOptions(
features = "./src/test/resources/Features/file.feature",
glue = "stepDefinations",
tags = {"@LoginTest"},
plugin = {
"pretty",
"html:target/cucumber-reports/cucumber-pretty",
"json:target/cucumber-reports/CucumberTestReport.json",
"junit:target/cucumber-reports/CucumberTestReport.xml"
}
)
public class TestRunner {
}
We need to create a new class for Test Runner.
Above the class we need to write
@RunWith(Cucumber.class)
@CucumberOptions(
features = "Feature file path",
glue = "StepDefinations package name",
Tags= {"@Tag name"},
plugin = {
"pretty", // Format
"html:target/cucumber-reports/cucumber-pretty", // generate Html report
"json:target/cucumber-reports/CucumberTestReport.json", // Json Report
"junit:target/cucumber-reports/CucumberTestReport.xml" // Junit report
}

You might also like