Wait for Angular zone to be stable

See original GitHub issue

See here: https://playwright.dev/docs/protractor#polyfilling-waitforangular

Edit: From maintainers


Introduction

Hi! I’m starting with Playwright and did my first projects.

I come from Protractor, that made me start easily to the Web Frontend E2E testing, but let me tell you that Playwright is fulfilling our needs incredibly and looks so promising.

We are migrating some projects we had working with Protractor to Playwright and we found it easy except for one thing. Protractor worked very well with Angular applications since it had the waitForAngular function that waits for the Angular zone (from zonejs) to be stable. That was so useful to avoid manual waits of network requests or animations.

The problem

We have solved it successfully by using the waitForFunction method and tests are working the same as with Protractor. But they have become less descriptive and they include programming logic that we would like to have as short/easy to read as it was.

Example

In a Protractor project we had something like:

expect(page.getServiceListItems().count()).toBe(0);

As Protractor waits for the Angular Zone to be stable, the initial page load and further ajax asynchronous calls are made before this verification is done, so the ServiceList is populated when we verify that the count is not 0 (data to populate ServiceList comes from an ajax call).

In the Playwright project we have solved like this:

wait(() => {
    return new Promise(async (resolve, reject) => {
        const service_list_items = await page.getServiceListItems();
        if ( service_list_items.count() > 0 ) {
            resolve(true);
            return;
        }
        reject();
    });
}, TIMEOUT_MILLIS, DEBOUNCE_MILLIS, '0 apps were listed expecting more than 0')

The wait function is something similar to the Playwright’s page.waitForFunction, it waits until the promise is resolved.

The fact is that with Protractor we had it resolved in one line and with Playwright we have to create a function to make the verification.

Feature Request

We would like to have an optional “Angular mode” that waits for the Angular zone to be stable before every webdriver action so that ensures that HTTP requests/animations/whatever thing blocking Angular zone is not happening at this time.

Links

waitForAngular at Protractor docs

We must say that this is NOT a blocking thing as we found workarounds to solve it. But we think that could help to the readability/manteinability of the Playwright projects and would make lots of people working with Protractor to make their lives easier if they are thinking of migrating to Playwright.

Thank you so much for your support and for such a nice E2E framework.

Greetings.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
manuman94commented, Sep 1, 2021

Hi @aslushnikov,

Wow, that is awesome! Nice approach.

I have a question about it: since Protractor is not going to be mantained anymore in a not so far future, woudn’t be better to import only the clientsidescripts.js file instead of depending on the whole Protractor module?

On the other hand, it would be awesome if Playwright provide us a way to “hook” an asynchronous function before any webdriver action (such as obtaining ElementHandles, clicking, typing, etc.) to implement our custom waits hooked instead of using “await waitForAngular(page)” before all actions.

Thank you!

2reactions
jhyotcommented, Aug 25, 2021

I don’t really have a stake is this, and wouldn’t wish to deny anybody a feature they want 😃 , but I wanted to mention that Protractor is being deprecated by Google, and in the deprecation rationale (https://github.com/angular/protractor/issues/5502) they explicitly mention waitForAngular:

Although waitForAngular is useful, it strongly couples the testing platform to the Angular framework. Some teams at Google have found that solutions that do not require knowledge of Angular can perform the tests equally well by using a robust retry strategy. We believe this is how e2e testing should be done going forward, and projects in Google are already converging towards a testing platform that is WebDriver compliant and framework agnostic. This allows our web test team to maintain a single solution for all web applications.

So in light of this it might be questionable to insert something into a 3rd-party-framework, which itself is rather going away in Angular.

We are using Playwright with Angular, and waitForAngular didn’t really work that well in edge cases in our previous Selenium-based tests anyway, and now our Playwright tests seem to be quite stable even without waitForAngular, thanks to Playwright’s robust auto-waiting and -retrying mechanisms.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Protractor wait for angular application to be stable
I have an issue when running e2e tests with Protractor. It seems the whenStable is being called too early (a fraction of a...
Read more >
NgZone
A zone is an execution context that persists across async tasks. You can think of it as thread-local storage for the JavaScript VM....
Read more >
9 Understanding timeouts - Testing Angular Applications
Waiting for Angular to be stable prevents your test from interacting with the page while Angular is in the middle of an update,...
Read more >
Timeouts
For Angular apps, Protractor will wait until the Angular Zone stabilizes. This means long running async operations will block your test from continuing....
Read more >
Angular Testability: Dealing with Selenium or Protractor ...
If you trigger that a setInterval as soon as your app boots up, you're toast in terms of testing if you don't take...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found