5

Protractor hangs completely when trying to get any element property after logging in (idk if it's related to logging in or related just to switching pages).

it("Should get location of main container", async function() {
    await LoginPage.validLogin();
    // Works and logs in the dashboard
    await browser.sleep(3000);
    // Get the main container by class name
    const container = await element(by.css(".main-container"));
    // Logs properly the element functions (as expected)
    console.log(container);
    console.log(await container.getLocation()); // Hangs here
});

In this case, I'm trying to get the location of the main container element on the page. The first console.log fires and shows properly, while the second hangs completely, so I get the script timeout. Increasing the timeout time doesn't help at all...

I found online that misusing $timeout in AngularJS instead of using $interval may lead to this strange behaviour, but I really can't skim through the entire (very big!) project's codebase to change everything hoping that it just works, not to talk about the external libraries using $timeout.

I have SELENIUM_PROMISE_MANAGER = false; in my Protractor config so I disabled the built-in Control Flow in order to manually manage the promises using async/await, but even if I use the built-in Control Flow without using async/await I get the very same behaviour and error. I'm using Jasmine as testing framework.

Maybe I'm missing something? Any help would be much appreciated, thanks!

3 Answers3

1

This is caused by the fact that angular is not stable. Have a look at the link below. I found my answer there. When the page you are trying to test is open go to the browser dev tools and type in the console getAllAngularTestabilities(). There are a few properties here that indicate whether angular is ready to be tested. hasPendingMicrotasts needs to be false. hasPendingMacroTasks needs to be false. isStable needs to be true. I put a screenshot below. In my screenshot hasPendingMacrotasks is true and it must be false. So the page I looked at was not ready to be tested.

Failed: script timeout: result was not received in 11 seconds From: Task: Protractor.waitForAngular() - Locator: By(css selector, #my-btn)

console. My page is not ready for testing

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
Dani Oprean
  • 45
  • 1
  • 7
0

Try something like this:

it("Should get location of main container", async function() {
    await LoginPage.validLogin();
    const container = await element(by.css(".main-container"));
    await browser.wait(protractor.ExpectedConditions.presenceOf(container), 5000, 'Element taking too long to appear in the DOM');
    await console.log(await container.getLocation());
});
Kuba Lubas
  • 81
  • 3
0

I don't think that getLocation() exists in the Javascript bindings for selenium. I couldn't find it in the source code anyway. So that promise will never return which is why it hangs. But I the you can achieve basically the same thing with getRect():

it("Should get location of main container", async function() {
    await LoginPage.validLogin();
    const container = await element(by.css(".main-container"));
    await browser.wait(protractor.ExpectedConditions.presenceOf(container), 5000, 'Element taking too long to appear in the DOM');
    await console.log(await container.getRect());
});
C. Peck
  • 3,641
  • 3
  • 19
  • 36