BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Testing End-to-End with Nightwatch

Testing End-to-End with Nightwatch

This item in japanese

Lire ce contenu en français

Bookmarks

Nightwatch is a recently released acceptance framework based on Node.js that uses Selenium WebDriver API to automate web applications testing. The tool promises a simple syntax which enables the writing of end-to-end tests using JavaScript and CSS selector that runs against a Selenium server.

In opposition to BDD and unit testing that runs in isolation and uses mocks / stubs, an end-to-end test tries to emulate, as closely as possible, the perception a user has to a real system. In the case of a web application this implies firing up a browser, loading pages, running JavaScript, interacting with the DOM and so forth. Nightwatch tries to accomplish this goal using syntax sugar:

this.demoTestGoogle = function (browser) {
   browser
     .url(“http://www.google.com”)
     .waitForElementVisible('body', 1000)
     .setValue('input[type=text]', 'nightwatch')
     .waitForElementVisible('button[name=btnG]', 1000)
     .click('button[name=btnG]')
     .pause(1000)
     .assert.containsText('#main', 'The Night Watch')
     .end();
};

Besides simplifying the process of writing automated tests, Nightwatch can also be integrated in the Continuous Integration pipeline for a complete diagnosis of the system in development.

The current list of features can be found on Nightwatch website:

  • Simple but powerful syntax which enables you to write tests very quickly, using only JavaScript and CSS selectors. No need to initialize other objects and classes, you only need to write the test specs.
  • Built-in command-line test runner which enables you to run the tests either altogether, by group or single.
  • Manages the Selenium server automatically; can be disabled if Selenium runs on another machine.
  • Continuous Integration support: JUnit XML reporting is built-in so you can integrate your tests in your build process with systems such as Hudson or Teamcity.
  • Use CSS selectors or Xpath to locate and verify elements on the page or execute commands.
  • Easy to extend if you need to implement your own commands specific to your application.

In the JavaScript world, currently Selenium is one of most popular tool for acceptance testing alongside with PhantomJS, each one with its own approach: Selenium uses its WebDriver API and PhantomJS a faceless WebKit browser. Both are very mature tools with a strong support from the community. The biggest differences between these tools and Nightwatch are mainly the simplicity in the syntax and the Continuous Integration support. Selenium and PhantomJS have a more verbose syntax, which leads to bigger coding, and do not support out-of-the-box Continuous Integration from the command line (JUnit XML or other standard output) which Nightwatch do.

Nevertheless, Nightwatch is still evolving to become a more mature tool. Sebastien Vincent, author of WD.js another Node.js based acceptance testing framework, has made some critiques on Google Groups regarding the implementation chosen to handle callbacks:

The queue based chain is a bad pattern when async call are involved, as soon as you try to do something complex, or compose a bit, you end up having to manually halt the queue and insert tasks manually (but maybe Nightwatch will prove me wrong).

Vincent also pointed out the weakness in underline communication protocol used between Nightwatch and the Selenium Server:

Nightwatch is far for mature, just have a look at the HTTP protocol, no retry, no timeout setting, content/length+content-type in GET and DELETE. It is going to break pretty quickly in non-straightforward cases like sauce-connect or queuing.

Even with those critiques, the tool has been featured on current month’s GitHub most treading repositories, and the Twitter account is being used to get some feedback from the community and to get in touch with the developer.

Rate this Article

Adoption
Style

BT