2016-06-28 32 views
0

我正在測試的應用使用中繼器輸出列表,並且希望能夠找到特定行並點擊該行內的編輯按鈕。使用現有定位器單擊中繼器內的元素

我使用.filter找到相關的行,然後點擊編輯按鈕,但如果我指定的定位器按鈕,而不是如果我指的是現有的定義這僅適用:

this.editWineButton = element(by.buttonText('edit')); 

this.editWine = function (wineName) { 
    this.wineListEntries.filter(function(row){ 
     return row.element(by.binding('wine.name')).getText().then(function(name){ 
      return name === wineName; 
     }); 
    }).then(function (elem) { 
    //THIS WORKS 
     var button = elem[0].element(by.buttonText('edit')); 
    //THIS FAILS 
     //var button = elem[0].editWineButton; 
     button.click(); 
    }); 
}; 

已經爲編輯按鈕定義了定位器,我需要做些什麼才能重新使用篩選器的結果?

當我運行的代碼失敗版本我得到的錯誤

「失敗:無法讀取屬性未定義的‘點擊’」。

我覺得答案肯定是顯而易見的,但我看不出我的錯誤。任何建議將非常受歡迎。

回答

0

我試圖ELEM [0] .this.editWineButton的igniteram1的建議,但它並沒有解決問題。不過,我找到了解決辦法。這兩個關鍵部分是使用element.locator(),如Protractor Chained Elements by Using Variables?中所述。其次,加入

_this = this; 

修改後的代碼是:

this.editWineButton = element(by.buttonText('edit')); 

this.editWine = function (wineName) { 
    var _this = this; 
    this.wineListEntries.filter(function(row){ 
     return row.element(by.binding('wine.name')).getText().then(function(name){ 
      return name === wineName; 
     }); 
    }) 
    .then(function (elem) { 
     var button = elem[0].element(_this.editWineButton.locator()); 
     button.click(); 
    }); 
}; 
相關問題