How do I get selenium to work with webrat?

15 views
Skip to first unread message

JDS

unread,
Mar 13, 2009, 8:44:52 AM3/13/09
to webrat
I've read the posts on the hacks required to get webrat working with
selenium inside cucumber, but I'm just using shoulda inside plain old
integration testing (I did try cucumber and can see its uses when
communicating with non-techies, but my needs are closer to the metal
and it just gets in the way). I've followed the instruction for
setting up to get cucumber working with selenium at
http://wiki.github.com/aslakhellesoy/cucumber/setting-up-selenium and
I can start the selenium server (which seems to use Jetty internally)
OK.

As a start I've got a set of integration tests which all run fine. My
next step was to see what happens when I switch webrat to use
selenium. So I changed

Webrat.configure do |config|
config.mode = :rails
end

to

Webrat.configure do |config|
config.mode = :selenium
end

and run through all the gems & things that have to be installed, then
err... the same tests don't work.

I don't get errors , the process gets as far as saying "started" and
it's been sitting their like that for 15 minutes. When I ctrl-c I can
see that the process has hung trying to get a tcp connection.

Clearly there's more to getting webrat and selenium working together
than installing the gems and changing config.mode, but in the fine
tradition of opensource, it's not documented anywhere.

Has anyone got this kind of config working? Any tips and tricks? I'm
running Rails 2.3.1 on Ubuntu 8.10 with FireFox 3.0.7

Ta

John Small

Joaquin Rivera Padron

unread,
Mar 13, 2009, 9:13:54 AM3/13/09
to web...@googlegroups.com
I have tried that too but did not get it to run, instead I kept going with:

I took the provided selenium example packaged with cucumber, something like this

class YourPage < Selenium::WebPage
  def initialize(browser)
    @browser = browser
  end

  def goto(page)
    @browser.open page
  end
end

Before do
# you need to start the server with:
# $ selenium
# is it slow too start the server everytime?
#   server = Selenium::SeleniumServer.new(BuildMaster::Cotta.new, 4444)
#   server.start

  #TODO move this to some configuration yml?
  # if you note you need to change it, please do so
  @browser = Selenium::SeleniumDriver.new("localhost", 4444,
                                          "*chrome /usr/lib/firefox-3.0.7/firefox-3.0",
                                          "http://localhost",
                                          15000)
  @browser.start
  # you need a server running right?
  # $ script/server -e test --port 4000
  @host ||= 'http://localhost:4000'
  @page = YourPage.new(@browser)
end

After do
  @browser.stop
  @page.close
# if you started the server here, stop it!
#   server.stop
end

then I start my test server and selenium per hand, I know it is possible to start them both inside cucumber hooks but haven tried yet.

also to keep on using my webrat steps I simply construct a response open struct so that asserting steps still work,

def response
  OpenStruct.new :body => @page.html
end

also defined fill_in, click and so on...

def visit(path)
  @page.goto full_url(path)
end

# selenium have no text locator?
def click_button(button)
  @page.button(:id, button).click
end

def click_button_and_wait(button)
  @page.button(:id, button).click_wait
end

I know there must be better ways, but this worked for me

hth
joaquin

Amos King

unread,
Mar 16, 2009, 4:19:10 PM3/16/09
to web...@googlegroups.com
It should work with the mode being set to selenium. The default works
only with Firefox 3 and the Firefox 3 directory must be in your path.

Amos
--
Amos King
http://dirtyInformation.com
http://github.com/Adkron
--
Looking for something to do? Visit http://ImThere.com

JDS

unread,
Mar 17, 2009, 10:40:11 AM3/17/09
to webrat
I'll have to get back to you on this one after I've done some more
experimentation. Currently Firefox is on the path so it ought to work,
but it doesn't.

There must be a detailed explanation in a document somewhere, I can't
believe that I have to trawl through people's blogs & so on to find
out how to do something so basic.

John Small

Balint Erdi

unread,
Mar 23, 2009, 6:06:38 PM3/23/09
to webrat
Hey John,

If you are using webrat with selenium there is no need to start the
selenium driver (@browser) and the rails application manually, since
webrat will take care of everything for you. When you run your
feature, it will launch both of these and close them once the features
have run.

Still, it is not obvious to get them working but it is far from
impossible. See my config here:
http://gist.github.com/83635

And the examples in the cucumber source here:
http://github.com/aslakhellesoy/cucumber/tree/9823af9181103c5a191799a6f358247d473f3a67/examples/selenium_webrat

I hope that helps,
Balint
Reply all
Reply to author
Forward
0 new messages