Acceptance Testing in JavaScript with Cucumber.js & WebdriverIO

Published Oct 11, 2016Last updated Jan 18, 2017

When it comes to testing your JS code, it is of utmost importance that you include acceptance tests apart from unit tests.
This is where tools like Selenium or PhantomJS come into play. These tools help us run tests against our JS code in a real browser. If we put
in a bit of effort, we can run those on multiple devices or platforms using services like Sauce Labs or BrowserStack.

Enter WebdriverIO!! WebdriverIO lets you write tests, with actions through which you can control the browser and assert the results. You can use services such as Mocha, Cucumber, or Jasmine.

Here we will discuss how to setup Cucumber.js with WebdriverIO. If you are new to Cucumber.js or have not heard about it.
I would suggest reading about Cucumber.js here. You might not find a lot of information regarding it since it's a bit spread out (might want to use your Google-ing skills ).

While using Cucumber.js for testing, you basically write feature files to test multiple scenarios. For instance, the following is a feature file:

# title.feature
Feature: Title check
I should be able to go to a website
and check its title
Scenario: Get the title of webpage
Given I go to the website "http://www.google.com"
Then I expect the title of the page "Google"

If you've noticed, the syntax is pretty much self-explanatory and readable. You must be thinking, really... that's it ? Well, yes and no.
The code above will work as an entry point to your test, but WebdriverIO (the test runner), does not know what those lines mean. This is where step definitions come in.
Step definitions, as the name suggests is JS code (which the runner understands) for each step in the test. For the example above, the following are the step definitions:

Now we have our test files and step definitions in place, it's
time to link them using WebdriverIO, which will bring all these files together and run it for us.

The folder structure looks something like this:

Before we get started, let's install WebdriverIO and Cucumber deps first.

[sudo] npm i -g WebdriverIO
[sudo] npm i -g wdio-cucumber-framework

In order to start the browser for testing, we need to install and start Selenium. Selenium is the heart of browser automation testing, and WebdriverIO provides binding for Selenium in Node.js.

[sudo] npm i selenium-standalone@latest -g

Once all of them is installed, navigate to the directory where your feature files and step definitions are present—assuming that, we have
them in ~/project_name/test directory. Let's continue.

Invoke WebdriverIO

In order to do so, we need to create a configuration file. To create one, type...

wdio

...inside the test folder. This will prompt you to create a configuration file and give you four options.
For the time being, we will concentrate on the first option as that would help us get WebdriverIO up and running on our local machine.

When prompted, select the following options:

Where do you want to execute your tests? On my local machine
Which framework do you want to use? cucumber
Shall I install the framework adapter for you? No # we already have installed the adapter
Where are your feature files located? (./features/**/*.feature) # this is a glob pointing to the folder where all your feature (test files) are located.
Where are your step definitions located? (./features/step-definitions) # Your step definitions folder
Which reporter do you want to use? dot - https://github.com/webdriverio/wdio-dot-reporter # default reporter
Shall I install the reporter library for you? Yes
Do you want to add a service to your test setup? Selenium Standalone
Level of logging verbosity silent
In which directory should screenshots gets saved if a command fails? ./errorShots/
What is the base url? http://localhost # In case you did not provide a full url in tests

After answering all the questions, it will look like this:

Once that is done, you might want to install the assertion library of your own choice. I am using Chai here. Install and save Chai, using

npm i --save-dev chai

Once you are happy with the configuration, run the tests (in the current directory) using: