Protractor invisibilityOf flakiness, throwing NoSuchElementError #2138
Description
I am testing a non angular app where I have a login helper function in my tests similar to the following:
function login() {
return waitUntilVisible($('#txtUsername'))
.then(function () {
$('#txtUsername').sendKeys('tom');
})
.then(function () {
$('#txtPassword').sendKeys('*');
})
.then(function () {
$('#btnLogin').click();
})
.then(function () {
// this is the flaky line
return browser.wait(protractor.ExpectedConditions.invisibilityOf($('#txtUsername')), 20000, 'Element never disappeared');
});
}
Where browser.wait(protractor.ExpectedConditions.invisibilityOf($('#txtUsername')), 20000, 'Element never disappeared')
waits for a bootstrap modal containing the login form to close. However, this line is proving to be very flaky, occasionally failing with:
NoSuchElementError: No element found using locator: By.cssSelector("#txtUsername")
Or even:
StaleElementReferenceError: stale element reference: element is not attached to the page document
My first though was that the modal was closing before the line was called, however, the docs for invisibilityOf
suggest it should handle the element not being present, stating it as:
An expectation for checking that an element is either invisible or not present on the DOM.
How can I make this line robust?