In this second part of a three-part article series on acceptance testing with Ruby on Rails, you will learn the various commands that are performed during this process, and start your first acceptance test. This article is excerpted from chapter 11 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Commands for Acceptance Testing - The First Acceptance Test (Page 4 of 4 )
Now we'll write a simple acceptance test for the View Forum user story, which we implemented in Chapter 6. In the next section, we'll show you how to speed up the process of creating tests by using the Selenium IDE extension for Mozilla Firefox.
Create the first acceptance test by executing the generate script:
Open 01_view_forum.sel in your editor and modify it to contain the Selenese commands shown here:
|open|/forum/| |assertTitle|Forum| |assertTextPresent|Forum| |assertTextPresent|There are no posts|
When Selenium executes the View Forum test case, it will perform the following actions in the browser:
Open the /forum URL.
Verify that the title of the page is Forum.
Verify that the text "Forum" can be found somewhere on the page.
Verify that the page contains the text "There are no posts." Note that this requires that the test database is empty. You can empty the table manually or have the script do it, as we'll show you in the next sections.
Selenium tests are stored in the test/selenium directory. It is good practice to group related tests into test suites, which is why we put the forum acceptance tests in a folder called forum. Selenium on Rails creates test suites automatically by scanning the test/selenium directory for subdirectories and acceptance tests. Acceptance tests are sorted by their filenames, which are run sequentially. In our case, the test suite will be created from the test/selenium/forum folder.
Tip If you need the tests to run in a specific order, prepend their filenames with a number. For example, a test suite containing a login and logout test could have files named 01_login.sel and 10_logout.sel, so that the login test runs before the logout test. The same naming scheme can be used with test suite directories.
By default, Selenium scripts are enabled only for the test environment. This means you need to start WEBrick in test mode by executing the following command:
$ script/server -e test
Before running the test, make sure your test database has been updated to the latest version. This can be done with the rake command:
$ rake db:test:clone_structure
You can now execute the test we just created by issuing the following command:
$ rake test:acceptance
This will open all the browsers that you specified in the Selenium on Rails configuration file, one by one, and execute the View Forum test case in your browser. After the tests have run, you should see the test result page, as shown in Figure 11-1.
Figure 11-1.The Selenium TestRunner after a successful test run
Successfully executed commands are shown in green. Failures are in red. The table at the bottom of the page shows detailed statistics of the test.
Recording Selenium Tests
Writing acceptance tests requires detailed knowledge of how Selenium works (and a lot of typing). The good news is that Selenium IDE simplifies acceptance test creation. Selenium IDE is an integrated development environment that has been implemented as an extension available for Mozilla Firefox only.
Please check back tomorrow for the conclusion to this article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.