Tuesday, June 15, 2010

Retreiving the CheckBox value Using JQuery in Selenium

Like mentioned in the earlier post, retreving the checkbox values on a webpage can also be done using JQuery. Most of the blogs and tutorials we came across covered JQuery selectors, which involved a tedious process of repackaging the selenium-server.jar file. Since the browser recognizes JQuery, we just embedded the JQuery codes in our user-extensions.js, similar to wat was done in the previous post.

The JQuery code below gets the id of all check boxes on a webpage.

Selenium.prototype.getAllCheckBox = function() {   
var $n = this.browserbot.getCurrentWindow().jQuery.noConflict();
  var allVals = [];
        $n('input[type="checkbox"]').each(function() {
        return allVals;

As you can clearly recognize the second line of the code was slightly altered compared to the previous post, because JQuery didnt recognize on which browser it was running its scripts(i.e the test runner suite or the test browser). By declaring $ as a variable and specifying to which browser it actually denotes the checkbox values of the paricular webpage were retreived successfully.

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 dropdownlist too.....