2016-11-21 8 views
1

目的の要素のテキストに基づいてElementArrayFinderから特定のElementFinderを取得しようとしています。getText()のテキストに基づいて分度器のElementArrayFinderから特定の要素を取得します。

<ul id="names"> 
    <li><span>Adam</span> <span class="ids">ID: 1</span></li> 
    <li><span>Becky</span> <span class="ids">ID: 2</span></li> 
    <li><span>Chris</span> <span class="ids">ID: 3</span></li> 
</ul> 

そして、私は名前「ベッキー」を用いて、第2のLiを選択します:

例えば、私はAngular2アプリが提供する以下のHTMLスニペットを持っていると仮定します。だから私は、次の機能を持っている:

/** 
* Get a Card object that contains the ElementFinder of a specific <li> 
* @param desiredName {string} The name to search on the row 
* @returns {Card} A Card object with a "verifyID" method. 
*/ 
getCardByName(desiredName) { 
    return $$('#names li').map(function(el) { 
     return el.$('span:first-child').getText().then(function(name) { 
      console.log("getCardByName: got the card for " + name); 
      if (name === desiredName) { 
       console.log("getCardByName: returning card"); 
       return new Card(el); // el should be pointing to an <li> element 
      } 
     }); 
    }); 
} 

私はその後、実行してその関数を呼び出す:

it('test Becky', function(done) { 
    getCardByName("Becky").then(function(card) { 
     console.log("SPEC: testing card"); 
     card.verifyId("ID: 2"); 
     done(); 
    }) 
    .catch(function(err) { console.log(err); }); 
}); 

を私は取得しています出力は、私は長い時間を待ってから、参照、次のとおりです。

私の現在のソリューションで
getCardByName: got the card for Adam 
getCardByName: got the card for Becky 
getCardByName: returning card 

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 

、私はここに記載されている問題に実行しておく:https://github.com/angular/protractor/issues/2227

何かが間違っているのか、自分のコードを改善できるのか、あるいは上にリンクされているバグレポートに記載されている問題に対して本当に挑戦しているのかどうか不安です。カードのオブジェクトがどのように見える、それは価値がある何のため

、想定しています

function Card(containerEl) { 
    this.containerEl = containerEl; 
} 

Card.prototype = { 
    constructor: Card, 
    verifyId: function(expectedId) { 
     var el = this.containerEl.$('span.ids'); 
     expect(el.getText()).toEqual(expectedId); 
    } 
} 

答えて

1

あなたはこれが私のためのトリックを行いますが、それは実行するタスクに

var rows = element(by.id('names')); 

var becky = rows.all(by.tagName('li')).filter(function (elem) { 
      return elem.getText().then(function (val) { 
       return val === 'Becky' 
      }); 
     }).first(); 
+0

を行うには、フィルタ()関数を使用することができます私の6列の配列を13回通過します。私はもう1つ質問します。 http://stackoverflow.com/questions/40745593/getting-a-specific-element-from-an-elementarrayfinder-giving-me-issues – Machtyn

関連する問題