【问题标题】:Protractor - ScriptTimeoutError: asynchronous script timeout: result was not received in 20 seconds量角器 - ScriptTimeoutError:异步脚本超时:20 秒内未收到结果
【发布时间】:2018-03-13 15:59:19
【问题描述】:

我是 Protractor 的新手,我正在尝试运行我的脚本。

describe('Navigator homepage', function() {
it('should proceed to login', function() {
browser.get('url');
})
it('Clicks the proceed button',function() {
const proceedButton = element(by.id('auth-login-page-button'));

proceedButton.click();
});
});

但每当我运行它时,浏览器就会打开并进入网站,然后等待 20 秒,然后我得到错误:ScriptTimeoutError: asynchronous script timeout: result was not received in 20 seconds。该元素显然存在并且可以单击,但不能用于量角器。难道我做错了什么? 配置文件如下所示:

// An example configuration file.
exports.config = {
  directConnect: true,

  // Capabilities to be passed to the webdriver instance.
  capabilities: {
    'browserName': 'chrome'
  },

  // Framework to use. Jasmine is recommended.
  framework: 'jasmine',

  // Spec patterns are relative to the current working directory when
  // protractor is called.
  specs: ['login_spec.js'],

  allScriptsTimeout: 20000,
  getPageTimeout: 15000,
  framework: 'jasmine',
  jasmineNodeOpts: {
    isVerbose: false,
    showColors: true,
    includeStackTrace: false,
    defaultTimeoutInterval: 40000
  }
  };

【问题讨论】:

    标签: javascript automation jasmine protractor


    【解决方案1】:

    鉴于您添加的错误消息(请参阅评论),原因似乎是持续轮询 $timeout,这使得承诺无限期未解决,因此导致量角器异步超时 (see details here)。

    解决方案

    正确的解决方案是避免使用$timeout,而使用$interval。这样量角器就可以继续负责控制流,管理你的异步任务。所以这是一种软件错误,而不是量角器错误。

    您的错误信息:

    Failed: Timed out waiting for asynchronous Angular tasks to finish after 20 seconds. 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/timeo‌​uts.md#waiting-for-a‌​ngular While waiting for element with locator - Locator: By(css selector, md-raised md-primary md-button md-ink-ripple). *The following tasks were pending: - $timeout: function (){return _this.getVersion()}*

    解决方法

    不太好的解决方法是通过设置browser.waitForAngularEnabled(false);(在 beforeEach 中或直接在规范中)来关闭 Protractor 的 waitForAngular 部分。

    但这也意味着,在测试规范本身中手动处理 controlFlow。这需要大量使用.then()ExpectedConditions,失去了Protractor的主要优势之一。

    调试可能性

    检查描述here for potential causes and workarounds。 特别尝试browser.waitForAngularEnabled(false); 排除角度量角器问题。

    如果您找不到原因,则可能是时间问题(不太可能,但此时值得检查)。

    您可以尝试添加日志消息以缩小有效执行顺序:

    describe('Navigator homepage', function() {
        it('should proceed to login', function() {
        browser.get('url').then(function(){
            console.log("Page is fully loaded");
            });
        });
        it('Clicks the proceed button',function() {
            console.log("Start 2nd Test");
            const proceedButton = element(by.id('auth-login-page-button'));
            proceedButton.click();
        });
    });
    

    或者你把动作放在同一个测试用例中,使用then()同步执行它们:

    describe('Navigator homepage', function() {
        it('should proceed to login', function() {
        browser.get('url').then(function(){
            const proceedButton = element(by.id('auth-login-page-button'));
            proceedButton.click();
        });
    });
    

    onPrepare内打开主页

    顺便说一句:如果您总是先加载主页,只需将其放入 conf.js 的 onPrepare-part 中,它将始终在您的测试开始前执行一次:

    onPrepare: function () {
        browser.driver.manage().window().maximize();
        browser.get('url');
    },
    

    【讨论】:

    • 谢谢,尝试了所有这些选项,当我输入waitForAngular = false 时,它给了我错误Failed: Timed out waiting for asynchronous Angular tasks to finish after 20 seconds. 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 While waiting for element with locator - Locator: By(css selector, md-raised md-primary md-button md-ink-ripple). The following tasks were pending: - $timeout: function (){return _this.getVersion()}
    • 看来,您的答案出现在您的错误消息中:The following tasks were pending: - $timeout: function (){return _this.getVersion()} 所以有一个 $timeout 集,没有解决。在此处查找更多信息:github.com/angular/protractor/issues/169 和此处:github.com/angular/protractor/blob/master/docs/… 您(或您的开发人员)能否在您正在测试的页面上将 $timeout 替换为 $interval ... 或者只是注意 this.getVersion() 最终得到解决?
    • 哎呀...我看到我为waitForAngular 提供了错误的输入。正确的语法:browser.waitForAngularEnabled(false); 我将为此编辑我的答案。
    • 更改为browser.waitForAngularEnabled(false);,现在我在网页完全加载之前得到Failed: No element found using locator: By(css selector, *[id="auth-login-page-button"])
    • 是的,当您设置 browser.waitForAngularEnabled(false); 时,您将失去 Protractor 的主要优势之一:您现在需要自己管理您的 controlFlow,因为 Protractor 不会等待 Angular-promise 得到解决。如果您选择browser.get('url').then(function(){ const proceedButton = element(by.id('auth-login-page-button')); proceedButton.click(); });,它将适用于您的情况。但是如果您需要继续使用browser.waitForAngularEnabled(false);,您需要知道如何使用.then()ExpectedConditions。更好的方法是由您的开发人员替换 $timeout
    【解决方案2】:

    我遇到了类似的问题,我通过打开忽略同步解决了它

    browser.ignoreSynchronization = true
    

    【讨论】:

    • 这为我解决了这个问题!
    • 太棒了!经过 3 天尝试不同的解决方案后,这一解决方案奏效了。在 conf.ts 中分享我的 onPrepare:onPrepare: () => { let globals = require('protractor');让浏览器= globals.browser; browser.manage().window().maximize(); browser.manage().timeouts().implicitlyWait(10000); browser.ignoreSynchronization = true; }
    猜你喜欢
    • 1970-01-01
    • 2019-11-11
    • 2019-09-14
    • 1970-01-01
    • 2019-10-21
    • 2019-07-09
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    相关资源
    最近更新 更多