我正在测试一个包含信息列表和卡列表的网页。因此,为了使用方法选择正确的卡,我有以下内容:
getCardByName(expectedName: string): CardActions {
console.log("WatchActions: Getting Card of " + expectedName);
let majorCount = 1;
/*
* watchPageObject.cards: ElementArrayFinder = this.tab.$$('card-watch');
*/
let theCard = this.watchPageObject.cards.filter(function (elCard, index) {
return elCard.getText().then(
function (name) {
name = name.substr(0, name.search('\nAge:'));
console.log(majorCount++ + "-" + index + ") " + name +
" = " + (name == expectedName));
return name == expectedName;
});
}).first();
return new CardActions(new CardPageObject(theCard));
}
明显的缺陷显而易见,如果有两个或更多匹配,则永远不会引用第二个匹配。如有必要,我会越过那个逻辑桥梁。
该方法正常工作,我得到了我期待的返回值。但是,该脚本似乎需要比预期更长的时间,并且控制台日志记录将其显示出来(名称是假的):
1-0) KERGER, FEYSAL = false
2-1) JENNINGS, JEWELLAN = false
3-2) CRAVEN, LILLARD = true
4-3) HORSTMANN, GREG = false
5-4) MEUSA, FRANKLIN = false
6-5) LAURITO, RANDOLPH = false
7-6) JHANSON, LORENE = false
8-0) KERGER, FEYSAL = false
9-1) JENNINGS, JEWELLAN = false
10-2) CRAVEN, LILLARD = true
...
90-5) LAURITO, RANDOLPH = false
91-6) JHANSON, LORENE = false
92-0) KERGER, FEYSAL = false
93-1) JENNINGS, JEWELLAN = false
94-2) CRAVEN, LILLARD = true
95-3) HORSTMANN, GREG = false
96-4) MEUSA, FRANKLIN = false
97-5) LAURITO, RANDOLPH = false
98-6) JHANSON, LORENE = false
我该怎么做才能找出为什么过滤器发现有必要循环遍历7张牌的列表14次?
我希望使用公共页面提供一个通用示例,但该测试运行完美。
FWIW:这是我提出的例子(上面的代码是打字稿,下面的代码是javascript)
describe('Test and show the progression of the .filter() method', function() {
beforeAll(function() {
browser.get("http://www.angulartodo.com/");
});
afterAll(function() {
browser.close();
});
it('add values to todos', function(done) {
// Add a number of "todos".
var todos = ["Deposit Check", "Purchase Groceries", "Pick Up Kids"];
todos.forEach(function(todo) {
element(by.css('.todos form > input')).sendKeys(todo).then(function() {
element(by.css('.todos form > button')).click();
});
});
done();
});
it('display the map progressions', function(done) {
var listItems = element.all(by.css('.todos ul li'));
var majorCount = 1;
listItems.filter(function(el, index) {
return el.element(by.css('span:nth-child(2)')).getText().then(function(text) {
console.log(index + ") " + text);
return text.includes("Up");
});
}).getText().then(function(text) {
console.log(text);
});
done();
});
});