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)

Preface

Automating tests for user interfaces has always been the holy grail of programming. Now, using Zombie.js and Mocha you can quickly create and run your tests, making it simple to test even the smallest changes. Increase your confidence in the code and minimize the number of times you have to use a real browser while you develop.

Using Node.js for UI Testing is a quick yet 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 behavior and verify that your application behaves correctly.

You will create a web app in Node.js that uses complex user interactions and AJAX; by the end of this book 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.

You will also create a complete test suite, module by module, testing simple and complex user interactions.

What this book covers

Chapter 1, Getting started with Zombie.js, helps you to understand how Zombie.js works and what types of applications can be tested with it.

Chapter 2, Creating a Simple Web App, explains how to create a simple web app using Node.js, CouchDB, and Flatiron.js.

Chapter 3, Installing Zombie.js and Mocha, teaches you about creating the base structure of a test environment for a web application using Zombie.js and Mocha.

Chapter 4, Understanding Mocha, helps you to understand how you can use Mocha to create and run asynchronous tests.

Chapter 5, Manipulating the Zombie Browser, explains how Zombie.js is used to create a simulated browser that can load an HTML document and perform actions on it.

Chapter 6, Testing Interactions, explains how to trigger events in a document and how to test the results of document manipulations.

Chapter 7, Debugging, teaches you how to inspect the internal state of your application by using the Zombie browser object and some other techniques.

Chapter 8, Testing AJAX, is not present in the book but is available as a free download at the following link:

http://www.packtpub.com/sites/default/files/downloads/0526_8_testingajax.pdf

What you need for this book

To use this book you will need a PC running a modern mainstream operating system such as Windows, Mac, or Linux.

Who this book is for

This book is for programmers who use and somewhat understand JavaScript, especially having some experience with event-driven programming. For instance, if you have used JavaScript in the context of a web page for setting up event callbacks and making AJAX calls, you will experience a smoother learning curve. Alternatively, some experience in using Node.js will also ease the learning curve, but is not an absolute requirement.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: " To access a CouchDB database from Node you will use a library called nano."

A block of code is set as follows:

browser.visit('http://localhost:8080/form', function() {
  browser
    .fill('Name', 'Pedro Teixeira')
    .select('Born', '1975')
    .check('Agree with terms and conditions')
    .pressButton('Submit', function() {
      assert.equal(browser.location.pathname, '/success');
      assert.equal(browser.text('#message'),
        'Thank you for submitting this form!');
    });
});

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

  "scripts": {
    "test": "mocha test/users.js",
    "start": "node app.js"
  },...

Any command-line input or output is written as follows:

$ npm install
...
[email protected] node_modules/mocha
...

[email protected] node_modules/zombie
...

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking onthe errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.