I\'m using protractor and when I run my tests on browserstack I receive the following error
StaleElementReferenceError: stale element reference: element is n
The way I understand the then()
-functionality, it starts an async task. Therefore any line of code outside the then()
-function will be continued as soon as then()
is entered.
Read more about here, here and here.
Further exist several challenges around browser.get()
and browser.driver.get()
in terms of promise-resolving, as it's not clear, whether the page to load can be synchronized with the ControlFlow. Therefore a browser.driver.get()
isn't always forcing Protractor to wait.
Read more about here and here
Your test now combines these two issues in a way.
I suggest to try browser.waitForAngular();
in your solution to trigger protractor to actually wait until all promises are resolved:
describe('...', () => {
it('...', () => {
expect(element.all(by.css(...).count()).toBe(9);
element.all(by.css(...)).get(0).isDisplayed().then((state) => {
expect(state).toBeTruthy();
});
//instead of using done to explicitly announce the "function finished"
//use browser.waitForAngular() to let Protractor wait for any promises to be resolved.
browser.waitForAngular();
});
}
describe('', () => {
beforeAll(() => {
// .click() returns a promise, so Protractor waits for it to be resolved
//element(by.css('.go-home').click();
browser.driver.get('/'); // doesn't return a promise
browser.waitForAngular(); // triggers Protractor to wait for $http and promises resolved.
});
...
});