|Despite identifying itself as “Web Application Testing in Ruby,” WATIR can perform automated acceptance testing for websites powered by any language, PHP included.|
All but the simplest HTML forms typically have too many variations to be effectively tested manually. The ideal solution is test automation, an approach that allows you to run suites of tests and compare their results with your expected outcomes. (If like me you primarily develop PHP-driven websites, then one popular automated testing solution is PHPUnit. ) Besides the common unit tests, which determine whether individual units of source code operate as expected, you also should implement acceptance tests, which test the website from the user’s perspective.
But implementing acceptance tests means we’re back to square one, right? After all, somebody has to navigate the interface manually and perform tasks such as form completion and submission. That used to be the case, until automated acceptance testing solutions such as WATIR became available. Despite identifying itself as “Web Application Testing in Ruby,” WATIR can test websites powered by any language, PHP included.
In this introduction I’ll demonstrate WATIR’s impressive capabilities by showing you how to create acceptance tests capable of verifying the proper operation of those pesky forms I was complaining about.
WATIR is available as a Ruby gem, meaning you’ll need to install Ruby if it’s not already available on your system. Because Ruby’s installation process varies according to operating system, I’ll just point you to the official documentation for further details.
When you have Ruby installed, install WATIR on Linux or OS X by executing the following command:
%>gem install firewatir
On Linux, you also need to install a Firefox-specific add-on. These add-ons are version-specific, with add-ons available for Firefox 2.x, 3.x, and 3.5. See the WATIR documentation for more information.
On Windows, use the following command:
%>gem install watir
Other versions of WATIR are also available for testing your website on Internet Explorer and Safari; check out the WATIR documentation for more details.
Testing Your Installation
You can verify that WATIR is installed correctly by entering the Interactive Ruby Shell and executing a few commands:
%>irb irb(main):001:0>require 'rubygems' ==> true irb(main):002:0>require
'watir' ==> true irb(main):003:0>b = Watir::Browser.new
Executing this last command should actually result in a new instance of your browser being started! Return to the terminal and execute the following command:
Your newly-opened browser should respond by navigating to Developer.com. This ability to script browser actions is indicative of WATIR’s capabilities. But it can do so much more, read on to see a real-world example.
Testing an HTML Form
Suppose you wanted to perform an acceptance test of a login form. If the login is successful, the user will be transported to a profile page that includes the message, “Welcome back, Jason” (presuming the user’s name is Jason). You can use WATIR to easily create this test. You’ll just need to know the login form URL and the field names comprising the form. That form looks like this:
In the interest of space I’ll omit the PHP code used to process the form. If successful, the user will be redirected to
profile.php and greeted with the aforementioned welcome message.
Of course you should repeatedly run the acceptance tests to make sure something doesn’t break over time. Therefore, instead of using IRB let’s create a new file named
test_login_form.rb, adding the following code to the file:
require 'rubygems' require 'watir' browser = Watir::Browser.new browser.goto "http://localhost/developer/watir/login.php"
browser.text_field(:name, "email").set("email@example.com") browser.text_field(:name, "password").set("secret")
browser.button(:value, "Login").click if browser.contains_text("Welcome back, Jason") puts "Test passed. Test user login successful."
else puts "Test failed. Test user did not successfully login." end
Open a terminal window and execute this script:
When executed, the browser window will open as it did in the earlier example, but this time you’ll watch what amounts to a slow-motion completion of the form, carried out using the instructions in the same order they appear in the test. If you return to the terminal window, you’ll see one of the two concluding messages based upon whether the test was successful! Sure beats manually testing the form, doesn’t it?
A real-world testing environment will involve creating dozens — if not hundreds — of tests, which can be executed at regular intervals. Therefore, you might consider configuring WATIR tests to run in conjunction with
Test::Unit. In any case, WATIR is sure to improve the quality of your code, not to mention make your job much more fun!