【问题标题】:Selecting a row in table based on content and clicking link from selected row - Protractor根据内容在表格中选择一行并单击所选行中的链接 - 量角器
【发布时间】:2016-08-19 21:12:35
【问题描述】:

我有一个如下所示的页面对象:

<table border>
  <th>Email</th>
  <th>action</th>
  <tr current-page="adminUsers.meta.page">
    <td>admin@example.com</td>
    <td><a href="" ng-click="adminUsers.onRemove(user, adminUsers.meta.page)">Delete permanently</a></td>
  </tr>
  <tr current-page="adminUsers.meta.page"> 
    <td>matilda@snape.com</td>
    <td><a href="" ng-click="adminUsers.onRemove(user, adminUsers.meta.page)">Delete permamently</a></td>
  </tr>
</table>

我想创建一种方法,使我能够根据用户的电子邮件地址删除用户。

这是我根据How to find and click a table element by text using Protractor?提出的:

describe('Admin panel', function() {

  it('admin deletes a user', function() {

    var re = new RegExp("matilda@snape.com");
    var users_list = element.all(by.xpath("//tr[@current-page='adminUsers.meta.page']"));
    var delete_btn = element(by.xpath("//a[contains(text(), 'Delete permamently')]"));

    users_list.filter(function(user_row, index) {
      return user_row.getText().then(function(text) {
        return re.test(text);
      });
    }).then(function(users) {
      users[0].delete_btn.click();
    });
    // some assertion, not relevant right now
  });
});

首先,我试图过滤我想要删除的用户所在的行(所有行都适合我的过滤器的数组,然后选择第一行 - 无论如何都应该是一行),然后单击相应的删除按钮。

但是,根据我的调试,我知道该方法会忽略过滤并单击表中可用的第一个删除按钮,而不是过滤元素中的第一个按钮。 我做错了什么?

【问题讨论】:

    标签: javascript angularjs protractor pageobjects


    【解决方案1】:

    在这种特殊情况下,我将使用 XPath 及其 following-sibling 轴:

    function deleteUser(email) {
        element(by.xpath("//td[. = '" + email + "']/following-sibling::td/a")).click();
    }
    

    【讨论】:

      【解决方案2】:

      我同意@alexce 简短而优雅的回答,但是@anks,你为什么不在你的过滤器中删除??

       describe('Admin panel', function() {
      
      it('admin deletes a user', function() {
      
      var re = new RegExp("matilda@snape.com");
      var users_list = element.all(by.xpath("//tr[@current-page='adminUsers.meta.page']"));
      var delete_btn = element(by.xpath("//a[contains(text(), 'Delete permamently')]"));
      
      users_list.filter(function(user_row, index) {
        return user_row.getText().then(function(text) {
          return if(re.test(text)) { //assuming this checks the match with email id
             user_row.delete_btn.click();
          }
        });
      })
      // some assertion, not relevant right now
      });
      });
      

      【讨论】:

      • 哈,没想到。我将在另一种类似的情况下进行检查:) 感谢您的输入:)
      猜你喜欢
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多