使用嵌套转发器中量角器中行中的文本获取元素

时间:2017-01-02 11:19:08

标签: angularjs selenium-webdriver 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="(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>

我使用下面的答案https://stackoverflow.com/a/41129924/2833311

获得了根表元素

但未能获得嵌套的转发器元素。

getSpecificNestedCell: function(tableObject, rowText, nestedTableObj, rowText1, columnCss) {
    var ele = element.all(by.repeater(tableObject)).filter(function(rowElement){
      return rowElement.element(by.css("td h4")).getText().then(function(text){
        return text.indexOf(rowText) !== -1;
      });
    }).first();

    ele = ele.all(by.repeater(nestedTableObj)).filter(function(rowElement1){
        return rowElement1.element(by.linkText(rowText1)).getText().then(function(text1){
          return text1.indexOf(rowText1) !== -1;
        });
      }).first().element(by.css('[' + columnCss + ']'));

    findObject.waitUntilReady(ele);
    return ele;   }

在哪里,

  1. tableObject ==&gt; (a,b)在测试中
  2. rowText ==&gt; ROW2
  3. nestedTableObj ==&gt;在testData中测试
  4. rowText1 ==&gt; test.data
  5. columnCss ==&gt; NG-点击= “checkFunction()”
  6. 使用上面的代码我可以获得元素,如果它在嵌套的重复内部有单行,但是当嵌套的转发器有多行时它无法获取元素

1 个答案:

答案 0 :(得分:2)

这应该给你你想要的东西

function getSpecificNestedCell(tableObject, rowText, nestedTableObj, rowText1, columnCss) {
   return element(by.cssContainingText('tbody[ng-repeat="' + tableObject + '"]', rowText))
   .element(by.cssContainingText('tr[ng-repeat="' + nestedTableObj + '"]', rowText1))
   .element(by.css('[' + columnCss + ']'));
}

这是我用过的选择器 enter image description here