From c0ce2c460b9e2cf023070e05f79388c2a86bafd8 Mon Sep 17 00:00:00 2001 From: Jeremy Tymes Date: Sun, 2 Dec 2018 22:49:48 -0500 Subject: [PATCH] Allow separate request and response timeout overrides in cy.wait (#2829) * Allow overriding the request and response timeouts for XHR. * Add interface to the XHR wait overloads to take in the request and response timeouts. --- cli/types/index.d.ts | 24 +++++++++- .../driver/src/cy/commands/waiting.coffee | 6 ++- .../integration/commands/waiting_spec.coffee | 46 +++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/cli/types/index.d.ts b/cli/types/index.d.ts index 1d2c9ac5a2ca..e29251778c8a 100644 --- a/cli/types/index.d.ts +++ b/cli/types/index.d.ts @@ -1514,13 +1514,13 @@ declare namespace Cypress { * * @see https://on.cypress.io/wait */ - wait(alias: string, options?: Partial): Chainable + wait(alias: string, options?: Partial): Chainable /** * Wait for list of XHR requests to complete. * * @see https://on.cypress.io/wait */ - wait(alias: string[], options?: Partial): Chainable + wait(alias: string[], options?: Partial): Chainable /** * Get the window object of the page that is currently active. @@ -1657,6 +1657,26 @@ declare namespace Cypress { timeout: number } + /** + * Options that control how long the Test Runner waits for an XHR request and response to succeed + */ + interface TimeoutableXHR { + /** + * Time to wait for the request (ms) + * + * @default {@link Timeoutable#timeout} + * @see https://docs.cypress.io/guides/references/configuration.html#Timeouts + */ + requestTimeout: number, + /** + * Time to wait for the response (ms) + * + * @default {@link Timeoutable#timeout} + * @see https://docs.cypress.io/guides/references/configuration.html#Timeouts + */ + responseTimeout: number + } + /** * Options to force an event, skipping Actionability check * @see https://docs.cypress.io/guides/core-concepts/interacting-with-elements.html#Actionability diff --git a/packages/driver/src/cy/commands/waiting.coffee b/packages/driver/src/cy/commands/waiting.coffee index 3f89beb7671b..0fcdbf789a73 100644 --- a/packages/driver/src/cy/commands/waiting.coffee +++ b/packages/driver/src/cy/commands/waiting.coffee @@ -101,17 +101,19 @@ module.exports = (Commands, Cypress, cy, state, config) -> ## but slice out the error since we may set ## the error related to a previous xhr timeout = options.timeout + requestTimeout = options.requestTimeout ? timeout + responseTimeout = options.responseTimeout ? timeout [ index, num ] = getNumRequests(state, alias) waitForRequest = -> options = _.omit(options, "_runnableTimeout") - options.timeout = timeout ? Cypress.config("requestTimeout") + options.timeout = requestTimeout ? Cypress.config("requestTimeout") checkForXhr(alias, "request", index, num, options) waitForResponse = -> options = _.omit(options, "_runnableTimeout") - options.timeout = timeout ? Cypress.config("responseTimeout") + options.timeout = responseTimeout ? Cypress.config("responseTimeout") checkForXhr(alias, "response", index, num, options) ## if we were only waiting for the request diff --git a/packages/driver/test/cypress/integration/commands/waiting_spec.coffee b/packages/driver/test/cypress/integration/commands/waiting_spec.coffee index c681a1a1197f..95d3288bfd35 100644 --- a/packages/driver/test/cypress/integration/commands/waiting_spec.coffee +++ b/packages/driver/test/cypress/integration/commands/waiting_spec.coffee @@ -110,6 +110,17 @@ describe "src/cy/commands/waiting", -> .wait("@fetch").then -> expect(cy.timeout()).to.eq 199 + it "waits for requestTimeout override", (done) -> + cy.on "command:retry", (options) -> + expect(options.type).to.eq("request") + expect(options.timeout).to.eq(199) + done() + + cy + .server() + .route("GET", "*", {}).as("fetch") + .wait("@fetch", {requestTimeout: 199}) + it "waits for responseTimeout", (done) -> Cypress.config("responseTimeout", 299) @@ -125,6 +136,41 @@ describe "src/cy/commands/waiting", -> null .wait("@fetch") + it "waits for responseTimeout override", (done) -> + cy.on "command:retry", (options) -> + expect(options.type).to.eq("response") + expect(options.timeout).to.eq(299) + done() + + cy + .server({delay: 100}) + .route("GET", "*", {}).as("fetch") + .window().then (win) -> + win.$.get("/foo") + null + .wait("@fetch", {responseTimeout: 299}) + + it "waits for requestTimeout and responseTimeout override", (done) -> + retryCount = 0 + cy.on "command:retry", (options) -> + retryCount++ + if retryCount == 1 + expect(options.type).to.eq("request") + expect(options.timeout).to.eq(100) + + ## trigger request to move onto response timeout verification + win = cy.state("window") + win.$.get("/foo") + else if retryCount == 2 + expect(options.type).to.eq("response") + expect(options.timeout).to.eq(299) + done() + + cy + .server({delay: 100}) + .route("GET", "*", {}).as("fetch") + .wait("@fetch", {requestTimeout: 100, responseTimeout: 299}) + ## https://github.com/cypress-io/cypress/issues/369 it "does not mutate 2nd route methods when using shorthand route", -> cy