addNewOptionToDropDownAndSelect

The following action adds a new option to a given HTML dropdown and selects it:


/*
 * Adds a new option to a dropdown list and selects that option.
 *  locator - locator for the dropdown element to add the option to
 *  text - the option value to set.
 */
Selenium.prototype.doSelectNew = function(locator, text) {
     var element = this.page().findElement(locator);
     if (element.type.match(/select-+/i)) {
         element.options[element.options.length] = new Option(text, text);
         element.options[element.options.length - 1].selected = true;    
     }
}			
			

Example:

open /dropdown.html  
verifySelected teammember Rick
selectNew teammember Bob
verifySelected teammember Bob

This particular excample loads a page, check that the option with label "Rick" is selected, adds an option with label and value 'Bob', and finally checks that the selected option is 'Bob'.

File: 

AttachmentSize
Plain text icon addNewOptionToDropDownAndSelect.txt500 bytes

Type: 

User-Extension