If you've built a complicated application and you want to make sure it works, you perform acceptance testing. This three-part article series will show you how to automate the process. It is excerpted from chapter 11 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
In this chapter, you will learn how to automate acceptance tests. Acceptance tests are important, since they prove that the system works according to the requirements.
Throughout this book, George has been performing acceptance testing quite haphazardly by randomly testing that the user stories we have implemented work as he intended. This is better than no acceptance tests at all, but since we're building an e-commerce site, we want to be absolutely sure that everything works all the time. We don't want to lose a customer by providing a service that doesn't work as expected, and George doesn't have the money to hire a dedicated tester. This is where Seleniuman-- open source testing tool originally developed as an in-house project at ThoughtWorks--comes in handy.
With Selenium and the Selenium on Rails plugin, you can automate acceptance tests that otherwise would be performed manually, or more likely, not at all. You can run the automated acceptance tests when, for example, you refactor code or release a new version. This raises your comfort level by giving you immediate feedback when something breaks.
Using Selenium
The core of Selenium, referred to as Selenium Core, is implemented as JavaScript that runs directly inside your browser, unlike other similar tools like Fitnesse, which run in a separate process. This allows scripts written using Selenium to issue commands like mouse clicks and other actions that mimic the real interaction between a user's browser and a web application. We can, for instance, write a script that simulates George accessing the Emporium forum, filling out the form on the new post page, and then clicking the submit button. Selenium can also check whether the request was successful by, for example, checking that the next page displays the expected content. All these actions are done through Selenium commands, which we'll go through in the "Writing Selenium Tests" section later in this chapter.
Selenium on Rails (http://www.openqa.org/selenium-on-rails), a plugin developed by Jonas Bengtsson, integrates Selenium into the Rails framework. The plugin provides many features to simplify the use of Selenium with Rails. For example, it does the following:
Creates test suites automatically from tests that are located in the same directory. For example, storing a test in test/selenium/authentication/test_login.sel would automatically make the test (test_login.sel) belong to the authentication test suite.
Deploys Selenium to the test environment automatically. (Selenium is not deployed to the development or production environments by default.)
Allows you to write Selenium tests in ERB, Selenese, or RSelenese, rather than just HTML.
Lets you place the Selenium files in a directory other than /public. Selenium can be located in /vendor/selenium or the RubyGems repository.
Allows for the use of fixtures in Selenium tests and clearing sessions. Fixtures are run by accessing the URL /selenium/setup from acceptance tests.