Protractor isPresent() timing needs Angular-aware diagnosis
0 reputation · 19 May 2021, 17:45 UTC
0 reputation · 19 May 2021, 17:45 UTC
The goal is to define a reliable presence assertion for an element rendered asynchronously by Angular in a Protractor 7.x test. The relevant APIs appear to overlap: waitForAngular() synchronizes with Angular’s event loop, while element().isPresent() reports whether a located element exists in the DOM.
The unresolved point is how isPresent() behaves when an implicitlyWait() timeout is configured. A delayed element lookup could make a presence check wait for the locator, while an element that exists before Angular stabilizes could still be transient or not yet interactive. The intended assertion should distinguish “not rendered” from “not ready for interaction” without turning a boolean check into an implicit synchronization mechanism.
For Protractor 7.x with Jasmine 4, what is the documented timing contract of isPresent() under a nonzero implicit wait, and should an existence assertion precede or follow waitForAngular()? What test should verify that distinction?
37775 reputation · 20 May 2021, 01:42 UTC
isPresent() is a snapshot check, not a waiter. It delegates to Selenium's findElements (plural), which does not honor implicitlyWait(). A nonzero implicit wait has no effect on the boolean result—it returns immediately whether the element exists in the DOM at that instant.
Order: Call waitForAngular() before asserting presence for Angular-rendered elements. waitForAngular() synchronizes with Angular's Zone.js task queue; isPresent() only reports DOM existence. An element can exist before Angular stabilizes or appear only after, so presence and readiness are separate concerns.
isPresent() resolves synchronously (modulo WebDriver round-trip) to true/false based on current DOM state.implicitlyWait() applies only to findElement (singular), not findElements.waitForAngular() blocks until Angular's testability API reports no pending tasks; it does not wait for DOM mutations directly.Run this minimal spec to confirm the timing contract in your environment:
describe('isPresent timing contract', () => {
beforeAll(() => {
browser.manage().timeouts().implicitlyWait(5000);
browser.waitForAngularEnabled(true);
});
it('returns false immediately for not-yet-rendered element', async () => {
await browser.get('/page-with-delayed-element'); // element appears after ~2s
const start = Date.now();
const present = await element(by.id('delayed')).isPresent();
const elapsed = Date.now() - start;
expect(present).toBe(false);
expect(elapsed).toBeLessThan(1000); // should NOT wait 5s
});
it('returns true after Angular stabilizes', async () => {
await browser.waitForAngular();
const present = await element(by.id('delayed')).isPresent();
expect(present).toBe(true);
});
});
For reliable assertions on Angular-rendered elements:
await browser.waitForAngular(); — ensure app is stableawait expect(element(locator).isPresent()).toBe(true); — snapshot assertionIf you need to wait for an element, use browser.wait(ExpectedConditions.presenceOf(el), timeout) or visibilityOf(el). Never use isPresent() as a synchronization mechanism.
waitForAngular() can hang on non-Angular pages or if testability API is disabled; guard with browser.waitForAngularEnabled(false) when needed.If the verification test above shows isPresent() waiting near the implicit timeout, share the exact Protractor/Selenium/WebDriver versions and whether you're using a custom ElementFinder wrapper—that would indicate a non-standard override.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.