Home arrow Ruby-on-Rails arrow Page 4 - Commands for Acceptance Testing

Commands for Acceptance Testing

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

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
July 29, 2010
  1. · Commands for Acceptance Testing
  2. · Accessor Commands
  3. · Element Locators
  4. · The First Acceptance Test

print this article

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:

$ script/generate selenium forum/01_view_forum

--------------------------------------------  create  test/selenium/forum
  create test/selenium/forum/01_view_forum.sel

Open 01_view_forum.sel in your editor and modify it to contain the Selenese commands shown here:

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

  1. Open the /forum URL.
  2. Verify that the title of the page is Forum. 
  3. Verify that the text "Forum" can be found somewhere on the page. 
  4. 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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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