Home arrow Ruby-on-Rails arrow Page 2 - Recording Acceptance Tests
RUBY-ON-RAILS

Recording Acceptance Tests


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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
July 30, 2010
TABLE OF CONTENTS:
  1. · Recording Acceptance Tests
  2. · Recording the View Forum Acceptance Test
  3. · Recording the Post to Forum Acceptance Test
  4. · Recording the Show Post Acceptance Test
  5. · Recording the Reply to Post Acceptance Test

print this article
SEARCH DEVARTICLES

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):

  1. The page title of the forum must be Forum. 
     
  2. 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. 
     
  3. 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.
--------------------------------------------

mysql> delete from forum_posts;

--------------------------------------------
Query OK, 13 rows affected (0.09 sec)
--------------------------------------------

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.

Follow these steps to record the test:

  1. Open http://localhost:3000/forum. You should now see the forum main page showing the text "There are no posts yet." 
     
  2. Right-click somewhere on the page and select Show All Available Commands -> assertTitle Forum from the pop-up menu. 
     
  3. Select the text "There are no posts yet." 
     
  4. Right-click the selected text and select verifyTextPresent There are no posts yet from the pop-up menu. 
     
  5. 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." 
     
  6. 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:

|open|/selenium/setup?fixtures=beer,wine,booze||

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:

|open|/selenium/setup?clear_tables=forum_posts||

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:

|verifyElementNotPresent|//div[@id='posts']/div[21]||

We can test that the navigation between the two pages works by adding the following commands:

|clickAndWait|link=Next page|| |clickAndWait|link=Previous 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[21]||
|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


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials