Selenium

Up to this point, we've been focusing entirely on unit testing, but it is worth mentioning that there are many other forms of tests that can benefit a Backbone application, such as load tests, smoke tests, and usability tests. However, a detailed discussion of all these tests is outside the scope of this book, and in most workplace environments, these tests won't be the purview of the development team anyway. Instead, they will be managed by the quality assurance (QA) department.

However, there is one type of testing, acceptance testing, which is worth mentioning. In smaller organizations that lack a QA department, acceptance tests will often be created and maintained by developers, and even in larger organizations, it's not unheard of for developers to assist with the creation and maintenance of these tests.

Acceptance tests test the functionality of your site, by checking whether or not a user can perform a specific action. A given acceptance test might check whether the user can log in to a site, change his password, or place an order. Unlike unit tests, which test a small piece of the larger functionality (such as a single placeOrder function), acceptance tests validate all of the disparate functions that are used to complete a particular user interaction.

On the web, there is a single tool that almost completely dominates acceptance testing: the Selenium web driver (http://www.seleniumhq.org/). The Selenium web driver allows you to create automated tests that perfectly simulate the actions of a real user on your site. A Selenium test can click a button, fill in a text field, scroll, and do just about anything else that an actual user can do.

Selenium is available for many different languages, including JavaScript. It can either be used directly via the Selenium web driver (https://code.google.com/p/selenium/wiki/WebDriverJs) or be wrapped with a library that offers an alternate syntax such as Nightwatch.js (http://nightwatchjs.org/). Here's an example from the Nightwatch.js home page that demonstrates a simple acceptance test written with Nightwatch.js:

module.exports = {
    'Demo test Google' : function (client) {
        client
          .url('http://www.google.com')
          .waitForElementVisible('body', 1000)
          .assert.title('Google')
          .assert.visible('input[type=text]')
          .setValue('input[type=text]', 'rembrandt van rijn')
          .waitForElementVisible('button[name=btnG]', 1000)
          .click('button[name=btnG]')
          .pause(1000)
          .assert.containsText('ol#rso li:first-child',
            'Rembrandt - Wikipedia')
          .end();
  }
};

As you can see from the preceding example, Nightwatch.js (and Selenium itself) uses the same selectors that you already use for CSS and jQuery, along with special methods such as waitForElementVisibility to control timing and prevent the automation script from moving too quickly through the test. This allows you to simulate any user story you want, no matter how simple or how complex it is, and then, repeat this story over and over to test your site.

However, Selenium-based tests do have their limits with the biggest being that because Selenium operates on the user level, it has no awareness of what's going on at the code level. If a Selenium test fails, it won't provide a stack trace or point you to a line of failing code; instead, it will simply alert you that a particular operation failed, and it will be up to you to debug it manually, just as you would with a user-submitted bug. Ideally, you should try and catch as many failures as you can in your unit test suite and only rely on Selenium-based acceptance tests to find bugs that "slip through the cracks" in your unit testing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.188.160