【问题标题】:Get the element using the text in the row in Protractor from nested repeater使用来自嵌套中继器的量角器中的行中的文本获取元素
【发布时间】:2017-01-02 11:19:08
【问题描述】:

我的表格结构如下

<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 ==> (a, b) 在测试中
  2. rowText ==> ROW2
  3. nestedTableObj ==> 在 testData 中测试
  4. rowText1 ==> test.data
  5. columnCss ==> ng-click="checkFunction()"

使用上面的代码,如果嵌套重复中有单行,我可以获取元素,但是当嵌套重复器有多行时,它无法获取元素

【问题讨论】:

  • 为什么你使用body标签两次?
  • 这是开发团队的设计
  • 第一个body标签和第二个body标签有什么区别?两者都是一样的。那你为什么要?
  • 其实是根中继器(a, b) in tests绘制的,可以是n个数字。为了理解,我只是硬编码了两次。

标签: angularjs selenium-webdriver protractor angularjs-e2e e2e-testing


【解决方案1】:

我相信这应该会给你你想要的

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 + ']'));
}

这是我工作时使用的选择器

【讨论】:

  • 太棒了。你拯救了我的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 2021-06-03
  • 2020-03-29
  • 2017-01-11
  • 1970-01-01
相关资源
最近更新 更多