Reputation: 321
I've been battling this test most of the afternoon, I've commented out what I tried to do but it wouldn't work. I have the ability the search for users within the company, a table is then generated and I want to select the one that matches the name of the user. Currently I just use td.text-center as it is the only td in the table, but this seems really poor and prone to breaking if other users with the same name are in the system.
Attempt 2 seems like it would be the best solution but it always gives the element not found error. Has anyone found a reliable way of locating TD within a table? The method below works in other tables, just not this one....
this.editUser = function(name) {
// Original Code Block
//browser.actions().mouseMove(element(by.css('td.text-center'))).perform();
//var editUserBtn = element(by.css('.user-action-open'));
//editUserBtn.click().then(function() {
// var editDetails = element(by.cssContainingText('li > a', 'Edit Details'));
// browser.wait(EC.visibilityOf(editDetails), 3000);
// editDetails.click();
//});
// Attempt 2
// browser.actions().mouseMove(element(by.css('td.text-center'))).perform();
//browser.sleep(3000);
//var edit = element.all(by.repeater('user in users')).filter(function(rowElement){
// return rowElement.element.all(by.css('td[ng-bind="user.Firstname"]')).getText().then(function(text){
// return text.trim() == name;
// });
//}).first();
//browser.actions().mouseMove(edit).perform();
// Currently just times out and doesn't find the element.
var editUserButton = $('.user-action-open');
browser.wait(EC.visibilityOf(editUserButton), 20000).then(function() {
editUserButton.click().then(function() {
var editDetails = element(by.cssContainingText('li > a', 'Edit Details'));
browser.wait(EC.visibilityOf(editDetails), 3000);
editDetails.click();
});
});
}
Upvotes: 1
Views: 671
Reputation: 5016
You could use ExpectedConditions
to do some waiting instead of arbitrarily sleeping for 3 seconds. You could instead do a browser wait. This will wait up to the timeout if the object is in a desired state.
let users = element.all(by.repeater('user in users');
// the original post shows that this is an element.all
// this would only be true if the user can have multiple first names
let firstName = element(by.css('td[ng-bind="user.Firstname"]'));
browser.actions().mouseMove(element(by.css('td.text-center'))).perform();
// wait to see if the repeater is present or timeout in 3 seconds
browser.wait(users).isPresent(), 3000);
let edit = element.all(users).filter((rowElement) => {
// wait for first name, get the first name of the row's element
// if the text matches the name, resolve the promise true.
return browser.wait(rowElement.firstName, 3000).then(() => {
return rowElement.firstName.getText().then((text) => {
return text.trim() == name;
});
});
}).first();
browser.actions().mouseMove(edit).perform();
The rowElement.firstName
might not be the correct syntax.
Chaining the promises
// wait to see if the repeater is present or timeout in 3 seconds
browser.wait(users).isPresent(), 3000).then(() => {
let edit = element.all(users).filter((rowElement) => {
// wait for first name, get the first name of the row's element
// if the text matches the name, resolve the promise true.
return browser.wait(rowElement.firstName, 3000).then(() => {
return rowElement.firstName.getText().then((text) => {
return text.trim() == name;
});
});
}).first();
browser.actions().mouseMove(edit).perform();
}).catch(err => {
// we could also have asserted the length was at least 1
// maybe we want to catch on something else? maybe this isn't a
// fail case so maybe do not use `fail(`
fail("did not find users");
});
Upvotes: 1