Powered By Blogger

Search Here!

Wednesday, April 2, 2014

Select Dropdown Value in Protractor JS !

Select value from drop down in Protractor JS using Index, Text and Random Text.

/**
* Usage: selectDropdownByNumber ( element, index)
* element : select element
* index : index in the dropdown, 1 base.
*/
exports.selectDropdownByNumber = function (element, index, milliseconds) {
    element.findElements(by.tagName('option'))
        .then(function (options) {
            options[index].click();
        });
    if (typeof milliseconds != 'undefined') {
        browser.sleep(milliseconds);
    }
};


/**
* Usage: selectDropdownByText (selector, item)
* selector : select element
* item : option(s) in the dropdown.
*/
exports.selectDropdownByText = function selectOption(element, item, milliseconds) {
    var desiredOption;
    element.findElements(by.tagName('option'))
    .then(function findMatchingOption(options) {
        options.some(function (option) {
            option.getText().then(function doesOptionMatch(text) {
                if (text.indexOf(item) != -1) {
                    desiredOption = option;
                    return true;
                }
            });
        });
    })
    .then(function clickOption() {
        if (desiredOption) {
            desiredOption.click();
        }
    });
    if (typeof milliseconds != 'undefined') {
        browser.sleep(milliseconds);
    }
};

/**
* Usage: selectRandomDropdownReturnText ( element, milliseconds)
* element : select random element
* index : wait time to select value for drop down.
*/
exports.selectRandomDropdownReturnText = function (element, milliseconds) {
    return element.findElements(by.tagName('option')).then(function (options) {
        var randomNumber = Math.floor((Math.random() * options.length
        ));
        options[randomNumber].click();
        return options[randomNumber].getText().then(function (text) {
            return text;
        })
    })
    if (typeof milliseconds != 'undefined') {
        browser.sleep(milliseconds);
    }
};

2 comments:

  1. Whats the problem with using sendKeys??

    ReplyDelete
  2. lifesaving, why is this stuff not built into Protractor

    ReplyDelete