Friday, October 5, 2007

Selenium on Rails for Java

In our previous blog posts we have gone over using Selenium on Rails to write custom DSLs to make in browser web based testing much easier. The largest complaint that we hear about this technique is that it uses Selenium on Rails and most people out there are not in an environment where they can use Ruby and/or Rails. The solution to this problem, at least for Java development houses, is to use JRuby and gold spike to make a Selenium on Rails installation that packages itself up as a war. This allows you to develop DSLs using the convenience of Ruby and Selenium on Rails but never having to actually install Ruby. It also allows you to deploy the test suite to your application server of choice. By doing this you also solve the cross-domain scripting problem that often plagues selenium test suites (the need for the tests and the application to run on the same domain).

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:

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/
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.
4. Create a testing Ruby on Rails project
  • 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:
    jruby $JRUBY_HOME/bin/rails selenium-tests 
    This should result in a directory and a whole bunch of files called selenium-tests being created
  • Next we want to make our rails project self contained, not dependent on having the rails gem installed. To do this do the following:
    cd selenium-tests
    rake rails:freeze:gems
    This will leave you with the rails gem installed in your rails project.
  • 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:

    jruby script/plugin install
    This will install the golden spike plugin into Rails.
Selenium on Rails

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:
    jruby script/plugin install
    When this command completes selenium-on-rails will be present in your vendors/plugin directory.
  • 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:
    jruby script/server -e test
    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.
2. Running everything under Tomcat
  • 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:

    $JRUBY_HOME/bin/gem install activerecord-jdbc --no-ri --no-rdoc
    This should cause the gem service to start and it to say that Activerecord-jdbc was successfully installed.
  • 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:
    $JRUBY_HOME/bin/rake war:standalone:create 
    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.
  • 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.
Once a basic installation of selenium-on-rails is running inside Tomcat it is then possible to follow our previous blog posts and create domain specific languages (DSL) for testing or do any of the other nice tricks that Ruby + Selenium give you.

Tags: , ,,
, , , , , , ,