我尝试开始使用 Protractor 进行测试,但现在遇到了一个无法解决的问题。
我有这个测试:
describe('Test_3', function() {
var my_url = 'http://wks-15103:8010/ps/ng-components/examples/ps-checkbox.html'
var main_checkbox = element(by.xpath("//div[@ng-model='My_Group']/div[1]/span/span[1]"));
var checkbox_list = element.all(by.xpath("//div[@ng-model='My_Group']/div[2]/div/span/span[1]"));
var title_checkbox_list = element.all(by.xpath("//div[@ng-model='My_Group']/div[2]/div/span"));
var disabled_pri = 'n-check-checkbox';
var enabled_pri = 'n-check-checkbox n-check-checkbox_checked';
beforeEach(function() {
browser.get(my_url);
});
it('schould be chosen',function(){
main_checkbox.click();
checkbox_list.filter(function(elem, index) {
return elem.getAttribute('class').then(function(text) {
return text != 'n-check-checkbox n-check-checkbox_disabled' & text!='n-check-checkbox n-check-checkbox_disabled n-check-checkbox_checked';
});
}).then(function(filteredElements) {
filteredElements.each(function(element, index) {
expect(element.getAttribute('class')).toEqual(disabled_pri);
});
});
});
});
它不起作用。
但后来我尝试使用没有循环的过滤 .each
它工作正常。
describe('Test_3', function() {
var my_url = 'http://wks-15103:8010/ps/ng-components/examples/ps-checkbox.html'
var main_checkbox = element(by.xpath("//div[@ng-model='My_Group']/div[1]/span/span[1]"));
var checkbox_list = element.all(by.xpath("//div[@ng-model='My_Group']/div[2]/div/span/span[1]")); //это список самих чекбоксов
var title_checkbox_list = element.all(by.xpath("//div[@ng-model='My_Group']/div[2]/div/span")); //это список чекбоксов с названиями
var disabled_pri = 'n-check-checkbox';
var enabled_pri = 'n-check-checkbox n-check-checkbox_checked';
beforeEach(function() {
browser.get(my_url);
});
it('schould be chosen',function(){
main_checkbox.click(); //Убрали флажок с группового чекбокса
checkbox_list.filter(function(elem, index) {
return elem.getAttribute('class').then(function(text) {
return text != 'n-check-checkbox n-check-checkbox_disabled' & text!='n-check-checkbox n-check-checkbox_disabled n-check-checkbox_checked';
});
}).then(function(filteredElements) {
filteredElements[0].click();
filteredElements[0].click();
expect(filteredElements[0].getAttribute('class')).toEqual(disabled_pri);
});
});
});
我的错误是什么?
最佳答案
如 docs for filter
中所述它返回 ElementArrayFinder
的实例。当您调用 then
ElementArrayFinder
实例上的方法,它解析为 ElementFinder
的数组s,所以在 then
的回调中您会收到一个纯 JavaScript 数组 ElementFinder
s。要迭代它,您可以使用 native JavaScrupt forEach
:
checkbox_list.filter(function(elem, index) {
// ...
})
.then(function(filteredElements) {
filteredElements.forEach(function(element, index) {
// ....
});
});
否则,ElementArrayFinder
有它自己的方法each
,您应该在 filter
的结果上正确调用它,不在 then
的回调内:
checkbox_list.filter(function(elem, index) {
// ...
})
.each(function(element, index) {
// ....
});
也许the source code for ElementArrayFinder
也可以帮助你。
关于angularjs - Protractor:如何在Protractor中正确使用过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31379109/