This took me a lot of time to find out, so here is a short and handy overview for everyone that wants to use jQuery selectors instead of Xpath selectors in their Selenium IDE tests. First the steps to achieve this, followed by some comments for the few readers that are still interested.
Add jQuery selectors to Selenium
- The selector engine of jQuery is called Sizzle. Download it from sizzlejs.com and store the file sizzle.js in your Selenium project. (I used the current version, 1.0.)
- Create a blank JavaScript file, rename it to “user-extensions.js” and save it to your Selenium project.
- Add the following code to “user-extensions.js”:
PageBot.prototype.locateElementBySizzle = function(locator, inDocument) {
var results = [];
window.Sizzle(locator, inDocument, results);
return results.length > 0 ? results[0] : null;
}
- Open Selenium IDE from Firefox.
- Go to Options > Options > General.
- Click the “Browse …”-button of “Selenium Core extensions (user-extensions.js)”
- Multiple select “sizzle.js” and “user-selections.js”. They should be added comma-seperated to the input field (for example: “
D:myProjectuser-extensions.js, D:myProjectsizzle.js
“).
- Restart Selenium IDE.
- Execute the following test:
Command: verifyElementPresent
Target: sizzle=body
Value: true
This test should succeed on every normal HTML page. ;)
Comments
- Why use Sizzle and not the complete jQuery library? The same setup can also be used for jQuery, but I could not get it working. Some say that recent versions of jQuery (I use 1.4.4) don’t cope with Selenium, at the moment.
- It’s still possible though to use jQuery functionality in your tests. If jQuery is available on the page you are testing, one could do a test similar to:
Command:assertEval
Target: selenium.browserbot.getUserWindow().jQuery("body").length
Value: 1
Helpful resources