在函数内部更改量角器默认超时 [英] Changing protractor default timeout inside function
问题描述
我有一个函数要在我的一些量角器测试中调用,它执行的一些任务需要超过量角器默认超时(似乎是 60 秒)
I have a function to be called in some of my protractor tests which does some tasks that take more than the protractor default timeout (which seems to be 60 seconds)
我了解到您应该能够使用jasmine.DEFAULT_TIMEOUT_INTERVAL"更改默认超时,但是使用以下代码,超时仍然发生在我设置的 4 分钟之前.由于我想在以后重用这个测试部分,我不能简单地将它作为参数添加到测试函数中.
I've read that you should be able to change the default timeout with "jasmine.DEFAULT_TIMEOUT_INTERVAL", however with the following code, the timeout still happens before the 4 minutes I have set up. Since I want to reuse this test part in the future, I cannot simply add it as a parameter to the test function.
这是示例代码,谁能告诉我我做错了什么?
Here is the sample code, can anyone tell me what I'm doing wrong?
describe('reset data', function() {
it('should reset data', function() {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 240000;
browser.ignoreSynchronization = true;
// ... test code here
});
});
在大约 60 秒后测试失败后,我收到以下错误:
I get the following error, after the test fails after roughly 60 seconds:
错误:超时 - 未在超时内调用异步回调由 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定.
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
推荐答案
我创建了两个函数来覆盖,然后恢复默认的量角器超时:(仅在 Chrome 中测试过)
I have created two functions to override, then restore the default protractor timeouts: (Only tested in Chrome)
import { browser } from 'protractor';
export function DefaultTimeoutOverride(milliseconds: number) {
browser.driver.manage().timeouts().setScriptTimeout(milliseconds);
}
export function DefaultTimeoutRestore() {
browser.driver.manage().timeouts().setScriptTimeout(browser.allScriptsTimeout);
}
编辑
我现在创建了一个辅助函数 ('itTO'),它包装了 Jasmine 的 'it' 语句并自动应用超时 :)
I have now created a helper function ('itTO') that wraps Jasmine's 'it' statement and applies the timeout automatically :)
import { browser } from 'protractor';
export function itTO(expectation: string, assertion: (done: DoneFn) => void, timeout: number): void {
it(expectation, AssertionWithTimeout(assertion, timeout), timeout);
}
function AssertionWithTimeout<T extends Function>(fn: T, timeout: number): T {
return <any>function(...args) {
DefaultTimeoutOverride(timeout);
const response = fn(...args);
DefaultTimeoutRestore();
return response;
};
}
function DefaultTimeoutOverride(milliseconds: number) {
browser.driver.manage().timeouts().setScriptTimeout(milliseconds);
}
function DefaultTimeoutRestore() {
browser.driver.manage().timeouts().setScriptTimeout(browser.allScriptsTimeout);
}
这样使用:
itTO('should run longer than protractors default', async () => {
await delay(14000);
}, 15000);
const delay = ms => new Promise(res => setTimeout(res, ms))
这篇关于在函数内部更改量角器默认超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!