How to Select Option in Drop Down Protractorjs E2E Tests

How to select option in drop down protractorjs e2e tests

I had a similar problem, and eventually wrote a helper function that selects dropdown values.

I eventually decided that I was fine selecting by option number, and therefore wrote a method that takes an element and the optionNumber, and selects that optionNumber. If the optionNumber is null it selects nothing (leaving the dropdown unselected).

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

I wrote a blog post if you want more detail, it also covers verifying the text of the selected option in a dropdown: http://technpol.wordpress.com/2013/12/01/protractor-and-dropdowns-validation/

How to select a drop down using protractor?

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"]'));

How to select option in drop down protractorjs e2e tests

I had a similar problem, and eventually wrote a helper function that selects dropdown values.

I eventually decided that I was fine selecting by option number, and therefore wrote a method that takes an element and the optionNumber, and selects that optionNumber. If the optionNumber is null it selects nothing (leaving the dropdown unselected).

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

I wrote a blog post if you want more detail, it also covers verifying the text of the selected option in a dropdown: http://technpol.wordpress.com/2013/12/01/protractor-and-dropdowns-validation/

How to select option in drop down protractorjs e2e tests

I had a similar problem, and eventually wrote a helper function that selects dropdown values.

I eventually decided that I was fine selecting by option number, and therefore wrote a method that takes an element and the optionNumber, and selects that optionNumber. If the optionNumber is null it selects nothing (leaving the dropdown unselected).

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

I wrote a blog post if you want more detail, it also covers verifying the text of the selected option in a dropdown: http://technpol.wordpress.com/2013/12/01/protractor-and-dropdowns-validation/

protractor select values from drop down implemented using mat-select

This is how I solved it. First I had to click the dropdown and then another click for selecting the value.

element(by.tagName('mat-toolbar')).element(by.tagName('mat-select')).click();

element(by.cssContainingText('mat-option .mat-option-text', 'Delta')).click();
browser.waitForAngular();

Thanks for your guidance guys. Appreciate it.

Select a particular option in Protractor

Found the perfect answer.

element.all(by.css('cssSelector of the dropdown')).each(function (eachElement, index) 
{
eachElement.click();// select the <select>
browser.driver.sleep(500);// wait for the renderings to take effect
element(by.css('cssSelector of option want to select')).click();// select the first md-option
browser.driver.sleep(500);// wait for the renderings to take effect
});

This works like calm :)



Related Topics



Leave a reply



Submit