问题描述
在量角器中,如何处理重复的内容,比如一张表格?例如,给定以下代码,它会踢出一个包含 3 列的表:Index
、Name
和 Delete-Button
在每一行中:
In Protractor, how does one handle repeated content, from say a table? For example, given the following code, that kicks out a table with 3 columns: Index
, Name
and Delete-Button
in each row:
<table class="table table-striped">
<tr ng-repeat="row in rows | filter : search" ng-class="{'muted':isTemp($index)}">
<td>{{$index+1}}</td>
<td>{{row}}</td>
<td>
<button class="btn btn-danger btn-mini" ng-click="deleteRow(row)" ng-hide="isTemp($index)"><i class="icon-trash icon-white"></i></button>
</td>
</tr>
</table>
在我的测试中,我需要根据给定的名称单击删除按钮.在 Protractor 中找到这个的最佳方法是什么?
And in my test I need to click the delete button based on a given name. What's the best way to find this in Protractor?
我知道我可以获取 rows.colum({{row}})
文本,获取该文本的索引,然后单击 button[index]
,但我希望有一个更优雅的解决方案.
I know I could grab the rows.colum({{row}})
text, get the index of that, and then click on the button[index]
, but I'm hoping for a more elegant solution.
例如,在 Geb 中,您可以将行定位符传递给模块,然后该模块将使用列指示符分割每一行.而那个解决方案让我关注 Protractors map 方法......
For example, in Geb, you could pass a row locator to a module, that would then dice up each row with column indicators. And that solution has me eyeing Protractors map method...
推荐答案
你可以使用map或者filter.api 看起来像这样:
You can use map or filter. The api would look like this:
var name = 'some name';
// This is like element.all(by.css(''))
return $$('.table.table-stripe tr').filter(function(row) {
// Get the second column's text.
return row.$$('td').get(2).getText().then(function(rowName) {
// Filter rows matching the name you are looking for.
return rowName === name;
});
}).then(function(rows) {
// This is an array. Find the button in the row and click on it.
rows[0].$('button').click();
});
http://angular.github.io/protractor/#/api?view=ElementArrayFinder.prototype.filter
这篇关于如何在 Protractor 中处理表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!