async waitForAngular()

in lib/browser.ts [514:598]


  async waitForAngular(opt_description?: string): Promise<any> {
    let description = opt_description ? ' - ' + opt_description : '';
    if (!await this.waitForAngularEnabled()) {
      return true;
    }

    let runWaitForAngularScript = async(): Promise<any> => {
      if (this.plugins_.skipAngularStability() || this.bpClient) {
        return null;
      } else {
        let rootEl = await this.angularAppRoot();
        return this.executeAsyncScript_(
            clientSideScripts.waitForAngular, `Protractor.waitForAngular() ${description}`, rootEl);
      }
    };

    try {
      let browserErr = await runWaitForAngularScript();
      if (browserErr) {
        throw new Error(
            'Error while waiting for Protractor to ' +
            'sync with the page: ' + JSON.stringify(browserErr));
      }
      await this.plugins_.waitForPromise(this);

      await this.driver.wait(async () => {
        let results = await this.plugins_.waitForCondition(this);
        return results.reduce((x, y) => x && y, true);
      }, this.allScriptsTimeout, 'Plugins.waitForCondition()');
    } catch (err) {
      let timeout: RegExpExecArray;
      if (/asynchronous script timeout/.test(err.message)) {
        // Timeout on Chrome
        timeout = /-?[\d\.]*\ seconds/.exec(err.message);
      } else if (/Timed out waiting for async script/.test(err.message)) {
        // Timeout on Firefox
        timeout = /-?[\d\.]*ms/.exec(err.message);
      } else if (/Timed out waiting for an asynchronous script/.test(err.message)) {
        // Timeout on Safari
        timeout = /-?[\d\.]*\ ms/.exec(err.message);
      }
      if (timeout) {
        let errMsg = `Timed out waiting for asynchronous Angular tasks to finish after ` +
            `${timeout}. This may be because the current page is not an Angular ` +
            `application. Please see the FAQ for more details: ` +
            `https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular`;
        if (description.indexOf(' - Locator: ') == 0) {
          errMsg += '\nWhile waiting for element with locator' + description;
        }
        let pendingTimeoutsPromise: Promise<any>;
        if (this.trackOutstandingTimeouts_) {
          pendingTimeoutsPromise = this.executeScriptWithDescription(
              'return window.NG_PENDING_TIMEOUTS',
              'Protractor.waitForAngular() - getting pending timeouts' + description);
        } else {
          pendingTimeoutsPromise = Promise.resolve();
        }
        let pendingHttpsPromise = this.executeScriptWithDescription(
            clientSideScripts.getPendingHttpRequests,
            'Protractor.waitForAngular() - getting pending https' + description,
            this.internalRootEl);

        let arr = await Promise.all([pendingTimeoutsPromise, pendingHttpsPromise]);

        let pendingTimeouts = arr[0] || [];
        let pendingHttps = arr[1] || [];

        let key: string, pendingTasks: string[] = [];
        for (key in pendingTimeouts) {
          if (pendingTimeouts.hasOwnProperty(key)) {
            pendingTasks.push(' - $timeout: ' + pendingTimeouts[key]);
          }
        }
        for (key in pendingHttps) {
          pendingTasks.push(' - $http: ' + pendingHttps[key].url);
        }
        if (pendingTasks.length) {
          errMsg += '. \nThe following tasks were pending:\n';
          errMsg += pendingTasks.join('\n');
        }
        err.message = errMsg;
      }
      throw err;
    }
  }