Wednesday, October 20, 2010

Using Selenium Tests with Django/Python, reverse SSH tunnels

Using Firefox/Internet Explorer with Selenium behind a firewall accessing a Slicehost/Amazon EC2 instance

Here is a HOWTO to get things working so that you can do Selenium on your own client browser and be running your code/dev server off a Slicehost/Amazon EC2 instance. You need to have an SSH client, Java, and the Selenium RC JAR file.

You also need to keep in mind that the more tests you have, the slower things go. Selenium RC executes each test one-by-one. Selenium Grid is meant to run multiple Selenium tests in parallel. Sauce Labs can be configured to parallelize these tests, but you can use this setup procedure to do your own sanity checks.

If you are using Windows to do testing, you may need to setup your PATH and profiles to get Firefox working. You may find it easier to run Firefox/Chrome tests on MacOS and Ubuntu-based machines than on Windows.

1. Download Selenium-RC from http://seleniumhq.org/download or http://selenium.googlecode.com/files/selenium-remote-control-1.0.3.zip download. You will also need to download Java from http://www.java.com.

2. Unzip the selenium-remote-control-1.0.3.zip file and extract the selenium.py file from selenium-python-client-driver-1.0.1. You may wish to review the selenium.py file to see what type of Selenium commands you can issue.

3. Setup a reverse tunnel on your client side (not SSH'd into your Slicehost). If you don't have an SSH public key, you will be prompted for one. Run the ssh command on your local machine:

ssh -nNT -R 9999:127.0.0.1:4444 username@dev.myhost.com

This SSH command is essentially tunneling the port 9999 on dev.myhost.com to your local host 4444. You can change port 9999 to whatever local port on dev.myhost.com. You can also change 127.0.0.1 to any other notes running the Selenium RC server on your local network.

3. Inside your Python code, you will need to import the selenium.py and then connect to the appropriate site hosting your dev server instance. The dev server needs to be accessible from your browser.

import selenium

# *iehta = Internet Explorer proxy mode, *firefox = Firefox
    selenium_browser = selenium("localhost", 9999, "*iehta", "http://dev.myhost.com")
#   selenium_browser = selenium("localhost", 9999, "*firefox", "http://dev.myhost.com")

    selenium_browser.start()
    selenium_browser.open("http://www.myhost.com")
    return selenium_browser

Things to note:

a. The example above shows port 9999. You may wish to change your reverse tunnel port.
b. The *iehta stands for Internet Explorer Proxy mode. You can also use *iexplore, *firefox but on PC's you will need to do setup PATH and setup work for Firefox.

5. Internet Explorer also needs to be changed to disable some security settings that cause issues:

a. You may also need to go into Internet Explorer -> Tools -> Internet Options -> Advanced and disable "Check for publisher's certificate revocation" and "Check for server certificate revocation*". You may also need to disable "Warn about certificate address mismatch."

b. Inside Tools->Internet Options->Privacy, you should turn off Pop-up Blocker.

c. Inside Tools->Internet Options->Security,you should turn off Protected Mode.

d. Restart IE8

Keep in mind these security changes will make your machine more susceptible to viruses/spyware, so buyer beware.

6. If you are using Windows, go to DOS but you must run in Administrator mode. At the command-line, run the Selenium server:

java -jar selenium-server.jar

Also, you can telnet to localhost 4444 and telnet to the port that you tunneled on dev.myhost.com to verify the reverse tunnel is operating correctly.

6 comments: