JavaScript Functional Testing with Nightwatch.js

A while back, Eric Elliott wrote JavaScript Testing: Unit vs Functional vs Integration Tests, in which he explained the different types of test and when to use which.
In today’s article, I would like to go into JavaScript functional testing in a little more depth. To do so, we will explore and use the Nightwatch.js library.
But before getting started, allow me to remind you what a functional test is, and why it matters. Roughly speaking, functional testing is a process aimed at ensuring that an application is working as expected from the user’s perspective.
We are not talking about technical tests, such as unit or integration tests. Here, the goal is to make sure that a user can seamlessly execute a certain scenario, such as sign in to a platform, buy a product, and so on.
Introducing Nightwatch.js
Nightwatch.js describes itself an a Node.js powered end-to-end testing framework. It relies on Selenium, a project aimed at facilitating web browser automation.
Through a human-friendly syntax, Nightwatch.js makes it possible to “script” scenarios, which are then automatically played by a browser (not necessarily headless).
Installing Nightwatch
Nightwatch is itself a Node.js module, which means you will need Node installed on your machine. The easiest way to do this is using a version manager such as nvm. Nightwatch is distributed on npm, so you would install it like any other module—either globally with -g, or inside the current project with –save-dev.
npm install –save-dev nightwatch

Nightwatch relies on the Selenium WebDriver API and consequently needs a Selenium WebDriver server. This runs on Java, which means, you also have to install the Java Development Kit (JDK 7+) on your environment. You can download the JDK from the Oracle website.
Once downloaded and installed, you can make sure that Java is correctly available on your machine with java -version. The last step is to download the Selenium standalone server packaged as a jar from the Selenium downloads page. I recommend you put it inside a bin folder inside your project.
your_project/
|
|– bin/
| |– selenium-server-standlone-2.53.1.jar
|
`– package.json

Okay, we’re all set now. Let’s get started.
Configuring Nightwatch
As you can imagine, Nightwatch has a lot of configuration. Fortunately, we don’t have to know everything to get started. The configuration can either live in a nightwatch.json file or in a nightwatch.conf.js file at the root of your project. I would recommend the later as it is a little more flexible, as well as giving you the ability to add comments.
var SELENIUM_CONFIGURATION =
start_process: true,
server_path: ‘bin/selenium-server-standalone-2.53.0.jar’,
host: ‘127.0.0.1’,
port: 4444
;

var FIREFOX_CONFIGURATION =
browserName: ‘firefox’,
javascriptEnabled: true,
acceptSslCerts: true
;

var DEFAULT_CONFIGURATION =
launch_url: ‘http://localhost’,
selenium_port: 4444,
selenium_host: ‘localhost’,
desiredCapabilities: FIREFOX_CONFIGURATION
;

var ENVIRONMENTS =
default: DEFAULT_CONFIGURATION
;

module.exports =
src_folders: [‘tests’],
selenium: SELENIUM_CONFIGURATION,
test_settings: ENVIRONMENTS
;

Note: I personally find easier to read a configuration file when it is split into smaller configuration objects, which a JSON file does not allow.
In our case, we tell Nightwatch that our tests will live in a tests folder, using a certain Selenium configuration, and certain test settings. Let’s go through each chunk:
var SELENIUM_CONFIGURATION =
start_process: true,
server_path: ‘bin/selenium-server-standalone-2.53.0.jar’,
host: ‘127.0.0.1’,
port: 4444
;

With this configuration object, we tell Selenium to run on 127.0.0.1:4444, which happens to be the default value for Nightwatch. We also make sure that it boots automatically using the Selenium server we downloaded and stored in our bin folder.
Note: for more advanced usage, be sure to check the list of all Selenium options.
Moving on to the actual testing setup:
var DEFAULT_CONFIGURATION =
launch_url: ‘http://localhost’,
selenium_port: 4444,
selenium_host: ‘localhost’,
desiredCapabilities: FIREFOX_CONFIGURATION
;

var ENVIRONMENTS =
default: DEFAULT_CONFIGURATION
;

The test_settings option from Nightwatch expects an object whose keys are the names of each environment, mapped to a further configuration object. In our case, we haven’t set up a custom environment (yet) so we use default. Later on, we could have a staging or production testing environment.
In the environment configuration, we tell Nightwatch which URL to open (which would be different for staging for instance), and what browser should be used to run the tests.
Note: for more advanced usage, be sure to check the list of all test options.
Continue reading %JavaScript Functional Testing with Nightwatch.js%

via Reme Le Hane

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s