From eb9587ffde0254e8a55ecd7b1a10ec70e301560f Mon Sep 17 00:00:00 2001 From: Walmyr Date: Fri, 19 Jan 2024 15:15:49 +0100 Subject: [PATCH] Better name `cy.intercept` aliases --- cypress/e2e/playground.cy.js | 8 ++++---- src/index.html | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cypress/e2e/playground.cy.js b/cypress/e2e/playground.cy.js index 7d0718b..fb4ad10 100644 --- a/cypress/e2e/playground.cy.js +++ b/cypress/e2e/playground.cy.js @@ -117,9 +117,9 @@ describe('Cypress Playground', () => { 'GET', 'https://jsonplaceholder.typicode.com/todos/1', { statusCode: 500 } - ).as('getTodo') + ).as('serverFailure') cy.contains('#intercept button', 'Get TODO').click() - cy.wait('@getTodo') + cy.wait('@serverFailure') .its('response.statusCode') .should('be.equal', 500) cy.contains( @@ -133,9 +133,9 @@ describe('Cypress Playground', () => { 'GET', 'https://jsonplaceholder.typicode.com/todos/1', { forceNetworkError: true } - ).as('getTodo') + ).as('networkError') cy.contains('#intercept button', 'Get TODO').click() - cy.wait('@getTodo') + cy.wait('@networkError') cy.contains( '#intercept .error span', 'Oops, something went wrong. Check your internet connection, refresh the page, and try again.' diff --git a/src/index.html b/src/index.html index ab5f1e1..f5e87dd 100644 --- a/src/index.html +++ b/src/index.html @@ -148,15 +148,15 @@


cy.wait('@getTodo')

With the cy.intercept() command, you could even simulate an API failure.

-

For example: cy.intercept('GET', 'https://api.example.com/todos/1', { statusCode: 500 }).as('getTodo').

+

For example: cy.intercept('GET', 'https://api.example.com/todos/1', { statusCode: 500 }).as('serverFailure').

Then, you could assert that it has really failed.

-

For example: cy.wait('@getTodo').its('response.statusCode').should('be.equal', 500).

+

For example: cy.wait('@serverFailure').its('response.statusCode').should('be.equal', 500).

And maybe, even assert that certain fallback element has been displayed.

For example: cy.contains('.error', 'Oops, something went wrong.').should('be.visible').

Finally, with the cy.intercept()command, you could force a network error, to test how your web app would behave if there were no internet available, for instance.

-

For example: cy.intercept('GET', 'https://api.example.com/todos/1', { forceNetworkError: true }).as('getTodo').

+

For example: cy.intercept('GET', 'https://api.example.com/todos/1', { forceNetworkError: true }).as('networkError').

Then, you could wait for such a failure.

-

For example: cy.wait('@getTodo').

+

For example: cy.wait('@networkError').

And then, you could even assert that certain fallback element has been displayed.

For example: cy.contains('.error', "Oops, it seems you don't have internet connection.").should('be.visible').