Automating Flash, AJAX, Popups and more using Ruby, Watir and Sikuli
Jonathan Kohl pointed me at Sikuli, a Python-based tool for automating applications using image recognition. Unlike most tools, which attempt to identify objects via public APIs, Sikuli looks at the pixels on the screen and attempts to identify objects based on how they look.
This isn’t exactly a new approach, as commercial tools have had this feature for a long time as a means of creating custom objects. So after playing with Sikuli, I wondered whether I could take advantage of it as a library to augment my Watir scripts in Ruby.
It turns out, the answer is ‘yes’, with a caveat. You need to use JRuby (although you could probably do it in Ruby using the Ruby-Java bridge – It just looked a lot harder), and you also need to use Watir-Webdriver, a new implementation of Watir’s API which is used in a number of other automation frameworks.
Below is a simple example script and instructions to get you started. It navigates to a website, then clicks on the flash control there. I’ve so far only tested this on Windows. It should work on OSX and Linux, but perhaps not quite so easily (I’m waiting on some feedback). Check the instructions for ‘Install OpenCV’ at the page with instructions for calling Sikuli from other tools.
Sikuli can be used to automate Flash components, any challenging AJAX elements of your web application, to dismiss pop-ups or probably even to inspect visual elements of the page (though I’d want to do this minimally). It’s a little slow, but an interesting and immediately useful add-on to Watir or your favourite java-based testing tool.
#Install Java, or install the JRuby/JRE bundle at the next step
#Install JRuby 1.5.1 - http://jruby.org/download
#Install Sikuli - http://sikuli.org/download.shtml
#Install watir-webdriver (eg. jgem install watir-webdriver)
#Update ssh - jgem install jruby-openssl
#Copy sikuli-script.jar to \jruby-1.5.1\lib
#Get the test image
#Download http://www.software-testing.com.au/images/flashmap_middleeast.png and put it in the image folder as below
#See http://sikuli.org/trac/wiki/How to use Sikuli Script in your JAVA programs for examples