Book Image

Using Node.js for UI Testing

By : Pedro Teixeira
Book Image

Using Node.js for UI Testing

By: Pedro Teixeira

Overview of this book

<p>Automating tests for your user interfaces has always been the holy grail of programming. Now, using Zombie.js and Mocha you can create and quickly run your tests, allowing you to test even small changes. Increase your confidence in the code and minimize the number of times you have to use a real browser while you develop.</p> <p>"Using Node.js for UI Testing" is a quick and thorough guide on how to automatically test your web app, keeping it rock solid and bug-free. You will learn how to simulate complex user behaviour and verify that your application behaves correctly.</p> <p>You will create a web app in Node.js that uses complex user interactions and AJAX; by the end you will be able to fully test it from the command-line. Then you will start creating the user interface tests for this application using Mocha as a framework and Zombie.js as a headless browser.</p> <p>You will also create a complete test suite, module by module, testing simple and complex user interactions.</p>
Table of Contents (15 chapters)

Checking the existence of elements


When the Browser.visit() callback is fired, we check for errors. We also check whether the page was successfully loaded if the HTTP response status code was between 200 and 299. These 2XX response codes correspond to the ok request state and are part of the server's way of telling the user-agent that everything went well.

Despite receiving an ok response, we shouldn't take the server's word for granted. We may have received the response status code and an HTML document, but can't be sure that we got the intended document containing the markup for the user signup form.

In our case, we may wish to verify that the document has a heading element containing the New User string and that the new user form elements are present. Here is the code for the complete test:

it('should load the signup form', function(done) {
  Browser.visit("http://localhost:3000/users/new", function(err, browser) {
    if (err) throw err;
    assert.ok(browser.success, 'page loaded');
 ...