In this post we attempt to go over how to get Selenium on Rails up and running in an Application Server in such a way that you can easily write your own custom DSLs. For the purposes of this example we are going to use a Red Hat Linux box and Apache Tomcat 6. More detail on troubleshooting a JRuby installation can be found here.
Getting Started
1. Install Jruby:
The easiest way to do this is with a direct download:
wget http://dist.codehaus.org/jruby/jruby-bin-1.0.1.tar.gz
Untar this wherever you want to install jruby, I am installing it in /home/apache/jruby so my examples will be using that.2. Set your JRUBY_HOME and PATH variables to make jruby accessible everywhere:
export JRUBY_HOME=/home/apache/jruby/
export PATH=$JRUBY_HOME/bin:$PATH
3. Test your jruby installation- Create a file test_jruby.rb with the following contents: puts "I
t-shirts" - Run your simple ruby program: jruby test_jruby.rb -> This should output I
t-shirts.
- Install the gem for rails (this will take a few minutes):
$JRUBY_HOME/bin/gem install rails -y --no-rdoc --no-ri
- Now create a selenium-tests rails project:
This should result in a directory and a whole bunch of files called selenium-tests being createdjruby $JRUBY_HOME/bin/rails selenium-tests
- Next we want to make our rails project self contained, not dependent on having the rails gem installed. To do this do the following:
This will leave you with the rails gem installed in your rails project.cd selenium-tests
rake rails:freeze:gems - Finally install something called golden spike. This is what allows a rails project to be compiled into a war file. This is done by running the following command from your selenium-tests directory:
This will install the golden spike plugin into Rails.
jruby script/plugin install http://jruby-extras.rubyforge.org/svn/trunk/rails-integration/plugins/goldspike
Now that Jruby in install and Ruby on Rails is running (without ruby actually be installed) we can get on with installing Selenium on Rails and then testing everything from within a Tomcat instance.
1. Installing Selenium on Rails
- Selenium on Rails is nothing more than a Rails plugin, like golden spike. To install it we use the script/plugin command. To be specific, from your selenium-tests directory run the following:
When this command completes selenium-on-rails will be present in your vendors/plugin directory.jruby script/plugin install http://svn.openqa.org/svn/selenium-on-rails/selenium-on-rails
- Finally generate a basic selenium test that you can use for double checking that everything is working. To do this use the selenium generator. From in your selenium-tests directory type the following:
jruby script/generate selenium test - To test to make sure everything is working up to this point you can start the internal Ruby on Rails web server and see if you can connect to the default selenium suite. To do this type tho following in your selenium-tests directory:
Now the ruby web server is running on port 3000 so point your web browser at: http://localhost:3000/selenium. You should see a selenium test suite with your single test in it.jruby script/server -e test
- The way that the golden spike plugin works is that will compile your entire project into a war. This war can then be put under an Apache Tomcat instance and have the tests run in the same domain (and same environment) as your normal web based java application. The first step is to install the Activerecord-jdbc gem. To do this type the following:
This should cause the gem service to start and it to say that Activerecord-jdbc was successfully installed.
$JRUBY_HOME/bin/gem install activerecord-jdbc --no-ri --no-rdoc - One of the ways that selenium-on-rails operates is that it only exists when the rails environment is set to test. Unfortunately golden spike (the war creator) does not support RAILS_ENV very well right now. The easiest way to do this is to edit line 77 of selenium-tests/vendor/plugins/goldenspike/lib/war_config.rb and change the @rails_env variable from "production" to "test". Hopefully golden spike will respect the RAILS_ENV variable a bit better in the future and this step will not be necessary.
- Now it is time to build the war! To do this issue the following command from your selenium-tests directory:
This will take a moment to run as it downloads the appropriate jars and builds them into a single war. For more information on this process, and the wide variety of options that can be passed in to optimize the process, look at JRuby Wiki. You should also notice that we are doing all of this in test mode, this is how selenium-on-rails prefers to operate, though it can be tweaked to run in any mode. The result of this command should be a selenium-test.war file.$JRUBY_HOME/bin/rake war:standalone:create
- Now lets test this war. To do this just copy the selenium-tests.war to the webapp directory of a running instance of Apache Tomcat. Then point your web browser at http://localhost:8080/selenium-tests/selenium. You should now see the basic selenium test suite running in selenium-on-rails in a Java application server.
Tags: selenium, selenium testing,ruby on rails,
white box testing, selenium on rails, jruby, java, apache tomcat, customink, tshirts, custom tshirts