anks
anks

Reputation: 313

Selecting a row in table based on content and clicking link from selected row - Protractor

I have a page object that looks like this:

<table border>
  <th>Email</th>
  <th>action</th>
  <tr current-page="adminUsers.meta.page">
    <td>[email protected]</td>
    <td><a href="" ng-click="adminUsers.onRemove(user, adminUsers.meta.page)">Delete permanently</a></td>
  </tr>
  <tr current-page="adminUsers.meta.page"> 
    <td>[email protected]</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("[email protected]");
    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?

Upvotes: 2

Views: 1290

Answers (2)

Ram Pasala
Ram Pasala

Reputation: 5231

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("[email protected]");
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
});
});

Upvotes: 0

alecxe
alecxe

Reputation: 473823

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();
}

Upvotes: 2

Related Questions