Saturday, May 21, 2011

Ad-Hoc Selenium Development using Ruby and IRB: some notes

There are a number of ways to go about developing Selenium tests. You can use the Selenium IDE Firefox plugin and record browser actions, but often the xpaths the IDE selects are not necessarily the ones you want to use - for example, they might capture wicket id's that are dynamically created and may not even work on immediate playback. With any luck, your web page developers will use custom, unique id's for their html widgets, and you can find these using Google Chrome inspection (right-click on a widget, and select 'Inspect Element') or the Firebug plugin to Firefox which does the same thing. Even so, xpaths can be tricky to get exactly right, which is why I often do ad-hoc Selenium development using Ruby and IRB (the interactive ruby environment). With this approach, you can do trial and error and get immediate feedback while developing your Selenium test scripts.



There are of course a few things you need to get going: Ruby, for one, Java, and Selenium. You can get the selenium-server.jar here, and for Ruby, you will need the selenium client gem:

sudo gem install selenium-client



You will also want to bookmark the Ruby SeleniumRC docs



In a terminal window, you can start the default selenium server like this:

java -jar selenium-server.jar

(you can now use selenium-server-standalone-2.1.0.jar for Firefox 5 support)

but when using Firefox it can be helpful to use a Firefox Profile that includes Firebug - this allows you to explore xpaths while doing your ad-hoc selenium explorations.



Create a separate Firefox profile

1 - (on Mac) /Applications/Firefox.app/Contents/MacOS/firefox-bin -ProfileManager

2 - Create a new profile, name it an save it to a new folder (e.g. /Users/YOU/firefox)

3 - Start Firefox using the new profile, and install the firebug Plugin

4 - Edit the file prefs.js within the new profile's folder

comment out this line: user_pref("browser.startup.page", 0);

make sure this value is 'true': user_pref("extensions.firebug.console.enableSites", true);

5 - Launch the selenium server with option to use the new profile

java -jar selenium-server.jar -firefoxProfileTemplate "/Users/YOU/firefox"



Now, in a different terminal window, start IRB and launch a Selenium client (this example uses the a bogus page and assumes a signed-up account, which can easily be done manually from the same page)



> irb



---------



# create and launch the selenium client (naming it '@browser')

require 'selenium/client'

@browser = Selenium::Client::Driver.new \

:host => "localhost",

:port => 4444,

:browser => "*chrome",

:url => "http:/www.bogus.com",

:timeout_in_second => 60



@browser.start_new_browser_session

# goto nowhere

url = "http://www.bogus.com/nowhere"

@browser.open url

# login

@browser.type("//form[@id='signInForm']//input[@name='email']", "you@email.com")

@browser.type("//form[@id='signInForm']//input[@name='password']", "password")

@browser.click("//form[@id='signInForm']//a")



# once signed in, see how many tutorial links there are

@browser.get_xpath_count("//div[@id='videoCategories']//td")



--------



change the url string to any website you like, and once there, select any widget, right-click on it and select 'Inspect Element' to bring up the Firebug console and start experimenting with the xpaths.



Some of the more common Selenium Client commands:

@browser.click(xpath_to_clickable)

@browser.type(xpath_to_input_form)

@browser.is_text_present(some_string)

@browser.go_back



The Selenium client commands come in different languages, and are essentially the same in all of them. The main difference between methods in Ruby and Java is that the Java methods use camelCase (isTextPresent instead of is_text_present) - this makes it pretty easy to translate your ad-hoc Ruby experiments into Java test cases

No comments:

Post a Comment