Ruby Forum RSpec > Rspec Stories / Selenium Nightmare

Posted by Joseph Wilk (joesniff)
on 08.05.2008 14:22
I have been using Rspec stories with Webrat feeling very productive and
happy.

Then I needed to do something with Selenium (Webrat could have done what
I needed but it does not yet have the functionality).

Selenium-core as part of a rails plugin looked nice but did not seem to
fit with rspec stories. So I went the Selenium-rc route.

Since Selenium uses a separate instance of rails
(http://www.nabble.com/stories-with-selenium-and-the-db-td16190686.html)
I had to turn off the ActiveRecordSafetyListener used in rspec to make
sure the db writes committed.

Which in turn left me having to manually cleanup my selenium stories :(

So that required writing a new, rather gritty scenario listener which
dealt with the cleaning operation. It has to do lots of horrible things
like remove all listeners for a selenium story and then re-add them all
for the others stories.

*Code Extract*

def story_ended(title, narrative)
  case title
  when 'Edit a page'

    #We have finished the selenium story
    $selenium_driver.stop

    #Do we need to re-add some listeners
    if !@listener_reloaded
      Spec::Story::Runner.scenario_runner.add_listener(ActiveRecordSafetyListener.instance)
        @listener_reloaded=true
    end
  end
end


I had to duplicate a lot of the story steps since now any previous
post/gets did not work since they post to the test instance and not the
selenium rails instance.

I also needed to invoke against the selenium driver so even when the
steps would work I had to duplicate them with
$selenium_driver.do_something()


This nice Given:

    Given('log in as a admin user')
      post '/admin/sessions/create', :login => @user.login, :password =>
@user.password
    end

Being duplicated with this

    Given('log in as a admin user')
      $selenium_driver.open '/admin/login'
      $selenium_driver.type 'login', 'developer'
      $selenium_driver.type 'password', 'test'
      $selenium_driver.click 'commit'
    end

After some very painful testing and a lot of time I got my Selenium-rc
and Webrat stories working. This experience really opened my eyes to the
big void introduced by Selenium-rc running outside of the test instance.

This has made me wonder whether I should have rspec stories stepping
outside of the test rails instance to drive Selenium tests.

Has anyone managed to make this process easier?

I'm hoping I'm doing something silly which is making it all harder!

Is it feasible to bring selenium into the test rails instances?

Is it just always going to be painful?

I was skipping along having a lot of fun with stories and Webrat, now
I'm face down in a puddle of mud, dreading that Selenium moment.

--
Joseph Wilk
http://www.joesniff.co.uk
Posted by Zach Dennis (Guest)
on 08.05.2008 17:11
(Received via mailing list)
I've had really iffy luck with Selenium plugins in the past
(selenium-on-rails, seleniumfu_rc, selenium_rc, etc.) so I've started to
write a RailsSeleniumStory. I also had to remove the
ActiveRecordSafetyListener in my efforts.

The RailsSeleniumStory is a part of the mhs_testing plugin [0] and it
provides higher level helpers. For example I love how form-test-helper 
is
used to select and submit forms:

   # option  1
   form = select_form 'expense_form'
   form.expense.amount = 12.99
   form.submit

   # option 2
   submit_form 'expense_form' do |form|
      form.expense.amount = 12.99
      form.expense.category_id = 2
      form.expense.comments = "map for trip"
   end

You can use this same syntax within RailsSeleniumStories. Right now you 
can
also use "have_tag" and "with_tag" matchers with Selenium. It supports 
basic
matching (I wouldn't get to crazy with nesting or lots of
assert-select/have-tag options), but it will be supporting more options
shortly.

So your login example could just look like:

   Given('log in as a admin user')
       open "/admin/login"
       submit_form "login_form" do |form|
          form.login = 'developer'
          form.password = 'test'
      end
   end

Which IMO I really like because if you need variations of that you can 
pull
out a helper method like:

   def submit_login_form(user, password='test')
      submit_form "login_form" do |form|
          form.login = user.login
          form.password = passsword
      end
   end

And you could push your open into a helper as well:
    def go_to_login_page
        open "/admin/login"
    end

And now your Given could look like:
   Given('log in as a admin user')
       go_to_login_page
       submit_login_form @user, 'test'
   end

Now granted submit_form and select_form both take a form's id, so each 
of
your forms need to have one.

If you are interested and have the time please check it out. Granted 
it's in
its infancy and there's not a whole lot of docs right now (there is a
README.Selenium for instructions on how-to setup in your project), but 
you
can find me on GTalk or in irc.freenode.net (zdennis) and of course 
right
here on the rspec ML. I am have 33 scenarios using the 
RailsSeleniumStory,

ttyl,

Zach

0 - http://github.com/mvanholstyn/mhs_testing/tree/master
Posted by David Chelimsky (Guest)
on 08.05.2008 17:16
(Received via mailing list)
Just an FYI - spec-ui, an rspec extension that supports integration
with selenium and watir, has been under some discussion lately and
should see some new life soon.
Posted by Zach Dennis (Guest)
on 09.05.2008 00:27
(Received via mailing list)
David,
Can you provide any more info? Is it just being talked about or are 
people
actively working on it now? I would be interested in assisting. I've 
done a
lot with Selenium and the ruby driver and would be interested in 
providing
beautiful high level helpers that allow people switch from non-Selenium
based specs to Selenium-based specs with more ease. I know this can be a
barrier for people.

Zach

On Thu, May 8, 2008 at 11:15 AM, David Chelimsky <dchelimsky@gmail.com>
Posted by David Chelimsky (Guest)
on 09.05.2008 00:42
(Received via mailing list)
On May 8, 2008, at 5:24 PM, Zach Dennis wrote:

> David,
>
> Can you provide any more info? Is it just being talked about or are  
> people actively working on it now? I would be interested in  
> assisting. I've done a lot with Selenium and the ruby driver and  
> would be interested in providing beautiful high level helpers that  
> allow people switch from non-Selenium based specs to Selenium-based  
> specs with more ease. I know this can be a barrier for people.

The short term plan is that Aslak will set up a spec-ui project up at
github, at which point you, Ian Dees (who ha also expressed an
interested in working on this), and anyone else who wishes can set up
clones. It might take a while before there is a formal release (gem
published to rubyforge), but at least this positions us to move
forward with your assistance.

I don't want to commit to a time frame, but my suspicion is that we
should have git repo up within the next week or two.

Cheers,
David
Posted by Joseph Wilk (joesniff)
on 09.05.2008 14:17
I would be happy to get involved with this project.
I'll be spending most of this year writing Rspec stories and anything 
that makes  browser testing easier will be easy to justify to my work 
colleagues.

David Chelimsky wrote:
> On May 8, 2008, at 5:24 PM, Zach Dennis wrote:
> 
>> David,
>>
>> Can you provide any more info? Is it just being talked about or are  
>> people actively working on it now? I would be interested in  
>> assisting. I've done a lot with Selenium and the ruby driver and  
>> would be interested in providing beautiful high level helpers that  
>> allow people switch from non-Selenium based specs to Selenium-based  
>> specs with more ease. I know this can be a barrier for people.
> 
> The short term plan is that Aslak will set up a spec-ui project up at
> github, at which point you, Ian Dees (who ha also expressed an
> interested in working on this), and anyone else who wishes can set up
> clones. It might take a while before there is a formal release (gem
> published to rubyforge), but at least this positions us to move
> forward with your assistance.
> 
> I don't want to commit to a time frame, but my suspicion is that we
> should have git repo up within the next week or two.
> 
> Cheers,
> David
Posted by aslak hellesoy (Guest)
on 09.05.2008 20:27
(Received via mailing list)
I have just git-svn'ed the code that was formerly in
http://rubyforge.org/projects/rspec-ext to GitHub:

http://github.com/aslakhellesoy

(rspec-distributed and rspec-ui)

I haven't maintained these in a while, so they might be a little
broken. Hopefully someone will clone and improve them.

FYI: The rspec-ui code was written prior to the story framework. Now
that the story framework exists, UI extensions should IMO be against
the story framework - not the RSpec example framework (describe/it).
You'll also see that I'm working on an experimental reimplementation
of the story framework, based on the excellent Treetop parser
generator. The goal of this framework is:

* Stories that are easier to run (separate command line - no need to
write all.rb files)
* Rake task
* Ability to run just one story (a --line switch)
* Before and After blocks
* i18n
* Nice backtraces that go all the way back to the plain text .story 
files
* Less code to maintain
* Easier to hook into for customisations (such as
Rails/Webrat/UI/Watir/Selenium/Celerity/FunFX/screenshot/etc
extensions)

Plus some other cool things I'll save for laters. A lot of the stuff
above is already implemented. Not a lot of specs for the code itself -
I've been driving it mostly by running sample stories. Don't shoot me.

Cheers,
Aslak
Posted by Ben Mabey (mabes)
on 10.05.2008 05:46
(Received via mailing list)
aslak hellesoy wrote:
>   
The DeepTest project has added rspec support and distributed processing
across multiple machines so rspec-distributed may not need the love:
http://github.com/qxjit/deep-test/tree/master


-Ben