从ElementArrayFinder获取特定元素会给我带来问题

时间:2016-11-22 15:21:38

标签: protractor

这是来自Get a specific element from an ElementArrayFinder in protractor based on the text of getText()

的后续问题

因此,使用我给出的答案,我使用了.filter()方法。它找到了我想要的行,但我得到了

Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By(css selector, li)

所以,我把一些记录进去了。这是我的代码:

function getCardByName(name) {
    let cards = element.all(by.css(li));
    cards.count().then(c => console.log("The count is ", c));
    let theCard = cards.filter(function(el, i) {
        return el.getText().then(function(text) {
            text = text.substr(0, text.search(" ID:"));
            console.log(i + ") Does the text, " + text + ", match name, " + name + "?  " + (text == name) + " | " + (text === name));
            text == name
        });
     }).first();

     return new Card(theCard);
}

此代码将选择并返回第一个ElementFinder对象,并将其作为参数传递给Card对象。

我在实际页面上得到的结果是(名字不是真实的):

The count is  7
0) Does text, KERGER, FEYSAL, equal name, CRAVEN, LILLARD?  false | false
1) Does text, JENNINGS, JEWELLAN, equal name, CRAVEN, LILLARD?  false | false
2) Does text, CRAVEN, LILLARD, equal name, CRAVEN, LILLARD?  true | true
3) Does text, HORSTMANN, GREG, equal name, CRAVEN, LILLARD?  false | false
4) Does text, MEUSA, FRANKLIN, equal name, CRAVEN, LILLARD?  false | false
5) Does text, LAURITO, RANDOLPH, equal name, CRAVEN, LILLARD?  false | false
6) Does text, JHANSON, LORENE, equal name, CRAVEN, LILLARD?  false | false
0) Does text, KERGER, FEYSAL, equal name, CRAVEN, LILLARD?  false | false
1) Does text, JENNINGS, JEWELLAN, equal name, CRAVEN, LILLARD?  false | false
2) Does text, CRAVEN, LILLARD, equal name, CRAVEN, LILLARD?  true | true
3) Does text, HORSTMANN, GREG, equal name, CRAVEN, LILLARD?  false | false
4) Does text, MEUSA, FRANKLIN, equal name, CRAVEN, LILLARD?  false | false
5) Does text, LAURITO, RANDOLPH, equal name, CRAVEN, LILLARD?  false | false
6) Does text, JHANSON, LORENE, equal name, CRAVEN, LILLARD?  false | false
0) Does text, KERGER, FEYSAL, equal name, CRAVEN, LILLARD?  false | false
1) Does text, JENNINGS, JEWELLAN, equal name, CRAVEN, LILLARD?  false | false
2) Does text, CRAVEN, LILLARD, equal name, CRAVEN, LILLARD?  true | true
3) Does text, HORSTMANN, GREG, equal name, CRAVEN, LILLARD?  false | false
...


Failed: Index out of bound. ...

这将循环6行13次(使用我实际测试的页面)。但它确实找到了我想要找到的行(参见结果3行的“true | true”?)为什么它循环13次并且不停止并且不在生成的数组中加载匹配结果?

1 个答案:

答案 0 :(得分:2)

根据我的看法,你实际上并没有返回一个布尔值(如果这是你代码中的复制粘贴)。你想要做的是(注意“return text == name”):

function getCardByName(name) {
    let cards = element.all(by.css(li));
    cards.count().then(c => console.log("The count is ", c));
    let theCard = cards.filter(function(el, i) {
        return el.getText().then(function(text) {
            text = text.substr(0, text.search(" ID:"));
            console.log(i + ") Does the text, " + text + ", match name, " + name + "?  " + (text == name) + " | " + (text === name));
            return text == name;
        });
     }).first();

     return new Card(theCard);
}