Tuesday, May 25, 2010

Retreiving the CheckBox value Using Javascript in Selenium

We had a problem having check boxes with no static identifiers. In this case we evaluated a JavaScript code from selenium RC to get ids of all check boxes and then exercise them.
The Java Script code below gets the id of all check boxes on a webpage.

Selenium.prototype.getAllCheckBox = function() {
var elements = this.browserbot.getDocument().getElementsByTagName('input');
var result = [];
var length = elements.length;
var i=0;
while(i<3) { if (elements[i].type == 'checkbox') { result.push(elements[i].value); } i++; } return result; }; The Selenium server has to be started with the -userExtensions option pointing to the user-extensions.js file. i.e as shown below(exactly the same way) C:\selenium-rc\selenium-server-1.0.3>java -jar selenium-server.jar -userExtensions user-extensions.js

In the selenium code the above function can be called by using doCommand()

my $allRadios = $sel->do_command("getAllCheckBox", '');


The same code can be used to identify the ids of buttons getAllButtons(), images and checkboxes too.....

No comments:

Post a Comment