javascript 量角器 - ScriptTimeoutError:异步脚本超时:20 秒内未收到结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46527912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protractor - ScriptTimeoutError: asynchronous script timeout: result was not received in 20 seconds
提问by Mateusz
I'm new to Protractor and I am trying to run my script.
我是 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();
});
});
But whenever I run it the browser opens up and proceeds to the website and then waits for 20 sec and I get Error: ScriptTimeoutError: asynchronous script timeout: result was not received in 20 seconds. The element is clearly there and can be clicked, however not for the protractor. Am I doing something wrong?
Config file looks like this:
但是每当我运行它时,浏览器都会打开并继续访问该网站,然后等待 20 秒,我收到 Error: 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
}
};
采纳答案by Ernst Zwingli
Given your added error-message (see comment), the cause seems an continuously polling $timeout, which lets a promise unresolved for indefinite time and therefore leads to a asynchronous timeout of protractor (see details here).
鉴于您添加的错误消息(请参阅评论),原因似乎是持续轮询$timeout,这让承诺无限期地无法解决,因此导致量角器的异步超时(请参阅此处的详细信息)。
solution
解决方案
The correct solution is to avoid the use of $timeoutand use $intervalinstead. That way protractor can stay in charge of the ControlFlow, managing your asynchronous tasks. So it's kind of a software bug, not a protractor error.
正确的解决办法是避免使用$timeout而使用$interval代替。这样量角器就可以负责控制流,管理您的异步任务。所以这是一种软件错误,而不是量角器错误。
your error message:
你的错误信息:
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()}*
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()}*
Workaround
解决方法
The not so nice workaround is to switch off the waitForAngular part of Protractor by setting browser.waitForAngularEnabled(false);(either in a beforeEach or directly in the spec.
不太好的解决方法是通过设置browser.waitForAngularEnabled(false);(在 beforeEach 中或直接在规范中设置)关闭量角器的 waitForAngular 部分。
However this also means, taking manually care of the controlFlow within the test specs itself. This requires using a lot of .then()and ExpectedConditions, losing one of the main advantages of Protractor.
然而,这也意味着,在测试规范本身中手动处理 controlFlow。这需要使用大量的.then()和ExpectedConditions,失去了量角器的主要优点之一。
Debug possibilities
调试可能性
Check the descriptions here for potential causes and workarounds.
Specifically try also browser.waitForAngularEnabled(false);to exclude angular-protractor-issues.
查看此处的说明以了解潜在原因和解决方法。还特别尝试browser.waitForAngularEnabled(false);排除角度量角器问题。
If you can't find cause, it could be a timing issue (unlikely, but worth being examined at that point).
如果找不到原因,则可能是时间问题(不太可能,但此时值得检查)。
You can try to add log-messages to narrow down the effective order of execution:
您可以尝试添加日志消息以缩小有效执行顺序:
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();
});
});
Or you put the actions in the same test case, using then()to execute them synchronously:
或者您将操作放在同一个测试用例中,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();
});
});
Open Homepage within onPrepare
在里面打开主页 onPrepare
As one nice side remark: If you always load the Homepage first, just put it into an onPrepare-part of your conf.js and it will always be executed once before your tests start:
顺便说一句:如果您总是先加载主页,只需将其放入 conf.js 的 onPrepare 部分,它将始终在您的测试开始之前执行一次:
onPrepare: function () {
browser.driver.manage().window().maximize();
browser.get('url');
},
回答by Beyar
I had a similar issue, I solved it by turning on ignore sync
我有一个类似的问题,我通过打开忽略同步来解决它
browser.ignoreSynchronization = true

