更改函数内的量角器默认超时 [英] Changing protractor default timeout inside function

查看:47
本文介绍了更改函数内的量角器默认超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以在我的一些量角器测试中调用,它执行一些比量角器默认超时(似乎是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆