Run selenium ruby scripts in parallel from Team City.

I recently found out why my automated tests run in parallel from my workstation, but then run in sequence from a team city build agent.  It all comes down to the rake runner in team city.  Team city uses a customized rake runner that extends rspec’s spec task.  You get all of the per test reporting, graphing and historical information from team city’s customized rspec reporting.  All this is great if you don’t mind running your tests in sequence, but If your using team city to launch automated web test using the selenium grid this is bad news.

I use a rake file to control the execution of my ruby selenium scripts. I modeled it from the ruby example in the selenium grid distribution. What is important to know about the run in parallel rake task is that it is not your normal rake task. It is first a rspec spec task.  This is a test runner from rspec that allows you to use a rake task to run specs. Not only is it a spec task, it also has been overridden by deep test, the software package that adds parallelization.  Since the spec task is actually deep test’s spec task and not the rspec spec task there are dependencies on what version of rspec deep tests needs.  You must run with rspec 1.1.8 so that the spec task that deep test overrides will work correctly.

So what happens when you run your deep test spec task from team city with their rake runner?   Deep test cannot launch your tests in parallel because Team City is using their customized spec task.  The solution is to use the command line runner to launch your rake task for the selenium grid, then your spec task will have access to the correct rspec gem for deep test to use as a spec runner.

Essentials of getting started with Selenium Grid and Ruby.

I have been running test/unit scripts on the grid for a while now and I was trying to remember where I got my test/unit syntax from.  I was looking to create a live template to create new test scripts in Rubymine. You actually have to make sure that you create the Selenium Driver object in a specific way or your scripts will not run on the Selenium Grid or they will not run in parallel correctly.  Your test class must also always have a setup and tear down, even if you call a method out side of the class to setup the driver.

There are 2 sources of information that you must understand and monitor for updates.  First is the Selenium Grid home page.  Recently when Firefox 3.5 became available, I needed to update the version of the grid software I was using.  The other site is the site for the selenium client gem. You can find documentation here on how to structure your spec and test/unit scripts so that they run on the grid.  The gem and the grid  are both maintained by the same person.  I revisit the sites often to pick up tips on how to structure rake files and get tips for running scripts in parallel. In working with this software, I have become very familiar with the files in the rspec, deeptest, and selenium-client gems.

Firefox cannot initialize the application’s security component.

I have noticed this alert showing up when I start Firefox. After experimenting,  I have found the solution.  On a Mac go into th ~/Library/Application Support/Firefox/Profiles directory and cd into your profile’s directory.  Remove the cert8.db file and restart Firefox. Firefox will regenerate a new file and sites that use https will start working again.

Picture 4

Fix keyboard mapping mixup on OSX

I dock my Mac laptop and use an external keyboard.  I map my dell keyboard so that the command key is in the same place as on the laptop.  This lets me use the alt key as command and the windows key as the alt/option key.   I had a problem when I undocked, the laptop mixed up the key mapping and the alt and command keys were reversed.  I used the keyboard setting tab multiple times to reset the keys do default without any success. To fix the problem I plugged in  both keyboards and selected the all option from the keyboard drop down list.  Then I deleted /Library/Preferences/com.apple.keyboardtype.plist and reconfigured both keyboards. I spend weeks trying to figure out why my keyboard mapping was wrong on my laptop.

Picture 1

Use custom Firefox profiles with selenium grid.

I started using custom Firefox profiles so that I could get around the self signed cert error in firefox.  I found a way to easily maintain one configured profile that is used across all Selenium remote control environments running on OS X and multiple windows versions.

  1. Download the selenium grid software.  I am using selenium-grid-1.0.4
  2. Create a new directory called SeleniumFireFoxProfile  in your selenium-grid-1.0.4 directory.
  3. Start your remote control using the custom profile. rake all:start SELENIUM_ARGS=”-firefoxProfileTemplate SeleniumFireFoxProfile”
  4. Create a test script to open a selenium client driver and navigate to the site that is getting the ssl cert error. Use a debugger to pause the execution of your script at the browser.open command
  5. Execute your script to the break point and accept the cert in your Firefox browser.
  6. Look for the creation of the cert_override.txt file in your user directory.   With Firefox 3.0 I found it on my mac in /private/var/folders.  I cd to the contained directory and run a find command to locate it sudo find . -name cert_override.txt. With Firefox 3.5 the profile is stored in the user directory ~/Library/Application Support/Firefox/Profiles.
  7. You should see an entry in your cert_override.txt file after you accept the certificate. Now copy the cert_override.txt from your user directory to your SeleniumFireFoxProfile directory in your selenium grid directory.
  8. Restart your remote control now using the profile with the cert_override.txt file in it.  Rerun your test and break on the open command now you should see your page render instead of the security warning.

Just a warning about the cert_override.txt file.  It is very white space sensitive, it is better to copy the whole file into your custom profile directory rather then copying a new line into an existing file.

I check in the override text file along with my selenium software.  This way I can add a cert on my local machine and then run a SVN update on the distributed grid hosts to get the new cert information. This works for both windows and OS X.

Using the selenium IDE and Rubymine

This post explains how to debug a Selenium test script in Rubymine like you would in the selenium IDE. It is common to have a missing element failure in a recorded selenium script.  A quick way to debug this in the Selnium IDE is to put a break point in your script and use the execute command button to find the right code for your failing command.
Add breakpoints start and stop test execution

Add breakpoints start and stop test execution

In the case of a failing script you can see below that the failing line is highlighted in red and you can see the log message explaining the error.
Failing script

Failing script

So when you export your selenium IDE script to your favorite language, how do you trouble shoot failures like this easily in your development environment?
Test Script

Test Script

I generated an error in the script by changing the name of the google button from “btnG” to “btn” .  Trouble shoot this error by using the Rubymine debugger and evaluate expression tool.  I put a break point on the line of code that is failing.  Then I can select the text and open the evaluate expression tool to see if I can locate the button.
picture-15

When you get the expression correct, you will see a message, result = nil.  This is a great tool for testing xpath expressions.

Successful Evaluation

Successful Evaluation

If you noticed that the code I used in this example is different than what is exported from the Selenium IDE, it is because I use the selenium-client gem and execute my tests using the selenium grid.

A new program for my Mac

I just discovered a new program for the Mac called Gowl.  It was a piggy back program installed with drop box.   The name is cheesy but I like what the program does.   As your working throughout the day you have emails, updates from Skype, and updates from Tweetdeck.  There are many different programs that present messages. I don’t want to cmd tab to all of the different programs to see if the status of a new message is important.  Growl lets me look in the top right corner of my screen to get messages from a number of different programs.  Growl also interfaces with rubymine and firefox.

picture-3

Time to start the blog.

I have been convinced in to starting a technical blog like many of my technology colleagues in Austin.  I will be posting information as I learn from my work experiences so that I can help better the coding community.

LA Trip

We went to LA and San Clemente in late August.   We stayed at the Grafton on sunset one night.

We woke up and hiked Runyon Canyon

The next night we stayed in  Mulholland estates in Beverly Hills.

The next day we met Christal’s friend at CAA and had coffee at the coffee shop.  We saw a few celebrities there.