New version of Selenium on Rails
Selenium on Rails has moved.
Screencast available here.
New version with partials support.
Update at the end.
I’ve just released a new version of Selenium on Rails. It’s a plugin that makes it easy to test Rails applications through browsers using Selenium.
The major new features are exprimental support for running all tests through a Rake task (which launches any number of browsers) and a new format RSelenese.
The Rake task has a few rough edges so I would appreciate if people could try it out and let me know if anything doesn’t work.
RSelenese is a format, contributed by Eric Kidd, which makes it possible to write the tests in Ruby, like this:
setup :fixtures => :all
open '/'
assert_title 'Home'
('a'..'z').each {|c| open :controller => 'user', :action => 'create', :name => c }
Another change is that the plugin is only available in test environment, but it’s configurable.
script/plugin update (or reinstall the plugin if that doesn’t work)
Check it out and let me know what you think!
Overview
Selenium on Rails provides an easy way to test Rails application with Selenium.
This plugin does four things:- The Selenium files don’t have to pollute /public, they can stay in the Selenium gem or in /vendor/selenium.
- No need to create suite files, they are generated on the fly—one suite per directory in /test/selenium (suites can be nested).
- Instead of writing the test cases in HTML you can use a number of better formats (see Formats).
- Loading of fixtures and wiping of session (/selenium/setup).
Installation
- Selenium needs to be available. It could either be installed as a gem (gem install selenium) or in /vendor/selenium/ (at the time of this writing the gem is quite old).
- Install Selenium on Rails: script/plugin install http://andthennothing.net/svn/public/selenium_on_rails/
- If RedCloth is available the Selenese test cases can use it for better markup.
- Run the Rakefile in the plugin’s directory to run the tests in order to see that everything works. (If RedCloth isn’t installed a few tests will fail since they assume RedCloth is installed.)
- Create a test case: script/generate selenium login
- Start the server: script/server -e test
- Point your browser to http://localhost:3000/selenium
- If everything works as expected you should see the Selenium test runner. The north east frame contains all your test cases (just one for now), and the north frame contains your test case.
win32-open3
win32-open3 is needed if you’re on Windows and want to run your tests as a Rake task (see test_acceptance), i.e. you don’t have to install it but it’s recommended.
You can build it from source or install the binary:
- Download the latest version of win32-open3, open3-0.2.2.so at the time of this writing.
- Open up irb and run this snippet: require ‘rbconfig’; include Config; puts CONFIG[‘sitearchdir’]
- Create a win32 directory under the directory you got, e.g. c:\ruby\lib\ruby\site_ruby\1.8\i386-msvcrt
- Rename the .so file to open3.so and put it in the win32 directory.
- Profit! (unless you get an error when doing require ‘win32/open3’)
Formats
The test cases can be written in a number of formats. Which one you choose is a matter of taste. You can generate your test files by running script/generate selenium or by creating them manually in your /test/selenium directory.
Selenese, .sel
Selenese is the dumbest format (in a good way). You just write your commands delimited by | characters.
|open|/selenium/setup|
|open|/|
|goBack|If you don’t want to write Selenese tests by hand you can use Selenium IDE which has support for Selenese.
SeleniumIDE makes it super easy to record test and edit them.
RSelenese, .rsel
RSelenese enable you to write your tests in Ruby.
setup :fixtures => :all
open '/'
assert_title 'Home'
('a'..'z').each {|c| open :controller => 'user', :action => 'create', :name => c }See SeleniumOnRails::TestBuilder for available commands.
HTML/RHTML
You can write your tests in HTML/RHTML but that’s mostly useful if you have existing tests you want to reuse.
Configuration
There are a number of settings available. You make them by renaming config.yml.example to config.yml and make your changes in that file.
Environments
Per default this plugin is only available in test environment. You can change this by setting environments, such as:
#config.yml
environments:
- test
- developmenttest_acceptance
You can run all your Selenium tests as a Rake task.
First, if you’re on Windows, you have to make sure win32-open3 is installed. Then you have to configure which browsers you want to run, like this:
#config.yml
browsers:
firefox: 'c:\Program Files\Mozilla Firefox\firefox.exe'
ie: 'c:\Program Files\Internet Explorer\iexplore.exe'Now you’re all set. First start a server:
script/server -e testThen run the tests:
rake test_acceptanceNow it should work, otherwise let me know!
Todo
Standalone mode
More work is needed on test_acceptance to be able to start the server when needed.
user_extension.js
Selenium has support for user_extension.js which is a way to extend the functionality of Selenium. However there is currently no easy way to add such a file in Selenium on Rails.
Partials
It would be nice if common parts of test cases could be stored in partials.
More setup/teardown support?
Currently there is only support to load fixtures and to wipe the session in /selenium/setup. Is there a need for more kinds of setups or teardowns?
Not todo
Editor
Creating an editor for the test cases is currently considered out of scope for this plugin. Selenium IDE does such a good job and has support for the Selenese format.
Credits
- Jon Tirsen—initial inspiration
- Eric Kidd—contribution of RSelenese
Contact
Jonas Bengtsson, jonas.b@home.se, http://andthennothing.net
Update: Thanks to Shinya rake test_acceptance is now supported on *nix as well.
Jonas, looks very useful, thanks! I'm trying to get this to work on my setup (kubuntu 5.10, ruby 1.8.4 and selenium gem 0.2.468). First I had to replace the "require 'YAML'" to "require 'yaml'" to get rake execution to work properly. Looks like on Linux, this is case sensitive. Then, I get 7 failures, all look like they are caused by the same issue. Here's the first one: 1) Failure: test_route(RendererTest) [./test/renderer_test.rb:17]: <"http://test.host/selenium/tests/suite/test_case.sel"> expected but was <"http://test.host/selenium/test_file?testname=suite%2Ftest_case.sel">. All errors appear to follow the same pattern where the url for a test is different than the one returned. An other one for example is "/selenium/tests/suite_two/suite_two_testcase.sel" vs. "/selenium/test_file?testname=suite_two%2Fsuite_two_testcase.sel" I was wondering if this is something due to my setup, in which case I could just change the expected result to what I get, or something else (difference in version somewhere?). I'll keep going and assume the failures are harmless
oops, the file where I needed to replace require 'YAML' was lib/selenium_on_rails_config.rb
Sorry 'bout that, I'll fix it.
I don't know why your tests fail. It seems like routes.tb is either not executed, or it doesn't work correctly for some reason. Let me know if you find the problem!
Great work! I've created a patch for test_acceptance to support *NIX environments (tested on Mac OS X).
http://www.kbmj.com/~shinya/selenium_on_rails/unix.patch
Thanks Shinya! I've included your patch now...
Eric Kidd's RSel format lacks support for the verify commands, so I've created a patch for the selenium_on_rails plugin that adds support for them here: http://xardion.chaosnet.org/verify.patch
Blane, I'll include it next week. Thank you!
I've added the verifies now, thank you Blane!
First off: Cool Plugin! Hopefully this will let me return to test first habits that were lost in the move to Rails and Ajax. Small editing request: Could you please change: "script/plugin install http://andthennothing.net/svn/public/selenium_on_rails/." to "script/plugin install http://andthennothing.net/svn/public/selenium_on_rails/" (drop the period). I lost some time to this until I realized the period was not part of the url. Silly, I know, but perhaps by changing it, you can save at least one future reader some time, or even keep them from giving up on your plugin. Thanks! -Steve-
Thanks for putting this together. Testing is always good. I am using it with Rails 1.0 on Fedora Core 4. I did notice that it wouldn't recognize any of my tests until I installed RedCloak. Thought you might like to know. Now, I can save scripts and run them back fine in the IDE, but when I try to run them in the test runner they only half work. I will investigate further... Thanks again, Ronan
I'm stalled in my effort to move my project's testing to your plugin. Both basic Selenium, and your plugin stall on file upload. Selenium IDE will handle file upload. Is there any way to make this happen (a la Selenium IDE)? I understand the need for the Javascript security feature, but am still asking because Selenium IDE does it.
If it can't happen, do you happen to know how to make the Selenium IDE run test suites?
Thanks Again!
Stephen, sorry for the delay (I've been busy and on vacation)!
I've removed the dot. Regarding your file upload problem I think it's better if you ask at the Selenium user forums.
Ronan, it's supposed to work without RedCloth installed so I'll have to see what's going on. Thanks for letting me know!
This is working great for me, but I had to add the following steps to my setup. 1) turn off class caching in [app-home]/config/environments/test.rb config.cache_classes = false 2) Start server with environment explicitly set to test. [app-home]/script/server -e test
What kind of problems did you have, and do you know how they were solved by turning off class caching?
Jonas. I turned off class caching so that I can continue to develop my app, fixing the bugs that selenium reveals, without restarting the server.
Looks like the new selenium 1.0.0 gem breaks selenium on rails. My work around is to use 0.7. Just passing on the info!
Thanks for telling Mike! The 1.0.0 is actually a completely different gem. So I'm thinking of adding Selenium Core to the Selenium on Rails distribution. Have you seen the Users Forum? It's probably a better place than this :-)
Hi Jonas, It's not only the file upload, what is handled different by IDE and the plugin. I have the problem with the plugin and ajax-stuff too. Though if i try to copy the source from the IDE into a foo.html test and run this via myapp/selenium, i'm not getting an error, but the test runs differently (not as wanted). In my particular case, after test has ran: 1. with IDE i see the page as expected - with all the layouts and css, 2. with the plugin - i see only the updated partial without any layouts or css.
script/server -e test selenese.rb undefined method 'register_template_handler' for ActionView::Base:Class
I love your plugin. Selenium is totally nice. My firm is doing some work with extjs and we've created some macros to handle the dynamic nature of the markup creation. Is there a standard way to add methods to selenium? For now, I've created a new file ext_js_test_builder_actions and included it in selenium_on_rails.rb, but I was wondering if there was some way that is an established way.