Rubinium: The Ruby code generator for Selenium

Rubinium is a tool that makes writing Selenium tests easier. Here’s a quick comparison:

A Selenium test:

<html><head><title>HelloWorld</title><head> <body> <table border=“1”> <tr><td colspan=“3”>HelloWorld Test</td></tr> <tr><td>open</td><td>/index.php</td><td>&nbsp;</td></tr> <tr><td>assertTitle</td><td>Hello World</td><td>&nbsp;</td></tr> <tr><td>assertLocation</td><td>/index.php</td><td>&nbsp;</td></tr> <tr><td>assertTextPresent</td><td>Hello World</td><td>&nbsp;</td></tr> </table> </body> </html>

And here’s the corresponding Rubinium test:

class HelloWorld < Rubinium::TestBase open “/index.php” assertTitle “Hello World” assertLocation “/index.php” assertTextPresent “Hello World” end The added immediate benefit is that the Rubinium test can be executed and if the actions/assertions aren’t valid the test will raise an exception. If the actions/assertions are valid, the test will exit without generating any output.

Download

The latest version of Rubinium can be found at

GEM Installation

Download and install rubinium with the following.

gem install --remote rubinium

Simple Example

Here’s a simple example that shows a mixture of Selenium and ruby:

require ‘rubygems’ require_gem ‘rubinium’

class SiteLive < Rubinium::TestBase HOST = “localhost:8080” MAIN_PAGE = “/index.html”

open HOST assertLocation MAIN_PAGE end

Notice that the host and main page are represented as ruby constants and the constants are used in the test, rather than storing the value directly in the test. This helps keep your tests DRY.

Here’s the Selenium test that gets generated:

<html><head><title>SiteLive</title></head>
  <body>
    <table border="1">
      <tbody>
        <tr><td colspan="3">SiteLive Test</td></tr>
        <tr><td>open</td><td>http://localhost:8080</td><td>&nbsp;</td></tr>
        <tr><td>assertLocation</td><td>/index.html</td><td>&nbsp;</td></tr>
      </tbody>
    </table>
  </body>
</html>

Test cases get aggregated into test suites. Here’s a simple test suite that contains the test case from above:

require ‘rubygems’ require_gem ‘rubinium’

Rubinium::TestSuite.new do |suite| suite.name = “MainSuite” suite.filename = “main-suite.html” suite.basedir = File.dirname(__FILE__) suite << SiteLive end.generate

The easiest way to create a test suite is to use the (probably familiar) block syntax. Individual tests are appended to the suite using the &lt;&lt; operator. After the suite has been initialized, you should invoke the generate method to generate the actual Selenium tests.

A test suite has three attributes, all optional: name, filename, and basedir. The first two allow you to set the name that will be displayed in the Selenium test and the filename of the generated Selenium suite. The basedir attribute is the directory where the files will be generated. While you can set the basedir directly in the suite, a more common way of setting it is by passing it as a command line parameter. E.g.,

ruby rts_main.rb tests/generated-tests

This usage is particularly amenable to Rake tasks:

TEST_PATH = “test/tests” GENERATED_TEST_PATH = “test/generated-tests”

ACCEPTANCE_TESTS = %w( rts_main.rb ).map {|test| File.join(TEST_PATH, test)}

desc “Generate selenium tests from rubinium tests” task :rubinium => [:clean, :environment] do ACCEPTANCE_TESTS.each do |test| ruby “#test #GENERATED_TEST_PATH” end end

NB: If basedir is supplied both as a command line parameter and as an explicit attribute in the suite, the command line parameter will be ignored.

More examples of Rubinium tests and suites are provided in the doc/examples directory.

Credits

The Folks at Thoughtworks

For creating Selenium.

Jim Weirich

For creating Rake and writing extensively and clearly about DSLs and metaprogramming in Ruby.

why_the_lucky_stiff

For inspiring me with Dwemthy’s Array

Stephen Baker

For the RSpec Rakefile I liberally pillaged from.

License

Rubinium is available under an MIT-style license.

:include: MIT-LICENSE

Support

The Rubinium homepage is rubinium.thinkpig.org. You can find the Rubinium RubyForge page at rubyforge.org/projects/rubinium.

Feel free to submit commits or feature requests. If you send a patch, remember to update the corresponding unit tests. Patches are much less likely to be adopted if I have to write the tests.

For other information, feel free to contact [email protected].