2016-05-25 3 views
0

さて、私はこのコードを書いてみることを試みているが失敗している。私がしたいのは、関数に渡されるインデックスがページ上の利用可能な要素の範囲内にあることを確認することです。そしてもしそうなら、要素参照を返す。分度器の約束と変数と結果を同期させる

​​3210

だから、私はこの機能を持っている:

function getRow(index) { 
    // get count of rows. 
    var count = $$('li').count(); 

    // Ensure a parameter has been passed. 
    if (!index) { 
     throw Error("A parameter must be supplied for function getRow. Valid values are 0 through " + count-1); 
    } 
    // Ensure the parameter is within bounds 
    if (index < 0 || index >= count) { 
     throw Error("The parameter, " + index + ", is not within the bounds of 0 through " + count-1); 
    } 

    return $$('li').get(index); 
} 

カウントは本当にカウントされていないため、上記の失敗しますが、約束を

は、私は次があるとします。

私はさまざまな方法で修正しようとしました。次のように私は成功するだろうと思ったものである:

// get count of rows. 
var count; 
$$('li').count().then(function(c) { count = c; }); 

または

// get count of rows. 
var count = $$('li').count().then(function(c) { return c; }); 

私は「イライラ得て、ブロック場合thenable関数内全体を投げしようとしたが、それはしませんしました「索引」を参照してください。

(私はこれを考え出したと思うたびに、私はそうではない、あらゆる支援をいただき、ありがとうございます。!)

UPDATE:

私が試したの下の提案に続き私のコードを変更:

function getRow(index) { 
    //get count of rows. 
    var count = $$('li').count().then(function(c) { 
    return function() { 
     return c; 
    } 
    }); 

    protractor.promise.all(count).then(function(count) { 
    // Ensure a parameter has been passed. 
    if (!index) { 
     throw Error("A parameter must be supplied for function getRow. Valid values are 0 through " + count-1); 
    } 
    // Ensure the parameter is within bounds 
    if (index < 0 || index >= count) { 
     throw Error("The parameter, " + index + ", is not within the bounds of 0 through " + count-1); 
    } 
    }); 

    return $$('li').get(index); 
} 

しかしprotractor.promise.all().then()ブロック内で、indexが定義されていない、ので、それは失敗しました。また、エラーメッセージでは、私もcountの値を取得しません。

> getRow(0); 
A parameter must be supplied for function getRow. Valid values are 0 through 
+0

配列ではなく単一のオブジェクトに対して 'index'を呼び出しているかもしれません(あなたのロケータに' .all'がありません)? 'var count = element.all(by.css(...))'は役に立ちますか?そして '.each'や' .map'のような.all関数を使いますか? [source](http://www.protractort.org/#/api?view=ElementArrayFinder.prototype.each) – Gunderson

+0

あなたはそうです。私は短縮形$$の使用を避けようとしていましたが、変換には失敗しました。私は質問を更新しました。 – Machtyn

答えて

1

私はあなたがそれを達成するためにはjavascriptクロージャを使用する必要があると思います。

はそれを試してみてください。

var count = element(by.css('li')).count().then(function(c){ 
    return function(){ 
     return c 
    } 
}); 

var value = count(); 

あなたの値は、 "値" 変数にする必要があります。

PS:私は今

+0

次のエラーが表示されます: 'TypeError:countは関数ではありません。 ' – Machtyn

+0

あなたの提案を使用して上記の質問に更新を加えました。 – Machtyn

1

このコードをテストするための分度器環境を持っていないここで私はそれを解決した方法です。

function getRow(index) { 
    var count = $$('li').count(); 

    var test = function(index, count) { 
     // Ensure a parameter has been passed. 
     if (index == undefined) { 
      throw Error("A parameter must be supplied for function getRow. Valid values are 0 through " + count-1); 
     } 
     // Ensure the parameter is within bounds 
     if (index < 0 || index >= count) { 
      throw Error("The parameter, " + index + ", is not within the bounds of 0 through " + count-1); 
     } 
    }); 

    count.then(function(count) { 
     test(index, count); 
    } 

    return $$('li').get(index); 
} 

私はこれがなぜ機能し、私の以前の試みはしませんでした。まあ、私は関数を抽象化し、きれいな方法でインデックスに渡すことができました。

+0

私はおそらく何をしたのかクロージャを作成します。 (flaviomeira10が示唆したように) – Machtyn

関連する問題