In this conclusion to a three-part article series on acceptance testing with Ruby on Rails, you'll learn how to use the Selenium IDE and how (and why) to record the results of your acceptance tests. This article is excerpted from chapter 11 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Recording Acceptance Tests - Recording the View Forum Acceptance Test (Page 2 of 5 )
We'll start by recording a test for the View Forum user story (originally implemented in Chapter 6). This is the same user story that we wrote a simple acceptance test for in the previous section, but this test will check a few different details. Recall that this user story describes how a user is able to view a list of the most recent posts by going to the forum main page, where posts are shown in a threaded fashion:
First post -->Reply 1 ---->Reply 1.1 -->Reply 2
We will verify that the following requirements are met (in the mentioned order):
The page title of the forum must be Forum.
When the forum is empty, the page must show a message and a link that provides instructions on how the user can create a new post.
When there are more than 20 posts in the forum, the list must be paginated.
Certainly, we could test more aspects of the user story, but this is sufficient for the moment. If something breaks in the future, we can always extend the test.
If not already done, start the Emporium application by executing the following command:
$ ruby script/server -e test
We have enabled Selenium only for the test environment, which is why we start WEBrick using the -e switch.
Next, make sure that the forum_posts table in your test database is empty by executing the following command and SQL:
$ mysql -uemporium -phacked emporium_test
-------------------------------------------- Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 74 to server version: 5.0.20-community
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. --------------------------------------------
You can also tell Selenium to empty the table automatically for you at the start of the test, as well show you later in this section.
Now open Firefox and start Selenium IDE by selecting Tools -> Selenium IDE from the main menu in the browser. You should see the Selenium IDE (see Figure 11-4).
Tip To be able to record tests in Selenese, you have to add a new custom test format. This is done by clicking Add on the Formats tab in the options dialog. The source for the Selenese "wiki-like" format can be downloaded from this page: http://wiki.openqa.org/display/SIDE/SeleniumOnRails.
Select the Selenese output format from the Options -> Format menu in the Selenium IDE window. This makes Selenium IDE generate the acceptance tests in Selenese instead of the default HTML. When started, Selenium IDE is in record mode by default, so you can start recording immediately.
Right-click somewhere on the page and select Show All Available Commands -> assertTitle Forum from the pop-up menu.
Select the text "There are no posts yet."
Right-click the selected text and select verifyTextPresent There are no posts yet from the pop-up menu.
Right-click the "Be the first one to post here" link, then select assertText link=Be the first one to post here from the pop-up menu. This verifies that the page has a link that says "Be the first one to post here."
You have now recorded the first two requirements of the acceptance test, and the Selenium IDE Table tab should look like Figure 11-5. Save the test by selecting File -> Save Test from the Selenium IDE menu.
Figure 11-5.The first version of the acceptance test
Tip You can execute the acceptance test and view the results directly in Selenium IDE by clicking the green play button in the Table tab. You can also watch the test being executed in slow-motion by selecting the Walk mode. Use the Step mode to step through each command in the test manually.
Next, we need to verify that pagination works correctly when there are more than 20 posts in the forum. To prepopulate the database with test data, we can use the following fixture:
<% 40.times do |i| %>post_<%= i %>: id: <%= i %> name: Post <%= i %> name subject: Post <%= i %> subject body: Post <%= i %> body created_at: <%= Time.now.to_s(:db) %> updated_at: <%= Time.now.to_s(:db) %> <% end %>
Save the fixture in test/fixtures/forum_posts.yml. The dynamic fixture adds 40 posts to the database, which means there should be two pages in the forum, and you should see Next page and Previous page links when navigating between the pages.
Selenium on Rails supports fixtures in the same way as the built-in Rails unit, integration, and functional tests. Use the open command to load a special URL that inserts all the specified fixtures into the database. Load multiple fixtures by separating them with commas:
Now add a new command to the test by selecting the line after the last assertText command in the list. Type open in the Command field, and enter /selenium/setup?fixtures=forum_posts in the Target field, as shown in Figure 11-6.
Figure 11-6. Adding a fixture to the test
Now that we are inserting data in the tests, the test will fail if we execute it twice in succession, so we need to make sure that the forum_test database table is empty at the start of the test. We do this by adding the following to the start of the script:
The trick to emptying tables of all data is to use the clear_tables parameter when calling the setup action. To empty more than one table, separate the table names with commas.
We also want to verify that the list is showing exactly 20 posts. We do this with the verifyElementNotPresent command and an XPath element locator that tries to find the twenty-first post, which it shouldn't find on the page:
After adding these commands, the test should look as follows (you can verify this by clicking the Source tab, as shown in Figure 11-7):
|open|/selenium/setup?clear_tables=forum_posts|| |open|/forum|| |assertTitle|Forum|| |verifyTextPresent|There are no posts yet.|| |assertText|link=Be the first one to post here|Be the first one to post here| |open|/selenium/setup?fixtures=forum_posts|| |verifyElementNotPresent|//div[@id='posts']/div|| |clickAndWait|link=Next page|| |clickAndWait|link=Previous page||
Figure 11-7. The source for the View Forum acceptance test shown in Selenium IDE
Save the changes, and then run the test once by clicking the green run button in Selenium IDE.
We can now execute the acceptance tests with rake:
$ rake test:acceptance
The test should pass, as shown in Figure 11-8.
Figure 11-8.The results after running the final version of the View Forum acceptance test