我也在进行 e2e 测试,我有几个功能可以选择下拉选项:
按值选择下拉列表。
this.selectDropdownByValue = function (dropdownElement, value) {
return this.selectDropdownByAttribute(dropdownElement, 'value', value);
};
按索引选择下拉列表:
this.selectDropdownByIndex = function (dropdownElement, index) {
dropdownElement.click();
dropdownElement.all(by.css('option')).get(index).click();
};
按部分标签选择下拉列表:
this.selectDropdownByPartialLabel = function (dropdownElement, partialLabel) {
return this.selectDropdownByAttribute(dropdownElement, 'label', partialLabel, true);
};
按标签选择下拉列表:
this.selectDropdownByLabel = function (dropdownElement, label) {
return this.selectDropdownByAttribute(dropdownElement, 'label', label);
};
每个下拉函数内部使用的函数是:
this.selectDropdownByAttribute = function (dropdownElement, attribute, value, partial, index) {
var pathModifier = '';
if (typeof index === 'undefined') {
index = 0;
}
if (typeof partial === 'undefined') {
partial = false;
}
if (partial) {
pathModifier = '*';
}
dropdownElement.click().then(function () {
dropdownElement.all(by.css('option[' + attribute + pathModifier + '="' + value + '"]')).get(index).click();
});
};
我希望这有帮助。