Selenium on Rails provides an easy way to test Rails application with SeleniumCore.
This plugin does four things:
Rails has been changed in ways that break the original versions of Selenium on Rails. If you're using versions before Rails 2.1 you need to use this release. There are no plans to update this release with new changes or bug fixes unless there is sufficient demand, so if you can update then do so.
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.
RSelenese enable you to write your tests in Ruby. This is my favorite format.
setup :fixtures => :all
open '/'
assert_title 'Home'
('a'..'z').each {|c| open :controller => 'user', :action => 'create', :name => c }
See SeleniumOnRails::TestBuilder for available commands. This is also available in the SeleniumIDE, using the format here. IMPORTANT NOTE: RSelenese generates the HTML tables for Selenium behind the scenes when the page is loaded - ONCE. That means code like this:
(1..10).each do |index| do something endWill only be executed when the test is loaded, not when the test is run. This is a common error and leads to tests that work the first time and fail the second time.
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 SeleniumIDE which has
SeleniumIDE makes it super easy to record test and edit them.
You can write your tests in HTML/RHTML but that‘s mostly useful if you have existing tests you want to reuse.
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. This is highly recommended, just as small functions would be recommended in structured programming.
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
To include a partial test case in a RSelenese test case:
include_partial 'login', :name => 'Jane Doe', :password => 'Jane Doe'.reverse
in a Selenese test case:
|includePartial|login|name=John Doe|password=eoD nhoJ|
and in a RHTML test case:
<%= render :partial => 'login', :locals => {:name = 'Joe Schmo', :password => 'Joe Schmo'.reverse} %>
There are a number of settings available. You make them by copying config.yml.example to config/selenium.yml in your application and make your changes in that file.
Per default this plugin is only available in test environment. You can change this by setting environments, such as:
#config.yml environments: - test - development
If you don‘t want to use the bundled Selenium Core version you can set selenium_path to the directory where Selenium Core is stored.
#config.yml selenium_path: 'c:\selenium'
You can run all your Selenium tests as a Rake task. If you're using a continuous builder this is a great way to integrate selenium into your build process.
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 test
Then run the tests:
rake test:acceptance
Now it should work, otherwise let me know!
If you want to store the results from a test:acceptance you just need to set in which directory they should be stored:
#config.yml result_dir: 'c:\result'
So when you run rake test:acceptance the tables with the results will be stored as .html files in that directory.
This can be useful especially for continous integration.