mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-21 01:30:12 +01:00
193 lines
6.8 KiB
Text
193 lines
6.8 KiB
Text
|
|
= Selenium on Rails
|
||
|
|
|
||
|
|
== Overview
|
||
|
|
|
||
|
|
Selenium on Rails provides an easy way to test Rails application with
|
||
|
|
SeleniumCore[http://www.openqa.org/selenium-core/].
|
||
|
|
|
||
|
|
This plugin does four things:
|
||
|
|
1. The Selenium Core files don't have to pollute <tt>/public</tt>, they can stay in the Selenium gem or in <tt>/vendor/selenium</tt>.
|
||
|
|
2. No need to create suite files, they are generated on the fly -- one suite per directory in <tt>/test/selenium</tt> (suites can be nested).
|
||
|
|
3. Instead of writing the test cases in HTML you can use a number of better formats (see <tt>Formats</tt>).
|
||
|
|
4. Loading of fixtures and wiping of session (<tt>/selenium/setup</tt>).
|
||
|
|
|
||
|
|
== Installation
|
||
|
|
|
||
|
|
1. Selenium Core needs to be available. It could either be installed as a gem (<tt>gem install selenium</tt>) or in <tt>/vendor/selenium/</tt>.
|
||
|
|
2. Install Selenium on Rails: <tt>script/plugin install http://svn.openqa.org/svn/selenium-on-rails/selenium-on-rails</tt>
|
||
|
|
3. <i>If RedCloth is available the Selenese test cases can use it for better markup.</i>
|
||
|
|
4. 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.)
|
||
|
|
5. Create a test case: <tt>script/generate selenium login</tt>
|
||
|
|
6. Start the server: <tt>script/server -e test</tt>
|
||
|
|
7. Point your browser to <tt>http://localhost:3000/selenium</tt>
|
||
|
|
8. 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[http://raa.ruby-lang.org/project/win32-open3/] is needed if you're
|
||
|
|
on Windows and want to run your tests as a Rake task
|
||
|
|
(see <tt>test:acceptance</tt>), i.e. you don't have to install it but it's
|
||
|
|
recommended.
|
||
|
|
|
||
|
|
You can build it from source or install the binary:
|
||
|
|
|
||
|
|
1. Download the latest version of win32-open3, open3-0.2.2.so[http://rubyforge.org/frs/download.php/8515/open3-0.2.2.so] at the time of this writing.
|
||
|
|
2. Open up irb and run this snippet: <tt>require 'rbconfig'; include Config; puts CONFIG['sitearchdir']</tt>
|
||
|
|
3. Create a <tt>win32</tt> directory under the directory you got, e.g. <tt>c:\ruby\lib\ruby\site_ruby\1.8\i386-msvcrt</tt>
|
||
|
|
4. Rename the .so file to <tt>open3.so</tt> and put it in the <tt>win32</tt> directory.
|
||
|
|
5. Profit! (unless you get an error when doing <tt>require 'win32/open3'</tt>)
|
||
|
|
|
||
|
|
== 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
|
||
|
|
<tt>script/generate selenium</tt> or by creating them manually in your
|
||
|
|
<tt>/test/selenium</tt> 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
|
||
|
|
SeleniumIDE[http://www.openqa.org/selenium-ide/] which has
|
||
|
|
support[http://wiki.openqa.org/display/SIDE/SeleniumOnRails] 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.
|
||
|
|
|
||
|
|
=== 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
|
||
|
|
|
||
|
|
To include a partial test case you write like this in a Selenese test case:
|
||
|
|
|
||
|
|
|includePartial|login|name=John Doe|password=eoD nhoJ|
|
||
|
|
|
||
|
|
in a RSelenese test case:
|
||
|
|
|
||
|
|
include_partial 'login', :name => 'Jane Doe', :password => 'Jane Doe'.reverse
|
||
|
|
|
||
|
|
and in a RHTML test case:
|
||
|
|
|
||
|
|
<%= render :partial => 'login', :locals => {:name = 'Joe Schmo', :password => 'Joe Schmo'.reverse} %>
|
||
|
|
|
||
|
|
== Configuration
|
||
|
|
|
||
|
|
There are a number of settings available. You make them by renaming
|
||
|
|
<tt>config.yml.example</tt> to <tt>config.yml</tt> and make your changes in that
|
||
|
|
file.
|
||
|
|
|
||
|
|
=== Environments
|
||
|
|
|
||
|
|
Per default this plugin is only available in test environment. You can change
|
||
|
|
this by setting <tt>environments</tt>, such as:
|
||
|
|
|
||
|
|
#config.yml
|
||
|
|
environments:
|
||
|
|
- test
|
||
|
|
- development
|
||
|
|
|
||
|
|
== <tt>test:acceptance</tt>
|
||
|
|
|
||
|
|
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 test
|
||
|
|
|
||
|
|
Then run the tests:
|
||
|
|
|
||
|
|
rake test:acceptance
|
||
|
|
|
||
|
|
Now it should work, otherwise let me know!
|
||
|
|
|
||
|
|
=== Store results
|
||
|
|
|
||
|
|
If you want to store the results from a <tt>test:acceptance</tt> you just need
|
||
|
|
to set in which directory they should be stored:
|
||
|
|
|
||
|
|
#config.yml
|
||
|
|
result_dir: 'c:\result'
|
||
|
|
|
||
|
|
So when you run <tt>rake test:acceptance</tt> the tables with the results will
|
||
|
|
be stored as <tt>.html</tt> files in that directory.
|
||
|
|
|
||
|
|
This can be useful especially for continous integration.
|
||
|
|
|
||
|
|
== Todo
|
||
|
|
|
||
|
|
=== Standalone mode
|
||
|
|
|
||
|
|
More work is needed on <tt>test:acceptance</tt> on Windows to be able to start
|
||
|
|
the server when needed.
|
||
|
|
|
||
|
|
=== user_extension.js
|
||
|
|
|
||
|
|
Selenium has support for <tt>user_extension.js</tt> which is a way to extend the
|
||
|
|
functionality of Selenium Core. 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
|
||
|
|
<tt>/selenium/setup</tt>. Is there a need for more kinds of setups or teardowns?
|
||
|
|
|
||
|
|
=== More documentation
|
||
|
|
|
||
|
|
|
||
|
|
== Not todo
|
||
|
|
|
||
|
|
=== Editor
|
||
|
|
|
||
|
|
Creating an editor for the test cases is currently considered out of scope for
|
||
|
|
this plugin. SeleniumIDE[http://www.openqa.org/selenium-ide/] does such a good
|
||
|
|
job and has support[http://wiki.openqa.org/display/SIDE/SeleniumOnRails] for
|
||
|
|
the Selenese format.
|
||
|
|
|
||
|
|
== Credits
|
||
|
|
|
||
|
|
* Jon Tirsen, http://jutopia.tirsen.com -- initial inspiration[http://wiki.rubyonrails.com/rails/pages/SeleniumIntegration]
|
||
|
|
* Eric Kidd, http://www.randomhacks.net -- contribution of RSelenese
|
||
|
|
|
||
|
|
== Information
|
||
|
|
|
||
|
|
For more information, check out the website[http://www.openqa.org/selenium-on-rails/].
|