In episode #136 of Railscasts, Ryan Bates showed us how to quickly turn a regular form into one that is submitted with Ajax.
In this episode you can see how to write a feature to test this functionality using Selenium.
Cucumber is great for Rails integration testing because it exercises the entire Rails stack, starting from the route mapper, through the controller, the models, right down to the database.
To achieve this, it uses Webrat which accesses your application very close to how a browser would do it.
Webrat however is not a browser. When you ask it to fetch a URL from your application, it actually takes a shortcut by instantiating your controllers and passing them a mock HTTP request.
This method however, does not let you test the client-side AJAX functionality of your application.
One solution, shown in this episode, is to use a full featured browser such as Firefox with a well known plugin: Selenium.
Learn how to:
- start with a Webrat feature and turn it into a Selenium feature;
- configure Cucumber with multiple profiles, to separate features using Selenium;
- emulate transactional fixtures using database_cleaner;
- create a new Rails environment for Selenium;
- Write Selenium steps for matching DOM elements.
References:
9 comments
Dan Bikle
9 months ago
RHS of cucumber.yml got chopped off.
I cannot see it.
What is there?
Dan Bikle
9 months ago
My guess:
features/step_definitions/product_steps.rb features/step_definitions/webrat_steps.rb
Dan Bikle
9 months ago
Now I think it is this:
selenium: —tags @selenium -r features/support/env.rb -r features/support/selenium.rb features/step_definitions features
Dan Bikle
9 months ago
At the end I see this on my mac:
Sun Nov 08 13:08 /pt/tt11 maco$ export RAILS_ENV=selenium
Sun Nov 08 13:09 /pt/tt11 maco$ script/cucumber -p selenium
undefined method `use_transactional_fixtures=’ for Cucumber::Rails:Module (NoMethodError)
./features/support/selenium.rb:8
Dan Bikle
9 months ago
I commented out use_transactional_fixtures in both env.rb and selenium.rb
Dan Bikle
9 months ago
Now I see this:
So that I can feel welcome As a general user I want to see a welcome page with 2 columns @selenium Scenario: Show the welcome page to a general user # features/welcome_page.feature:18 Given the server is up # features/step_definitions/welcome_page_steps.rb:3 And I am not logged in # features/step_definitions/welcome_page_steps.rb:7Sun Nov 08 13:17 /pt/tt11 maco$ script/cucumber -p selenium
Feature: Welcome Page
==> Waiting for Selenium RC server on port 4444… Ready!
==> Waiting for rails application server on port 3001… Ready!
and then ffox starts
but ffox cannot open a window
so everything hangs
eventually I see this:
When I go to the homepage # features/step_definitions/webrat_steps.rb:10 execution expired (Timeout::Error) /pt/r1/lib/ruby/1.8/timeout.rb:60:in `rbuf_fill’ /pt/r1/lib/ruby/1.8/timeout.rb:62:in `timeout’ /pt/r1/lib/ruby/1.8/timeout.rb:93:in `timeout’ /pt/r1/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill’ /pt/r1/lib/ruby/1.8/net/protocol.rb:116:in `readuntil’ /pt/r1/lib/ruby/1.8/net/protocol.rb:126:in `readline’ /pt/r1/lib/ruby/1.8/net/http.rb:2020:in `read_status_line’ /pt/r1/lib/ruby/1.8/net/http.rb:2009:in `read_new’ /pt/r1/lib/ruby/1.8/net/http.rb:1050:in `request’ /pt/r1/lib/ruby/1.8/net/http.rb:1037:in `request’ /pt/r1/lib/ruby/1.8/net/http.rb:543:in `start’ /pt/r1/lib/ruby/1.8/net/http.rb:1035:in `request’ /pt/r1/lib/ruby/1.8/net/http.rb:845:in `post’ (eval):2:in `/^I go to (.+)$/’ features/welcome_page.feature:21:in `When I go to the homepage’ Then I should see a page with 2 columns # features/step_definitions/welcome_page_steps.rb:15 And the LHS column should be relatively narrow # features/step_definitions/welcome_page_steps.rb:20 And the RHS column should be much more prominent # features/step_definitions/welcome_page_steps.rb:25 And the LHS column should have the links: login, register # features/step_definitions/welcome_page_steps.rb:30 And the RHS should only contain the word Welcome # features/step_definitions/welcome_page_steps.rb:361 scenario (1 failed)
8 steps (1 failed, 5 skipped, 2 passed)
5m6.380s
13:22:45.476 ERROR – Exception running ’0 ’command on session null
java.lang.NullPointerException: sessionId should not be null; has this session been started yet?
at org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(FrameGroupCommandQueueSet.java:212)
at org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(SeleniumCoreCommand.java:34)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.doCommand(SeleniumDriverResourceHandler.java:516)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.handleCommandRequest(SeleniumDriverResourceHandler.java:368)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.handle(SeleniumDriverResourceHandler.java:129)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
at org.mortbay.http.HttpServer.service(HttpServer.java:909)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:816)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:982)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:833)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:244)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
Sun Nov 08 13:22 /pt/tt11 maco$
Dan Bikle
9 months ago
Maybe some configuration to ffox needs to be done?
Dan Bikle
9 months ago
hello again.
I figured this out.
The jar file in my webrat gem was bad (in my opinion)
I replaced it with the jar file here:
http://release.seleniumhq.org/selenium-remote-control/1.0.1/selenium-remote-control-1.0.1-dist.zip
Now things are working better.
—Dan
sean
4 months ago
This screencast is really helpful to me. Thanks a lot!