2016-05-30 33 views
3

我需要選擇由ng-repeat創建的文本框,並使用sendKeys函數發送一些值。但是我不確定選擇文本框的方法。請提出一個方法來完成這個或者我應該使用css選擇器來代替。

<div class="qst_input_hld ng-scope" ng-repeat="option in options track by $index"> 
<input type="text" class="input-sm ng-pristine ng-valid" ng-model="options[$index]" placeholder="Option 1" ng-class="isOptionEmpty[$index] ? 'error-border' : ''"> 
<!-- ngIf: $index > 1 --> 
</div> 

回答

0

提供一個名稱和ID屬性的文本框:

<input ... name="your_textbox" id="textbox_{{index}}" /> 

如果你想選擇所有的文本框:

document.getElementsByName("your_textbox"); 

具體文本框:

document.getElementById("textbox_"+i); //i=index 

通過量角器: 首先改變你的輸入框的NG-模式:

<input type="text" class="input-sm ng-pristine ng-valid" ng-model="option" placeholder="Option 1" ng-class="isOptionEmpty[$index] ? 'error-border' : ''"> <!-- see ng-model=option --> 

然後使用模型選擇它:

var repeaterElements= element(by.repeater('option in options')); 

repeaterElements.each(function(entry) { 
    var input = entry.element(by.model("option")); 
}); 
+0

其實我的意思是選擇在量角器端到端測試框架的文本框通過使用元素(by.repeater)。可能嗎。 – albert

+0

看看更新的答案是否有幫助。 – Rambler

4

有多種方式查找的文本輸入,因爲有一箇中繼器在那裏,我懷疑有多個文本框。假設你要發送鍵,第一個,這裏是一個選項:

var desiredInput = element.all(by.repeater("option in options")).first().all(by.tagName("input")).first(); 
desiredInput.sendKeys("desired text"); 

請注意,您並不需要處理所有track by一部分 - 它是過得去量角器剝奪(source code reference) 。

另請注意,我剛剛使用了by.tagName()技術,根據您是否有其他input元素可能會或可能不會。您可以更加嚴格,並使用替代的CSS選擇器。檢查佔位符:

var desiredInput = element.all(by.repeater("option in options")).first().$('input[placeholder="Option 1"]'); 

而且,如果你想發送鍵輸入元素在轉發每一個項目,使用each()

element.all(by.repeater("option in options")).each(function (elm) { 
    var desiredInput = elm.$('input[placeholder="Option 1"]'); 
    desiredInput.sendKeys("desired text"); 
}); 
+1

+1提到我們根本不需要處理跟蹤。我正在谷歌搜索中尋找這個確認,並且讓我回答了這個問題。我會向量角器團隊詢問更多關於這方面的文檔。 – Keith

+0

@凱蒂謝謝!順便說一下,我已經爲[eslint-plugin-protractor' ESLint插件](https://github.com/alecxe/eslint-plugin-protractor)添加了一條規則,如果您使用擴展重複器語法在'by.repeater()'裏面定位器。 [檢查出來](https://github.com/alecxe/eslint-plugin-protractor/blob/master/docs/rules/use-simple-repeaters.md)。 – alecxe

+2

謝謝!供參考,問題記錄:https://github.com/angular/protractor/issues/3634 – Keith

相關問題