Angularjs 使用量角器行中的文本获取行号

Angularjs 使用量角器行中的文本获取行号,angularjs,protractor,angularjs-e2e,e2e-testing,Angularjs,Protractor,Angularjs E2e,E2e Testing,我的表格结构如下 <table class="table"> <thead> <tr> <th class="checkbox-column"></th> <th class="main-column main-column--checkbox">Name</th> </tr> </thead> <tbody ng-repeat="

我的表格结构如下

<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>

名称
第2行
第1行
我需要使用下面的
元素(通过.repeater('tests').row(0)).element(通过.css('xxx]'))
。目前,我使用硬编码的行号来实现这一点,但由于动态用户界面的变化,它不能一直工作

通过在量角器中传递文本,可以使用获取行号吗

例: 如果我通过了文本
ROW2
,那么它应该使用测试中的中继器
(a,b)将行号返回为0
,那么我可以动态地使用行号

通过xpath找到它:

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

函数GetRowNumber(yourText) { 变量项=元素(按.repeater('tests')).all(按.tagName('h4')).map(函数(elem){ 返回elem.getText(); }); 返回项目。然后(功能(元素){ 对于(变量i=0;i
此代码尚未测试。

您可以使用
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"));

实际上
元素(by.xpath('XXX'))
部分不是问题。需要获取行号
row(0)
应使用该行中的文本动态获取感谢,也可以将其展开以从嵌套表中获取值吗?i、 e使用元素
test.name
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"));
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'));