1

I'm new to automation, and I'm running a very basic test, which was running fine, but just recently it stopped working and I keep receiving the following error :

Failed: script timeout: result was not received in 11 seconds

I looked it up and did some basic debugging and found out that Protractor is not finding the element I'm referring (element(by.id('mat-input-0')) - more below in spec.js code file).
I'm using the latest version of Protractor (5.4.2), along with Jasmine last version (3.4.0). I'm testing on the latest chrome browser.

I tried many online solutions (like using async/await, adding long allScriptsTimeout and defaultTimeoutInterval in the config file (as shown below), calling the element using different locators (by xpath, by id, by tagName,...), trying browser.waitForAngular(); or browser.ignoreSynchronization = true,...) but none worked, so I'm wondering if anyone has any input on what might be the solution? (or maybe what is the real problem?)

Element HTML

<input _ngcontent-mkt-c2="" class="mat-input-element mat-form-field-autofill-control cdk-text-field-autofill-monitored ng-pristine ng-invalid ng-touched" formcontrolname="email" matinput="" name="email" placeholder="Email" required="" type="text" id="mat-input-0" aria-invalid="true" aria-required="true">

Spec.js:

describe('Login', function(){
    it('test 1', async function(){
        await browser.get('the STG URL im testing');


    })
    it('set username', async function(){
        await element(by.id('mat-input-0')).sendKeys('root@user.com');


    })
    it('set password', async function(){
        await element(by.id('mat-input-1')).sendKeys('1234');
    })

})

Conf.js:

exports.config = {  
    seleniumAddress: 'http://localhost:4444/wd/hub',
    allScriptsTimeout: 80000,

    framework: 'jasmine',

    specs: ['spec.js'],

    SELENIUM_PROMISE_MANAGER: false,

    jasmineNodeOpts: {
      defaultTimeoutInterval: 50000
    }
  };
DublinDev
  • 2,318
  • 2
  • 8
  • 31
Gėorge
  • 43
  • 1
  • 10
  • `browser.waitForAngular()` is not equivalent to `browser.ignoreSynchronization` you may be thinking of `browser.waitForAngularEnabled()`. Can you post the HTML of the object you are trying to locate? – DublinDev May 21 '19 at 09:29
  • @DublinDev Im guessing `browser.waitForAngular()` will be the same as `browser.waitForAngularEnabled(true)` no? - correct me if im wrong. Here is the HTML of the field im trying to locate: `` – Gėorge May 21 '19 at 13:51
  • I did fix the issue by using `browser.waitForAngularEnabled(false)` but i read that it's not recommended to use, and the tests after will become flaky...so I'm investigating another solution...Im wondering how can i know what is not yet stabilized on my page? (it's over Angular 7). @DublinDev – Gėorge May 21 '19 at 14:10
  • `waitForAngularEnabled(true)` tells Protractor that you want it to wait until all of the Angular calls on the page are complete and the page is stable before *every* action. Mu understanding is that if it is set `waitForAngular()` will be called before attempting any interaction going forward. – DublinDev May 21 '19 at 14:15
  • You are correct about the downsides of disabling `waitForAngularEnabled`. Based on your last comment it appears your issue is definitely related to Protractor not becoming 'stable'. Can you open the console on chrome dev tools and run the following command `getAllAngularTestabilities()`. When you expand the result you will probably see the properties `hasPendingMacrotasks: false` and `hasPendingMicrotasks: true`. Is that the case? – DublinDev May 21 '19 at 14:20
  • Most likely protractor tries to interact with your elements before the page is fully loaded. Try to added explicit wait by `await browser.sleep(10000)` to see if helps, if it does help get back to me and I'll tell how to do it the right way, since sleeping at work is not a good practice LOL also make sure your configs timeouts are greater than 10 000 ms – Sergey Pleshakov May 21 '19 at 14:23
  • Yes @DublinDev, they are under ngZone as follows: `ngZone: t hasPendingMacrotasks: true hasPendingMicrotasks: false isStable: true` – Gėorge May 21 '19 at 14:32
  • Yeah I thought it might. There is likely some polling activity your app is doing that is preventing it from 'stabilizing' (despite isStable being true in your response) and therefore timing out. [This](https://stackoverflow.com/questions/46588749/how-to-debug-timed-out-waiting-for-asynchronous-angular-tasks-failure-to-find-e) was the original posting I found which helped me understand the issue. There is a lot of information there but these timeout issue are not always easy to resolve unfortunately. Read that and see if it is helpful – DublinDev May 21 '19 at 14:46
  • Thanks @DublinDev, I will check, analyze and post back the outcomes...Thanks for the link; im sure it's going to be really helpful...Thanks :) – Gėorge May 21 '19 at 15:40
  • Just so i add an update. I'm still investigating a solution from protractor side (contrarily to the usual solution, which is to update the application code to add `this.ngZone.runOutsideAngular(() => { setTimeout(() => { // Changes here will not propagate into your view. this.ngZone.run(() => { // Run inside the ngZone to trigger change detection. }); }, REALLY_LONG_DELAY); });` ). but so far i found none. Any info would be highly appreciated. Im thinking of a code from protractor side to make asynchronicity run outside ngZone. @DublinDev the link helped a lot. – Gėorge May 26 '19 at 21:49
  • The only way for Protractor to get around the polling issue is to disable `waitForAngularEnabled` unfortunately. This means you will need to manually account for any changes in the page which protractor would normally wait for itself (meaning waiting for elements to load). It's not the end of the world really, it will be like any other test framework – DublinDev May 28 '19 at 07:53
  • Mostly, Protractor will end up acting as Selenium (with a little more flexibility) to treat the page load and the timing out, or not finding some elements since the page didn't yet fully load. Technically you will end up having to deal with promises yourself. We ended up fixing the blocker by updating the access token timer code and to have it running outside the ngZone. ((using the runOutsideAngular block)). – Gėorge Jun 01 '19 at 06:45

1 Answers1

0

There could be many reasons for the error you are receiving but as you said you are new to protractor. I would say first go with easy steps.

After protractor installation globally. Your machine must have folder in %appdata%. Go to the directory path AppData\Roaming\npm\node_modules\protractor\example. In this directory you must see two files conf.js and example_spec.js . In spec file replace url by the url you have and also do the same for tests.

Run config file without making any changes in it. and check if it is working fine then there is some configuration problem.

Please go through Protractor learning to learn more about it.

Nitin Sahu
  • 611
  • 6
  • 12