[Rspec-devel] Selenium Integration

Wayne Larsen wayne at larsen.st
Tue Aug 22 18:15:15 EDT 2006

Hi All,

In my understanding, using selenium to test your app (I'm speaking
about rails specifically here) can be done several ways:

1. With the selenium_on_rails plugin.  This expands the rails test
directory to include a directory for selenium tests.  This plugin also
includes RSelenese, which is a ruby dsl that evals to the selenium
table testing language.  This allows you to write something like:

open '/admin/users'
click "link=New User"
wait_for_text_present "Login Fields"
type "users_login", "Marie"

The only structure to these tests is based on directories and files.
Directories define a suite, and each test gets its own file.

2. Using selenium-rc and the given framework.  Aslak posted an example
of this using rspec that looked something like:

context "A running iteration" do
  setup do
    @sel = Selenium::SeleneseInterpreter.new("localhost", 4444,
"*firefox", "http://localhost:3000", 5000)

  specify "should allow finishing and tag all stages and stories" do
    @sel.open_and_wait "http://localhost:3000/iterations/1"
    # TODO: verify that the iteration is still 'running' (visual clue)
    @sel.click_and_wait "link=Iteration Status"
    @sel.click_and_wait "link=Finish this iteration"
    # TODO: verify that the iteration is 'finished' (visual clue)

  teardown do

This allows you to make use of rspec's framework for running your
Specs/Behaviours/Examples.  The downsides to this is that the language
for writing tests is not at all like rspec's elegant language, and for
me, I've found that the runtime overhead of selenium-rc is just not
practical for quick iterations.

3. Other options, such as Rubinium (I've only briefly looked at this)
that provides a method of compiling ruby code to selenium test's,
using a dsl pretty much equal to selenium's.

A better way?

OK, given that, and the fact that I'm pretty new to both rspec and
selenium, I would like to propose a different integration approach.
Building off the selenium_on_rails plugin, one should be able to
define a new type of format (say .specenese for argument) that would
look like a normal rspec specification.  The spec would then translate
to the selenese format that can then be executed.  So, essentially,
combining the rselenese format with rspec, to produce something
handwavingly like:

context "Test create user" do
  specify "should create a user when given valid user information" do
    open '/admin/users' and wait
    page.should_include "All Users"
    click 'link=New User'
    fill {:users=>{:login=>'rob', :firstname=>'Rob', :lastname=>'Gordon'}}

By taking advantage of rails conventions and rspec's capabilities, a
higher level functional api could be built.  I've started looking into
this, and believe it shouldn't be extremely difficult to do, but maybe
I'm missing something.

Also, with the api/dsl in place, it might be possible to switch
between browser driven and remote control modes based on some

So questions:
Does this make sense?
Is anyone pursuing anything like this?
Is there a better way that I haven't seen?

I might keep poking away at this if it has any merit, but in the
meantime I need to get back to building the app in the first place, in
which case the best option does seem to be the
rselenese/selenium_on_rails way, which is not really tied to
Test::Unit or rspec.


btw, pardon me for using the t word in the above.  I am new to both
rspec and bdd -- the reason I started investigating this in the first
place, is that it seems that it should be possible for a non technical
user to write the functional specs in the first place, and the easier
that can be done, the better.

More information about the Rspec-devel mailing list