2016-11-22 53 views
1

这是Get a specific element from an ElementArrayFinder in protractor based on the text of getText()获取从ElementArrayFinder给我一个特定的元素发出

因此,一个跟进的问题,用我给出了答案,我用.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次并且没有停止,并且没有将匹配结果加载到结果数组中?

回答

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); 
} 
+0

不幸的是,我只能给出类似于我正在做的示例代码。但是我没有把“回归”与比较者相提并论。 ...那就是我错过的!谢谢。 – Machtyn