Test Jifty with Selenium, on different browsers.

作者:   發佈於: ,更新於:   #jifty #selenium #test

We’ve recently working with Jifty team for a big internal update. Jifty team has been using Selenium as one of their testing tools, specifically, it’s Selenium-RC. In short, one current test application is first started and ask selenium server to connect to its URL and follow by a sequences of instructions to test the framework itself. Since this is a web application framework, it’s good and important to know a feature will be working on all browsers. Therefore, we need to write tests and run the tests on different browsers.

It’s simple to run those tests, just do somethingl like:

cd jifty/
prove -lr t/TestApp-\*/

Conventionally t/TestApp-* contains Jifty applications that are specially designed for running tests. Their sub-tests lives under t/TestApp-*/t/. For instance, our recent work goes under t/TestApp-JiftyJS/t/. If you run the prove command above, then you’ll see your Firefox browser pops up and start doing something. That means it’s doing it’s job just well. Of course you’ll need to install a bunches of Perl modules including Alien::SeleniumRC and WWW::Selenium. And your OS needs to be able to run Java. I’m a Mac user, so this all works for me.

Then I need to run these tests on Windows browsers to see if they all works. What should I do ? First way, I can install whole Jifty suit on my Windows box, also install Java for running selenium-rc. Sounds simple, but it’ll take too much windows-fu from me. I might gain a lots of experiences value, but 85% of my health points will be lost. Not a good choice.

Give it a second thought, it’s mindless to prepare multiple instance of Jifty environments just to run tests on different browsers.

Here’s what I’ve done on my windows box (it’s really a virtual machine, but that makes no difference at all):

  1. Install Java Runtime
  2. Download Selenium-RC
  3. Download Putty, the greatest windows SSH client with worst user experiences. No offense, but I really have to complain that it’s setting screen works against my intuition.
  4. Run the selenium-server.jar in the downloaded selenium-rc with this (do this in your CMD window):
  5. java -jar selenium-server.jar
  6. Use putty to connect to one of your server where Jifty are all setup. Let’s say it’s example.com. Also open a tunnel to forward port 4444 on remote to port 4444 on localhost.
  7. Note: With UNIX commands it’s this: ssh -R4444:localhost:4444 example.com
  8. Putty users can see the screenshot down below to find where to open that tunnel
  9. Setup these environment variables on your remote host, they’ll be recognized by Jifty::Test::WWW::Selenium modules to do the right thing :

    export SELENIUM_RC_SERVER=localhost:4444 export SELENIUM_RC_TEST_AGAINST=example.com export SELENIUM_RC_BROWSER='*iexplore'

Now run the prove command on example.com, you should see your local Internet Explorer pops up and do something.

The concept here is to only run selenium-server on Windows, but running the Jifty test applications on somewhere else. The caveat is that these two machines need to be rechable from each other. VPN should be OK. The SELENIUM_RC_TEST_AGAINST variable is set to the remote host hostname or IP address on remote. Because it’s telling selenium server where the Jifty application is.

With this technique, I can use my Mac to run the selenium server, and ssh into that example.com with ssh -R5555:localhost:4444, change SELENIUM_RC_SERVER to localhost:5555. Then I can test Jifty with my Mac browsers, without the need to setup another Jifty environment again.

If I’m not running my Windows in a virtual machine, but on a real machine where it has it’s own public IP, or it’s a private IP but it’s accessible from example.com. Then I don’t even need to use Putty to open up a ssh tunnel. I’ll just configure SELENIUM_RC_SERVER to it’s IP, like 192.168.10.123:4444. And directly run the prove on example.com, it’ll just work.

PS: This post is cross-posted to Handlino Blog