Selenium on Rails is getting DRYer
Selenium on Rails has moved.
Selenium on Rails has gotten way too little love lately, but now I just released an update.
First off, a new feature. It is now possible to create partial test cases and include them in other test cases. That way you can keep common actions in one place and use them in many.#_login.rsel
open '/login'
type 'name', name
type 'password', password
click 'submit', :wait=>true|includePartial|login|name=John Doe|password=eoD nhoJ|include_partial 'login', :name => 'Jane Doe', :password => 'Jane Doe'.reverseAlso included in this release is two defect fixes to rake test:acceptance.
The first fix is from David Vollbracht which makes it possible to start the server automatically on *nix. You still have to start_server: true in config.yml since I can’t get it to work reliably on Windows (if anyone knows why, give me a ping).
The second fix is from Dave Hoover which adds support for Safari.
script/plugin update (or reinstall the plugin if that doesn’t work)
Let me know if it doesn’t work or if you want it to work differently!
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/.
- 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|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 }HTML/RHTML
You can write your tests in HTML/RHTML but that’s mostly useful if you have existing tests you want to reuse.
Partial test cases
If you have some common actions you want to do in several test cases you can put them in a separate partial test case and include them in your other test cases.
A partial test case is just like a normal test case besides that its filename has to start with _:
#_login.rsel
open '/login'
type 'name', name
type 'password', password
click 'submit', :wait=>true|includePartial|login|name=John Doe|password=eoD nhoJ|include_partial 'login', :name => 'Jane Doe', :password => 'Jane Doe'.reverse<%= render :partial => 'login', :locals => {:name = 'Joe Schmo', :password => 'Joe Schmo'.reverse} %>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'script/server -e testrake test:acceptanceTodo
Standalone mode
More work is needed on test:acceptance on Windows 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.
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. SeleniumIDE 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
There seems to be a problem with the PartialsSupport class.
TestRunner fails with: uninitialized constant PartialsSupport
on renderer.rb:6:in `render_test_case'
It works on my computer :-)
Could you check if you have the file lib/selenium_on_rails/partials_support.rb? If so does it work if you add the following line to /lib/selenium_on_rails.rb:
I just made that change to the svn repository. Hope it helps!
Hi,
I've started putting Selenium on Rails into our continuous integration cycle, but even if test_acceptance fails with some errors, rake doesn't return an error. So I've created a patch that fixes this: http://www.kbmj.com/~shinya/selenium_on_rails/raise_test_failure.patch
BTW, have you checked out Selenium Remote Control? This allows you to control Selenium remotely using Ruby code, and it would be great if it gets integrated into Rails.
Hi Shinya,
The patch is included in the svn repository, thank you!
I've heard about the Selenium RC but not looked into it. Has RC been developed much lately or is it the same as what was included in the old Selenium project?
Thanks for incorporating the patch, Jonas.
Selenium RC has significantly changed since the driven mode in Selenium. Personally I think the TestRunner in Selenium, as used in Selenium on Rails, has the advantage in its simplicity (you don't need to run Selenium Server, and you can easily run tests using the web UI), but when you want to start using variables and conditions in your test, Selenium RC can handle it very well.
Is it possible to pre-setup session data when running selenium tests?
Shinya: Okey. I'll look at Selenium RC later on to see if it fits. But it will take some time since I'm quite busy. Let me know if you come up with an idea of how to incorporate it!
Erkki: No, not currently. What kind of data would you like to set in the session? Do you think it would possible to specify the data in the query part of the "setup" URL?
Jonas: I have a setup where I need to set user/login info through session variables. I cannot login through selenium because the login is not handled by the rails app I'm trying to test. I figure it should be possible to specify the parameters in the setup url. Perhaps a better solution would be another setup-session url where information could be posted?
Hi, Can you please guide me how to call a CSV file and a normal file to a Selenium script ? Please help me. Thanks, Jitu
Jitu: I'm not sure I understand what you want. How and where do you want to use CSV and "normal" files in selenium script? Also it's probably better if you use the user forum.
I am using rails 2.0.5 I followed the install for selenium on rails and used the pre 2-1 version, installed the win32-open3 gem, installed the RedCloth gem, tested the Rakefile and it worked. I start my webrick test server and jetty server. I then create a basic test to run. it runs fine when i run it from the \test\selenium folder via the command line prompt >ruby file_test.rb. You can see it open firefox and the testrunner, etc. however when i try to run it as a rake command from the command line project folder (>rake test:acceptance), it errors out. it says rake aborted!, browser takes too long. it is erroring out in the acceptance_test_runner.rb file. am i missing some kind of log file or yml file? it seems to try to start creating various firefox.yml, firefox(1).yml, ... files and after going past the max duration time limit it bombs. pls help. let me know what other details you might need. thanks.