Professional Documents
Culture Documents
33) To define the random digit, _____. (\d+) will pass our random digit.
34) In our script file @Given ends with ___$
35) To get random variable ?:love
36) To match random character digit, ______________.*.
37) To differentiate between singular and plural, __________.?:
Before we delve into the features and functioning of the Cucumber tool, we'll
understand the concept of Behaviour Driven Development, commonly known as BDD.
Now, let's watch this video that explains the concept of BDD with an example.
The following comparison shows the Agile principles (left) and how BDD achieves each of
them (right).
Following are few of the tools that work based on the BDD concept:
Cucumber is the most popular BDD tool, which we'll see in detail in this course.
JBehave is a BDD tool for JAVA that is developed by Dan North.
SpecFlow is another BDD tool similar to Cucumber, based on .Net. It is commonly
known as Cucumber for .Net.
EasyB is a BDD framework written in Groovy.
Cucumber is an open-source, behavior-driven development (BDD) tool used for writing
automation tests in plain text (called Gherkin) that supports various software platforms
like Java, Ruby, Selenium, etc.
Originally created as a command line tool in Ruby, Cucumber has been converted to
other development environments like Java.
This course would demonstrate Cucumber with Java and JavaScript. So, let's get started!
Significance of Cucumber
Though we have several other tools that operate on the BDD concept, why should we
prefer Cucumber?
Source of Truth
When we talked about the advantages of Cucumber in the above card, we said
Cucumber feature files could be the source of truth for a project. But how?
In BDD, the developers, testers, analysts, customer, and stakeholders will have
constructive discussions. The outcome of these discussions would be
the examples that describes the expected behavior of the system under
development.
A team uses these examples to run as automated acceptance tests. Thus, as the
team progresses on the solution to the problem statement, using Cucumber, the
team can differentiate the examples that are implemented and those not.
Thus, it provides the source of specification and tests for the system in a single
document.
As mentioned in the previous cards, Cucumber acts as a link between the business and
technical facets of a project.
As shown in the figure above, the working of Cucumber can be divided into business-
facingand technology-facing.
The prerequisites for Cucumber Java installation are JDK and a set of JAR files. The
installation can be done manually or by using a build tool like Maven etc.
The execution of feature files and step definition files can either be done using
Cucumber Command Line Interface (CLI) or an IDE like Eclipse and IntelliJ.
JAR Files
Java Archive files, commonly known as JAR files have a package file format that groups
several classes, its resources, metadata, etc.
The following JAR files have to be included for the proper functioning of Cucumber :
cucumber-core
cucumber-java
gherkin
cucumber-jvm-deps
These JAR files can be downloaded from Maven repository. Go to Maven repository and
search for the JAR names.
Uploaded by info.cukes.
Latest version present in the repository.
The first step is to build a Java project in Eclipse. (See the image above).
This will create a folder with the referenced JARs in the Eclipse project.
Click Help -> Eclipse MarketPlace. Search for Cucumber-Eclipse and follow the steps.
5 of 12
To setup Cucumber in Eclipse IDE using Maven, the following steps should be followed.
<dependencies>
<dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
Run Maven
Once all the dependencies are added in pom.xml, the Maven build tool should be run to
download all the jars.
1. Go to Command Prompt.
2. Enter mvn clean install.
This will show you a BUILD SUCCESS message once the installation is completed.
Gherkin
Before jumping directly on Gherkin, let's understand:
For a project business team will bring the requirements and provide those requirements
to the development team. Doesn't this process looks very easy but the problem starts
here.
Real-Time Problems
Let's talk one requirement.
This information is not completely shared, but the business team will expect this from
the development team.
How to handle it?
Gherkin Structure
Feature: Login to play application
Scenario: When a user logins, with correct username and password. The user should be
directed to the Play homepage.
Hope you have got some idea. You will learn more about writing scenarios
in Feature and Scenario topics.
To achieve all scenario, Gherkin was created.
Gherkin is a
simple
lightweight
structured language, which is regularly spoken.
Gherkin Syntax
Gherkin is a line-oriented language. i.e. indentation is used to define the structure.
Feature
Background
Scenario
Given
When
Then
And
But
*
Scenario Outline
Examples
We'll see how each of these keywords is used, as we proceed in this course.
Parameters:
Datatables:
Data tables can be defined for a scenario when the same steps have to be
executed for multiple data.
The data in the Data table are separated by pipe | symbol.
The first name in the column will be selected as the column name by default.
Example:
|Username|Password|
|User1|Pass1|
|User2|Pass2|
In the above example, the Username and Password will be taken as the column names.
This will be used for data-driven testing in Cucumber.
Language
#language:<language code> header in the Gherkin file implies the language in which the
Gherkin file is written. By default, it takes the English language.
Language code for Norwegian is no, for Japanese ja and so on.
All scenarios in a feature must be written in the same language.
Feature Files
A feature file contains the specifications in the readable language in the form of scenarios
and steps. In short, this will be our Gherkin file.
Feature files give the information on what-to-do.
Feature files will have a .feature extension.
Feature files use the Gherkin syntax that we discussed in the previous topic. There is no
code involved and can be understood by non-technical users too. Thus, feature files
represent the business facet of Cucumber.
Feature
Scenario
In Feature, you will write the test case topic and its description.
Example:
Description
It describes the feature in the feature file.
Single line or multiple lines of text can be entered.
Even though it is an optional field, this is highly recommended to improve the
understanding of the reader.
A feature injection template can be used for writing descriptions in the feature files, as
shown below:
As a type of stakeholder
I want a feature
Example:
Scenario: When a user logins, with correct username and password. The user will be
directed to the Play homepage.
// Scenarios details.
Given User is on the play URL https://play.fresco.me/
When User enter credentials for laptops,
then User is redirected to play home page.
// Rules to be followed.
Scenario Section
As we know, Scenario defines the rule and the steps to be converted for execution.
6 of 11
Given use to put our application in a well-known state before making any execution
process.
Note: Try to avoid writing user interaction in this step. It is a Precondition step
which must be written.
When
Note: Try to avoid writing user interaction in this step. It is a Precondition step
which must be written.
When
And, But
Mostly you can find multiple use of Given and Then.
Example:
Both the above steps execute exactly in the same way. But, from a reading
perspective, the first one is more clear. Hence, we use keywords while writing the
steps in a scenario.
9 of 11
Cucumber-JVM is used for implementing cucumber for JVM languages.
It currently has the support of Java.
Let's configure your system with Java, Maven, and Cucumber JVM.
If you are familiar with Maven, then these steps will be a cake-walk for you.
However, let's try to keep it simple for your understanding.
To create Java Project using maven.
Use this command.
mvn archetype:generate -DgroupId=com.play.bdd -DartifactId=cucumber-
jvm-maven -DarchetypeArtifactId=maven-archetype-quickstart
Note:
If you are willing to learn about Maven, please visit your Play course Maven
- Coalescing Pipeline.
cucumber-jvm-maven
o src
o pom.xml
Now, you have Maven project skeleton. Let's convert this project folder for cucumber
project.
App.java file
AppTest.java file.
$ cd src/main/java/com/play/bdd
$ rm App.jave file
$ cd ~
$ cd cucumber-jvm-mave/src/test/java/com/play/bdd
$ rm AppTest.java
Create a folder resources under src/test,
$ cd ~
$ cd cucumber-jvm-mave/src/test
Go to src/test folder
$ mkdir resources
Create resources folder
$ mkdir cucumber
$ vi play.feature
1. java
2. resources
What is BDD?
Introducing Cucumber
Cucumber - Installation
Gherkin Basics
Feature Files
Cucumber-JVM with Maven
60% Complete
Bookmarks
Hands-on 2
Feature File
You have created a feature file
$i
To insert your script
$ :wq
To comes out of the script.
$ cd ~
Go inside bdd folder src/test/java/com/play/bdd.
$ cd src/test/java/com/play/bdd
Create a folder
$ mkdir runner
What is BDD?
Introducing Cucumber
Cucumber - Installation
Gherkin Basics
Feature Files
Cucumber-JVM with Maven
60% Complete
Bookmarks
Hands-on 2
Runner File
Create a Java file inside the runner folder.
$ vi RunPlayTest.java
Once you have created java file, hit i to insert the code.
package com.play.bdd.runner;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
format = { "pretty", "html:target/cucumber" },
features = "classpath:cucumber/play.feature"
)
public class RunPlayTest {
}
Enter
The Script will execute; however, it will list missing steps for execution.
$ vi PlayStep.java
Hit i and copy this steps which you got in previous execution.
glue = "com.play.bdd.steps",
in @CucumberOptions the path in public class RunPlayTest.java
$ cd cucumber-jvm-maven
Run the script
For your understanding, we have created a folder run and checked the script.
You all have come to this level that you can create a Cucumber script. But you might find
it hard to write nice and efficient step definitions.
Use of Anchors
d represents a digit.
+ means the use of digit ones or more times.
\d+ means at least one digit matches.
. means any character.
.* it means at least one character matches.
Now, you have to design your script so that it handles default use of is and credit for
the singular. For plural, it should use are and credits.
To do so following this:
Random Variable
For example, I have an application where only value change is happening at one
certain of the point. Or a bot which is replying I love play or I adore play or I
like play.
How can we randomize that value in our cucumber
Let's learn how to randomize a variable within the step.
@Given("^I (?:love|Adore|like) play$")
public void i_have_a_X_cucumber_step() throws Throwable {
System.out.println("Step definition with a non-capturing group");
}
4 of 15
Till now we have learned how to get the random value. However, many of you
will be interested in getting data from dataTables.
Let's learn how to get the data in cucumber.
Setup Feature file
What is BDD?
Introducing Cucumber
Cucumber - Installation
Gherkin Basics
Feature Files
Cucumber-JVM with Maven
Diving Deep into Cucumber
60% Complete
Bookmarks
Hands-on 6
DataTable Step
You have seen how to create feature file, let's learn how to call that feature file in your
main script.
This will create a variable and get the value to our script.
6 of 15
Home
My Courses
Explore
Cucumber
What is BDD?
Introducing Cucumber
Cucumber - Installation
Gherkin Basics
Feature Files
Cucumber-JVM with Maven
Diving Deep into Cucumber
60% Complete
Bookmarks
Hands-on 6
Mapping Table
You feel confused, let's make it more simpler for you.
Create a folder named domain in test dbb. The inside domain creates a java file
names CourseList.java. Copy this code in the java file.
package com.play.bdd.domain;
public class CourseList {
You are using getter and setter concept to set the data value. You need a small change in
the Step file.
7 of 15
Home
My Courses
Explore
Tags
The different data in the feature files can be organized by using tags.
Tags must be prefixed by @.
Features, Scenarios, Scenario Outlines and Examples can be tagged.
When a feature is tagged, the tag is inherited by all the scenarios in the feature.
Example:
@Login
Feature:Login functionality
@InvalidLogin
Scenario: Login using Blank username and password
This scenario given above will have two tags: @Login & @InvalidLogin.
Usage of tags improves the quality of documentation. It also provides the capability to
filter features/scenarios based on tags.
Hooks
Cucumber supports hooks, which are blocks of code that are executed before and after a
scenario.
corbetura
cucumber-reporting
mockito-all
selenium-server-standalone
cucumber-junit
junit
These JAR files can be downloaded from the Maven repository, and the selenium-server-
standalone can also be downloaded from the Selenium website.
But, with Selenium, the step definitions is going to look different. It helps us to widen the
capabilities and will allow us to perform actions which we were not able to do,
otherwise.
Learn the basics of Selenium from the Selenium course available in FrescoPlay.
You will learn some dependencies to be included in the pom.xml for selenium.
You will learn how to set up the application set in Feature file.
You will learn how to include our selenium code in script file.
4 of 9
POM Dependency
Let's begin with the POM file.
You need to set pom dependency copy this dependency and set in pom.xml
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.31.0</version>
</dependency>
</dependencies>
Feature file
We need to set this as our resource folder with feature file.
Feature: DuckDuckGo
Moving to next, we will see how to write our code in script file.
Nightwatch
You learned, how to configure Cucumber for Java. Let's take a tour on how to configure
Nightwatch with selenium.
1 of 8
Getting Started
You will learn the basics of how to create a folder and file need for Nightwatch.
create a folder
$ mkdir sample
$ cd sample
Now create a package.json and nightwatch.conf.js files.
$ vi package.json
$ vi nightwatch.conf.js
It's time to create two folder
step_definitions
$ mkdir step_definitions
features
$ mkdir features
It's time to put configuration values in package.json and nightwatch.conf.js.
$ vi package.json
Copy the value for here and paste this value.
{
"name": "simple-example",
"version": "1.0.0",
"description": "",
"main": "nightwatch.conf.js",
"scripts": {
"e2e-test": "nightwatch"
},
"dependencies": {
"cucumber": "^4.0.0",
"cucumber-pretty": "^1.4.0",
"nightwatch": "^0.9.20",
"nightwatch-cucumber": "^9.1.2",
"phantomjs-prebuilt": "^2.1.15",
"selenium-server": "^3.10.0"
}
}
In this package.json, we have defined our dependencies that we need to install for
running Cucumber.
$ nightwatch.conf.js
Copy the following conf details.
require('nightwatch-cucumber')({
cucumberArgs: ['--require', 'step_definitions','--format', 'node_modules/cucumber-
pretty', '--format', 'json:reports/cucumber.json', 'features']
});
module.exports = {
output_folder: 'reports',
custom_assertions_path: '',
live_output: false,
disable_colors: false,
selenium: {
start_process: true,
server_path: seleniumServer.path,
log_path: '',
host: '127.0.0.1',
port: 4444
},
test_settings: {
default: {
launch_url: 'http://localhost:8087',
selenium_port: 4444,
selenium_host: '127.0.0.1',
desiredCapabilities: {
browserName: 'phantomjs',
javascriptEnabled: true,
acceptSslCerts: true,
'phantomjs.binary.path': phantomjs.path
}
}
}
};
step_definitions folder
We have created the folder named step_definitions. Go inside the folder.
$ cd step_definitions
Create a file inside step_definitions folder.
$ vi steps.js
And copy this script.
Cucumber
What is BDD?
Introducing Cucumber
Cucumber - Installation
Gherkin Basics
Feature Files
Cucumber-JVM with Maven
Diving Deep into Cucumber
Using Selenium with Cucumber
Nightwatch using Cucumber
60% Complete
Cucumber Summary
Cucumber Final Assessment
Bookmarks
Hands-on 6
step_definitions folder
We have created the folder named step_definitions. Go inside the folder.
$ cd step_definitions
Create a file inside step_definitions folder.
$ vi steps.js
And copy this script.
5 of 8
Home
My Courses
Explore
We have defined our steps folder, now let's define our features file.
$ cd ..
This command will be used to come out from step_definitions folder.
$ cd features
$ vi duckDuckGo.feature
This will create feature file and copy the value
Feature: DuckDuckGo
Scenario: Searching DuckDuckGo
Given I open DuckDuckGo search page
Scenario: Searching DuckDuckGo again
Given I open DuckDuckGo search page
Run Script
Come out of the features folder
$ cd ..
Run the command to install all the dependency from package.json.
$ npm install
It will install all the dependency.