Introduction to Cucumber using JavaScript and Selenium

Cucumber is a framework to help testers, developers, and business analysts practice Behavior Driven Development (BDD). In BDD, the business analysts, developers, and testers all write their test cases first (test cases are written in terms of end user behavior) before any software is built. They write their test cases in an easy to understand language called Gherkin. Then the developers take these test cases and use it to write software. The testers take the test cases and start automating them. Since they agreed to write their test cases first there should be no misinterpretation between business analysts, developers, and testers. Below, we will automate test cases using Cucumber, JavaScript, and Selenium. In this example, I will demonstrate how to search for the phrase “Canada” on DuckDuckGo.com and verify the Wikipedia link appears on the first page. Note I will be doing this on a Windows machine but you would easily do this on a Mac.


How to Automate your test cases using Cucumber, Selenium, and Javascript

Step 1 Create a New Folder for Your Project

Open the command prompt and create a project called cucumber-javascript. I placed the project here “C:\projects\cucumber-javascript\”

Open cmd prompt
Open cmd prompt

Step 2 Install Cucumber

Once you have created your directory run the command “npm install cucumber”. If you cannot run this command be sure to download Node.js

Install Cucumber
Install Cucumber

Step 3 Install Selenium WebDriver

Now install selenium by running the command “npm install selenium-webdriver”.
Install Selenium Webdriver
Install Selenium Webdriver

Step 4 Install Chrome Driver

In order to use Chrome, you need to install Chrome Driver.  Simply type, npm install chromdriver

Install Chromedriver
Install Chromedriver

Step 5 Initialize Cucumber

Initialize Cucumber
Initialize Cucumber

Step 6 Write your first Gherkin Statement

First, you will need to go into your features directory and write a Gherkin Script to search DuckDuckGo.

Sample Feature File
Sample Feature File

Step 7 Run Cucumber

Now run cucumber by running the command “node_modeuls\.bin\cucumber-js”. When you run cucumber you will notice several warnings.

Step 8 Add Step Definition

Now you need to copy and paste the code that cucumber dumped out in the previous step into the step definition file. Now go into \features\step_definitions file and add the file DuckDuckGo.js

Copy and paste methods from Cucumber
Copy and paste methods from Cucumber

Step 9 Add Code for Step Definitions

The next step is to add code for the methods created by cucumber. Note in the screenshot below I removed the parameter “callback” in the methods.

Code for Step Definitions
Code for Step Definitions

Step 10 Configure env.js

When you go into your features/support folder you will find an env.rb file.  This is a ruby file but since we are writing in JavaScript we need to create an env.js file. This file will basically instruct cucumber that we are going to use Chrome.

env.js file
env.js file

Step 11 Configure hooks.js

The hooks file will simply close the browser once your test case has finished executing.

hooks.js
hooks.js

Step 12 Run Cucumber

Run Cucumber: Test Automation using Cucumber JavaScript and Selenium
Run Cucumber: Test Automation using Cucumber JavaScript and Selenium