Mohsen
Mohsen

Reputation: 65785

How to override protractor's default timeout?

How can I override default timeout(defaultTimeoutInterval) for it and describe methods in Protractor? It's defaulted to 2500ms.

Upvotes: 13

Views: 11703

Answers (3)

djangofan
djangofan

Reputation: 29669

It feels like a hack, but the only way I could get it to work was by putting this code in one of my steps.js files:

const { setDefaultTimeout } = require('cucumber');
setDefaultTimeout(28 * 1000);

I tried jasmineNodeOpts and cucumberOpts, in my conf.js, but they didn't work for me.

Screenshot of what I did:

enter image description here

Upvotes: 0

samneric
samneric

Reputation: 3218

You can override the default timeout in a specific it test using these two functions to override then restore the default: (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);
}

EDIT

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);
}

use like this:

itTO('should run longer than protractors default', async () => {
        await delay(14000);
}, 15000);

const delay = ms => new Promise(res => setTimeout(res, ms))

Upvotes: 1

Mohsen
Mohsen

Reputation: 65785

I just found the answer myself.

In config.js:

jasmineNodeOpts: {
    defaultTimeoutInterval: 25000
},

Upvotes: 22

Related Questions