Introduction
Hello friends! Today, I am going to briefly explain CucumberJs basics in Protractor. Before that, let's get to know what the Protractor is.
What is Protractor?
Protractor is a node.js framework that is built on top of Selenium Webdriver APIs. It is basically used to automate angular applications. It contains the features that are available in selenium web driver. In addition to selenium locators, we have protractor locators that are helpful to automate Angular applications.
Before talking about Cucumber, let's see the installation details.
- Jdk 1.8 needs to be installed (https://www.oracle.com/in/java/technologies/javase/javase-jdk8-downloads.html)
- Download the node js (https://nodejs.org/en/download/)
- You need to write and run scripts on editor, I am using Visual studio code editor in this article, which is open-source and popular in the market. So you can download it from (https://code.visualstudio.com/).
After installing the above required software, you can verify it using the command prompt.
To check the installed JDK:
- java -version
- node -v
After installing the VisualStudio code editor. You can also use to run the code using the terminal.
Create a folder in the local system Ex: "Cucumberbasics" -> Open the folder in Visual Studio code.
- npm init
It just basically ask you certain details like package name. So you can give any package name (ex: cucumberjsBasics"). Please see the below screenshot

After entering the package name, it will ask you certain details like gitversion, authorname, keywords etc. you can enter the details or you can skip by clicking on enter button.
Refer to the image below. You can see the package.json file generated with the details you entered. You can edit the changes in package.json file also.

Configuring CucumberJs node package
You can refer to the npm cucumber package installation in https://www.npmjs.com/package/cucumber
You can download this in VisualStudio code terminal, as shown below.
After downloading in the left pane you can see "node_modules" folder. Expand the folder and you can see the "cucumber" folder-> bin--> cucumber.js this file helps you to execute cucumber tests.

Installing Gherkin Plugin
As you all know cucumber uses "Gherkin" language, Where it is written in simple plain English language with certain sets of keywords like "Given, When,Then". We have installed the cucumber package in the above example, that is specific to the above project. After installing the package we need to install the Gherkin plugin, To write the cucumber scripts.
Creating first feature file
As you know cucumber works on Gherkin, we need to create .feature file first. So first create a folder with name "feature" and inside the feature folder create "stepdefinition" folder. Create a "Login.feature" file inside the "feature" folder.
First, let's take the simple example of a user login. You need to enter the below code in the .feature file.
- Feature: Login
- In order to login to the application
- As a User
- I need to enter the Valid username and Password
- Scenario: In order to login to the angular app
- Given user navigates to the angular app website
- When user enters the Vaid username and password
- Then user should be successfully loggedin to the application

In order to run this in "Visual Studio code," you need to navigate to "node-module" -> bin-> cucumber.js file this will help you to run the cucumber files.
You need to enter the below command inside the terminal.
- .\node_modules\.bin\cucumber-js features
- .\node_modules\.bin\cucumber-js .\features\Login.feature

Now let's create a step definition file for the "login.feature" file.
- var {Given, When, Then} = require('cucumber');
- Given('user navigates to the angular app website', function () {
- // Write code here that turns the phrase above into concrete actions
- return console.log('Given - user navigates to the angular app website');
- });
- When('user enters the Vaid username and password', function () {
- return console.log('When - user enters the Vaid username and password');
- });
- Then('user should be successfully loggedin to the application', function () {
- return console.log('Then - user should be successfully loggedin to the application');
- });
Adding Multiple Scenarios to the feature file
When you are adding multiple scenarios to the feature file, you should be careful about the repeating scenarios. Let us say for ex: you have two scenarios, one is for login with valid user and one with an invalid user, as mentioned below.
- Feature: Login
- In order to login to the application
- As a User
- I need to enter the Valid username and Password
- Scenario: In order to login to the angular app
- Given user navigates to the angular app website
- When user enters the Vaid username and password
- Then user should be successfully loggedin to the application
- Scenario: In order to login to the angular app with invalid login
- Given user navigates to the angular app website
- When user enters the InVaid username and password
- Then user should not be successfully loggedin to the application
Every regular expression should start with ^/ and ends with $/ and the dynamic values should be like "([^"]*)", for every dynamic value in feature file should be represented with double quotes.
- Feature: Login
- In order to login to the application
- As a User
- I need to enter the Valid username and Password
- Scenario: In order to login to the angular app
- Given user navigates to the angular app website
- When user enters the "Valid" username "valid" password
- Then user should "be" successfully loggedin to the application
- Scenario: In order to login to the angular app with invalid login
- Given user navigates to the angular app website
- When user enters the "InValid" username "InValid" password
- Then user should "not" successfully loggedin to the application
- var {Given, When, Then} = require('cucumber');
- Given(/^user navigates to the angular app website$/, function () {
- // Write code here that turns the phrase above into concrete actions
- return console.log('Given - user navigates to the angular app website');
- });
- When(/^user enters the "([^"]*)" username "([^"]*)" password$/, function (username, password) {
- return console.log("When - user enters the "+ username+" username "+password+" password");
- });
- Then(/^user should "([^"]*)" successfully loggedin to the application$/, function (loginType) {
- return console.log("Then - user should "+ loginType+" successfully loggedin to the application");
- });
Adding Background
In the above example, we have seen multiple scenarios. But there are some repeating line for the above two scenarios in feature file. i.e Given statement, Navigating to the URL is the same for both the scenario. So you can mention it in the "Background". Any repeated statement that is same for both the scenarios can be mentioned in "Background".
Let's see this with example: In the below example, I have removed the "Given" statement from both the scenarios and i have placed that statement in "Background" which you need to declare after the "Feature". If you execute the cucumber test, it should work as same and there is no change in the step definition.
- Feature: Login
- In order to login to the application
- As a User
- I need to enter the Valid username and Password
- Background:
- Given user navigates to the angular app website
- Scenario: In order to login to the angular app
- When user enters the "Valid" username "valid" password
- Then user should "be" successfully loggedin to the application
- Scenario: In order to login to the angular app with invalid login
- When user enters the "InValid" username "InValid" password
- Then user should "not" successfully loggedin to the application

As you can see in the above result. The "Given" statement has been executed twice, since there are two scenarios.
Parameterizing the Scenarios
In the above scenarios, we created 2 scenarios for Valid and Invalid user. Each time, we cannot create the scenario to execute different sets of test data and it is not valid to create so many scenarios for the same execution. In order to avoid this we need to use "Scenario Outline" it allows to execute scenario with different sets of data. We can modify the scenario in the above example like this:
- Feature: Login
- In order to login to the application
- As a User
- I need to enter the Valid username and Password
- Background:
- Given user navigates to the angular app website
- Scenario Outline: In order to login to the angular app
- When user enters the Valid "<username>" valid "<password>"
- Then user should "<loginStatus>" successfully loggedin to the application
- Examples:
- | username | password | loginStatus |
- | Testuser1 | pass123 | passed |
- | Testuser2 | pass222 | failed |
- | Testuser3 | pass333 | passed |
Below is the execution result with different sets of data.







Join the conversation! Your thoughts help the community grow.