2

I have a page object that looks like this:

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

I want to create a method that will enable me to delete a user based on his email address.

This is what I came up with, basing on 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
  });
});

First I'm trying to filter the row in which there's a user I want delete (array with all rows fitting my filter, then selecting the first row - should be one row anyway) and then click corresponding Delete button.

However, from my debugging I know that the method ignores the filtering and clicks the first Delete button available in the table and not the first from filtered elements. What am I doing wrong?

Community
  • 1
  • 1
anks
  • 303
  • 2
  • 12

2 Answers2

2

In this particular case, I would use an XPath and its following-sibling axis:

function deleteUser(email) {
    element(by.xpath("//td[. = '" + email + "']/following-sibling::td/a")).click();
}
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

I agree with @alexce's short & elegant answer but @anks, why don't you delete inside your filter??

 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
});
});
Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • ha, hadn't thought of that. I will check this in another case that's similar here :) Thanks for input :) – anks Aug 24 '16 at 14:00