user6264
user6264

Reputation: 185

How to select a drop down using protractor?

Can any one help me on how to select a drop down in protractor.

Page Object code

function selectDropdownbyNum(element, optionNum) {
        if (optionNum) {
            element.all(by.tagName('option')).then(function(options) {
                browser.sleep('5000');
                options[optionNum].click();
                console.log('Desired value selected');
            });
        }
    }

var pageName= function(){
this.selectTier = async function(){
        var Tiers = element(by.xpath(/*element value*/));
        console.log('select silver method');
        browser.sleep(5000);
        selectDropdownbyNum(Tiers,2);
        console.log('value selected');
    };
};
module.exports = new pageName();

And Spec is as follows

it('select Silver Tier',async function(){
      browser.ignoreSynchronization = true;
      console.log('Executing silver tier selection test case');
      await pageName.selectTier()
    });

I have tried the above code. I am able to print all the values of the drop down, but am unable to click.

Is their any mistake in the above code.I can print the 'Desired value selected'. But value was not selected

Upvotes: 0

Views: 4249

Answers (3)

Rao
Rao

Reputation: 407

May this will help you for selecting option

element(by.cssContainingText('option','Option value')).click();

or

element(by.id('id')).sendKeys("Values from option");

this worked for me

Upvotes: 2

Anandu S
Anandu S

Reputation: 159

Try:

var Tiers = element(by.xpath(dropDownValue));
Tiers.click();
selectDropdownbyNum(element, optionNum) {
    if (optionNum) {
        element.all(by.tagName('option')).then(function(options) {
            options[optionNum].click();
        });
    }
}
selectDropdownbyNum(Tiers,4)

Note: avoid using Xpath example use :

element(by.css('select[formcontrolname="any value according to situation"]'));

Upvotes: 0

factor5
factor5

Reputation: 330

I haven't tested it, but I suppose it's because of the nested promise you are using inside the for loop. The nature of the promise is to be async, and the for loop is synchronous, which results in the loop complete whyle the very first promise items[i].getText().then get's resolved and that's why your click didn't succeed. If you don't need to know the option names, then just remove the nested promise items[i].getText() and just execute the click in the loop.

Upvotes: 0

Related Questions