AngularJS +量角器如何根据其文本而不是值选择下拉选项 [英] AngularJS + Protractor How to select Dropdown option based on its text not value
问题描述
我想通过文本而不是下拉框中的值单击项目.
I want to click on item by it's text and not by it's value from dropdown box.
我发现了这篇很棒的帖子:https://coderwall.com/p/tjx5zg 但它没有'没有按预期工作,在找到匹配项后搜索将永远继续,并且没有单击该项目,
i found this great post : https://coderwall.com/p/tjx5zg but it doesn't work as expected, the search continue forever after match was found and it is not clicking the item,
如果有人有更好的例子(一个有效的例子)或者可以修复这个代码并让它工作,
if someone have better example (a working one) or can fix this code and make it work,
我会明白的.
这是所用帖子中 Dan Haller 的代码(保留所有权利)
This is the code Dan Haller from the post used (all rights reserved to him)
function selectOption(selector, item){
var selectList, desiredOption;
selectList = this.findElement(selector);
selectList.click();
selectList.findElements(protractor.By.tagName('option'))
.then(function findMatchingOption(options){
options.some(function(option){
option.getText().then(function doesOptionMatch(text){
if (item === text){
desiredOption = option;
return true;
}
});
});
})
.then(function clickOption(){
if (desiredOption){
desiredOption.click();
}
});
}
这是一个选择项功能,我可以这样使用:
This is a select item function that I can use like this:
var browser = protractor.getInstance();
browser.selectOption = selectOption.bind(browser);
browser.selectOption(protractor.By.id('my-dropdown'), 'My Value');
推荐答案
这个函数选择你的选择框的第三个选项.
This function selects the 3rd option of your select box.
function selectDropdownByNumber(element, index, milliseconds) {
element.findElements(by.tagName('option'))
.then(function(options) {
options[index].click();
});
if (typeof milliseconds !== 'undefined') {
browser.sleep(milliseconds);
}
}
var mySelect = $('#my-dropdown');
selectDropdownByNumber(mySelect, 2);
更多信息可以在这里找到 - http://qainsight.blogspot.fr/2014/04/select-dropdown-value-in-protractor-js.html
More info can be found here - http://qainsight.blogspot.fr/2014/04/select-dropdown-value-in-protractor-js.html
这篇关于AngularJS +量角器如何根据其文本而不是值选择下拉选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!