13

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

Mohsen
  • 64,437
  • 34
  • 159
  • 186

3 Answers3

22

I just found the answer myself.

In config.js:

jasmineNodeOpts: {
    defaultTimeoutInterval: 25000
},
Mohsen
  • 64,437
  • 34
  • 159
  • 186
  • 2
    Hi, I need to override the default preference that you put in config, only for 1 "it"... I try passing a 3th parmeter to the "it" but didnt work. – Facundo Pedrazzini May 27 '16 at 16:07
  • @facundo-pedrazzini as a workaround I would put this "it" into a separate describe and applied the solution described here: http://stackoverflow.com/a/34291826/292787 – Stanislav Feb 07 '17 at 09:36
  • @facundo-pedrazzini I have added an answer to your question: https://stackoverflow.com/a/54045328/558509 – samneric Jan 04 '19 at 20:05
1

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))
Liam
  • 27,717
  • 28
  • 128
  • 190
samneric
  • 3,038
  • 2
  • 28
  • 31
0

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

djangofan
  • 28,471
  • 61
  • 196
  • 289