使用Protractor中的行中的文本获取行号

时间:2016-12-13 13:00:05

标签: angularjs protractor angularjs-e2e e2e-testing

我的表格结构如下


declare module 'underscore';

我需要使用以下内容获取表格单元格 <table class="table"> <thead> <tr> <th class="checkbox-column"></th> <th class="main-column main-column--checkbox">Name</th> </tr> </thead> <tbody ng-repeat="(a, b) in tests" class="ng-scope" style=""> <tr class="panel__sub-header"> <td> <input name="item-checkbox" ng-click="toggleSelectAll(a)" type="checkbox"> </td> <td colspan="4"> <h4 class="ng-binding">ROW2</h4> </td> </tr> <tr ng-repeat="test in testData" ng-class-odd="'odd'" ng-class-even="'even'" class="ng-scope odd" style=""> <td class="checkbox-column"><input name="item-checkbox" ng-click="checkFunction()" type="checkbox"></td> <td class="main-column"> <a ng-href="#" class="ng-binding" href="#">test.name</a> </td> </tr> <tr ng-repeat="test in testData" ng-class-odd="'odd'" ng-class-even="'even'" class="ng-scope odd" style=""> <td class="checkbox-column"><input name="item-checkbox" ng-click="checkFunction()" type="checkbox"></td> <td class="main-column"> <a ng-href="#" class="ng-binding" href="#">test.data</a> </td> </tr> </tbody> <tbody ng-repeat="(a, b) in tests" class="ng-scope" style=""> <tr class="panel__sub-header"> <td> <input name="item-checkbox" ng-click="toggleSelectAll(a)" type="checkbox"> </td> <td colspan="4"> <h4 class="ng-binding">ROW1</h4> </td> </tr> <tr ng-repeat="test in testData" ng-class-odd="'odd'" ng-class-even="'even'" class="ng-scope odd" style=""> <td class="checkbox-column"><input name="item-checkbox" ng-click="checkFunction()" type="checkbox"></td> <td class="main-column"> <a ng-href="#" class="ng-binding" href="#">test.name</a> </td> </tr> <tr ng-repeat="test in testData" ng-class-odd="'odd'" ng-class-even="'even'" class="ng-scope odd" style=""> <td class="checkbox-column"><input name="item-checkbox" ng-click="checkFunction()" type="checkbox"></td> <td class="main-column"> <a ng-href="#" class="ng-binding" href="#">test.data</a> </td> </tr> </tbody> </table> 。目前我正在使用硬编码的行号来实现这一点,但由于动态UI更改而无法一直工作

是否可以通过在量角器中传递文本来获取行号

例: 如果我传递文本element(by.repeater('(a, b) in tests').row(0)).element(by.css('[xxx]')),那么它应该使用转发器ROW2将行号返回为0然后我可以动态使用行号

3 个答案:

答案 0 :(得分:2)

您可以使用filter()方法获取包含预期文本的特定行,而不是通过获取行号来执行此操作。

var columnElement = element.all(by.repeater('(a, b) in tests')).filter(function(rowElement){
   return rowElement.element(by.css("td h4")).getText().then(function(text){
    return text.trim() == "ROW2"
  });
}).first().element(by.css("somecss"));

答案 1 :(得分:1)

通过xpath找到它:

element(by.repeater('(a, b) in tests').row(0)).element(by.xpath('XXX'))

function GetRowNumber(yourText)
{
    var items = element(by.repeater('(a, b) in tests')).all(by.tagName('h4')).map(function(elem) {
        return elem.getText();
    });

    return items.then(function(elems){
       for(var i = 0; i < elems.length; i++)
       {
          if(elems[i] === yourText)
              return i;
       }
    });
}

此代码尚未经过测试。

答案 2 :(得分:0)

var getRowNumber = function (text) {
    var tbody = element(by.repeater('(a, b) in tests'));
    tbody.filter(function (elem, index) {
       if (!!elem.element(by.cssContainingText('h4', text))) {
          return index;
       }
   })
};
console.log('row number :'+ getRowNumber('ROW2'));