Background : Selenium is an open source tool for web application testing.
Selenium IDE is a Firefox plugin allows you to record tests in browser and run them from the browser or as a java test ( or any other languages ) using Selenium Remote Control libraries.
You can extend functionality of Selenium IDE by writing Event Handlers, Locator Builders, Command Builders, Custom Format all in JavaScript.
For more about that http://wiki.openqa.org/display/SIDE/Writing+extensions.
More on Locator Builders
Locator Builders decide how HTML elements in a page are identified.
eg id attribute is a default locator builder.
So while recording a test an html element attribute selenium using the id attribute to recognize the element you changed, clicked etc.
Writing a new locator builder
You can instruct selenium to use a custom attribute to identify the elements in your page. Default Locator builders can be found at chrome://selenium-ide/content/locatorBuilders.js
Here's the js code to add a location builder for an attribute myattr
LocatorBuilders.add('myattr', function(e) {
if (e.hasAttribute('myattr')) {
if (e.getAttribute("myattr")) {
return '//' + e.nodeName + '[@myattr=\'' +
e.getAttribute("myattr") + '\']';
}
}
return null;
});
Since there are plenty of existing location builders, you need to specify the order in which they are invoked. This is how its done
LocatorBuilders.order = ['myattr','id', 'link', 'name', 'dom:name',
'xpath:link','xpath:img', 'xpath:attributes', 'xpath:href',
'dom:index', 'xpath:position'];
Complete code: mylocator.js
LocatorBuilders.add('myattr', function(e) {
if (e.hasAttribute('myattr')) {
if (e.getAttribute("myattr")) {
return '//' + e.nodeName + '[@myattr=\'' +
e.getAttribute("myattr") + '\']';
}
}
return null;
});
LocatorBuilders.order = ['myattr','id', 'link', 'name', 'dom:name',
'xpath:link','xpath:img', 'xpath:attributes', 'xpath:href',
'dom:index', 'xpath:position'];
Steps to add the extension code to selenium IDE
- Write locator code in a new .js file
- Specify the path in "Selenium IDE extensions" field in Selenium IDE > Options and restart selenium IDE