2016-11-22 55 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