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 Reply to Post Acceptance Test (Page 5 of 5 )
The Reply to Post acceptance test is run after the Show Post test, and it is used to verify that replying to the post works. The Reply to Post user story is similar to the Post to Forum user story. Record it by following these steps:
Select File -> New Test from the Selenium IDE menu. Verify that Selenium IDE is recording, by checking that the red record button is activated.
We want to verify that we are on the page where the previous acceptance test left us. Right-click somewhere on the page and choose assertTitle 'The Dice Man' from the pop-up menu.
Note If step 2 adds an open command to the acceptance test, remove it from the list. You need to get rid of it because it contains the unique id of the post, which won't be the same when you run the test again.
Click the Reply link. You should now see the reply to post page.
Type a name (for example, George) in the Name field.
Type a subject (for example, Lets play) in the Subject field.
Type a message (for example, Im starting today!) in the Body field.
Click the Reply button. You are taken to the forum main page.
Select the text ("Let's play by George" in our example), right-click it, and select verifyTextPresent Let's play by George from the pop-up menu.
Switch to the Source view by clicking the Source tab. Add the following to the end of the test: |assertLocation|/forum||.
Save the test as test/selenium/forum/04_reply_to_post.sel (by selecting File -> Save Test from the menu).
The test should now contain the text shown here:
|assertTitle|'The Dice Man'|| |clickAndWait|link=Reply|| |type|post[name]|George| |type|post[subject]|Let's play| |type|post[body]|I'm starting today!| |clickAndWait|commit|| |verifyTextPresent|Let's play by George|| |assertLocation|/forum||
We have now created acceptance tests for all of the forum-related user stories. Let's show George the wonder of automation by executing the complete set of Selenium acceptance tests:
$ rake test:acceptance
Firefox starts up, and Selenium then executing our acceptance tests one by one. Figure 11-11 shows the result page, which appears after the tests have run. All tests should be green, which indicates that they passed.
Figure 11-11.Running the recorded acceptance tests
Next, click the Walk radio button in the Execute Tests section of TestRunner window, and then click the All button beneath it to run the tests again. Selenium now runs the tests in a slow-motion mode that allows you to see each command being executed.
You can also try stepping through the tests, by clicking the Step radio button in the Execute Tests section and clicking the All button. Instead of executing the whole test, Selenium stops after each executed command. To continue the test, click the Continue button. This is useful when you're debugging a test.
In this chapter, you learned how to write automated acceptance tests using Selenium and the Selenium on Rails plugin. By automating acceptance tests, you not only remove manual work, but you also raise your confidence that your application works according to the requirements. We also showed you how to simplify the process of writing acceptance tests by using the Selenium IDE Firefox extension to record the tests.
In the next chapter, well show you how to deploy your application to production. There shouldn't be any surprises, as we have implemented a full set of automated tests that exercise almost all parts of the application.
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.