Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Provide documentation on how to use correctly browser.wait with protractor.ExpectedConditions #3578

Open
evilaliv3 opened this issue Sep 20, 2016 · 4 comments
Assignees

Comments

@evilaliv3
Copy link
Contributor

I'm trying to use the protractor.ExpectedConditions together with browser.wait() but i'm sporadically failing with the error: stale element reference: element is not attached to the page document.

The way i'm using the function is the following:

function (locator, timeout) {
  var t = timeout === undefined ? exports.browserTimeout() : timeout;
  var EC = protractor.ExpectedConditions;
  browser.wait(EC.invisibilityOf(element(locator)), t);
};

the reason that is causing the failure is probably that between the call to element(locator) and the usage of the element i've applications refresh that remove the element and cause the issue when the refernece is used.

i've not found any good documentation to use this routines without having this issue.

could you please provide some guidelines?

@juliemr juliemr changed the title Provide documentation on how to use correctly borwser.wait with protractor.ExpectedConditions Provide documentation on how to use correctly browser.wait with protractor.ExpectedConditions Sep 20, 2016
@evilaliv3
Copy link
Contributor Author

@juliemr it seems to me that this is related to #2277

is it possible that there is a regression?

@evilaliv3
Copy link
Contributor Author

Solved using element.isVisible in place of the ExpectedCondition:

function (locator, timeout) {
  var t = timeout === undefined ? exports.browserTimeout() : timeout;
  return browser.wait(function() {
    return element(locator).isDisplayed().then(function(present) {
      return present;
    });
  }, t);
};

i would suggest @cnishina that also for EC.invisibilityOf you adopt the same returning false in place of raising an exception.

@evilaliv3
Copy link
Contributor Author

evilaliv3 commented Sep 21, 2016

No damn, i'm still getting the error in a sporadic way even using the isDisplayed().

i confirm so that we are facing a regression on #2277

@tilmanschweitzer
Copy link
Contributor

@evilaliv3 Hi, this issue is caused by a race condition between the isPresent and isDisplayed check. If the element is removed between both calls or while isDisplayed is executed you get a NoSuchElementError or a StaleElementReferenceError.

This problem is solved for visibilityOf (https://github.com/angular/protractor/pull/3958/files), but it still exists for textToBePresentInElement and probably others.

Before these changes are release in a future version your can monkey patch your ExpectedContions and use workaround functions that I put in an npm module

https://www.npmjs.com/package/protractor-save-expected-conditions

heathkit pushed a commit that referenced this issue Jan 27, 2017
…ibility (#4006)

Add test cases to reproduce the missing element race conditions possible in
expected condition methods `visibilityOf`, `textToBePresentInElement`,
`textToBePresentInValue` and `elementToBeClickable`.

Add error handler `falseIfMissing` to all expected conditions that depend
on the presence of an element.

Expected conditions check the presence of an element before other checks,
but when an element is removed exactly in the moment after the `isPresent`
and before `isDisplayed` in `visibilityOf` the condition used to fail.

This solution does not handle missing elements in (`isEnable`, `isDisplayed`, `isSelected`) and focused only on expected conditions (see
#3972)

This problem was also referenced in
#3578 and
#3777
igniteram pushed a commit to igniteram/protractor that referenced this issue Feb 21, 2017
…ibility (angular#4006)

Add test cases to reproduce the missing element race conditions possible in
expected condition methods `visibilityOf`, `textToBePresentInElement`,
`textToBePresentInValue` and `elementToBeClickable`.

Add error handler `falseIfMissing` to all expected conditions that depend
on the presence of an element.

Expected conditions check the presence of an element before other checks,
but when an element is removed exactly in the moment after the `isPresent`
and before `isDisplayed` in `visibilityOf` the condition used to fail.

This solution does not handle missing elements in (`isEnable`, `isDisplayed`, `isSelected`) and focused only on expected conditions (see
angular#3972)

This problem was also referenced in
angular#3578 and
angular#3777
bodyduardU pushed a commit to bodyduardU/protractor that referenced this issue Dec 5, 2022
…ibility (#4006)

Add test cases to reproduce the missing element race conditions possible in
expected condition methods `visibilityOf`, `textToBePresentInElement`,
`textToBePresentInValue` and `elementToBeClickable`.

Add error handler `falseIfMissing` to all expected conditions that depend
on the presence of an element.

Expected conditions check the presence of an element before other checks,
but when an element is removed exactly in the moment after the `isPresent`
and before `isDisplayed` in `visibilityOf` the condition used to fail.

This solution does not handle missing elements in (`isEnable`, `isDisplayed`, `isSelected`) and focused only on expected conditions (see
angular/protractor#3972)

This problem was also referenced in
angular/protractor#3578 and
angular/protractor#3777
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants