Intercepting API calls in Cypress
Rather than put an arbitrary wait after a submit, you can craft an intercept like as follows:
cy.contains('SUBMIT').click({ force: true }) cy.intercept('POST', 'https://[URL]/graphql').as('submit') cy.wait('@submit')
What this does is force the code to wait until the call is made before continuing
If your scripts are set up to allow to test multiple environments (like mine is) by specifying the environment in the cypress.json, you can have the API follow that environment by doing this:
Under /cypress/fixtures, add a line below the baseURL to match your environment:
“baseURL”: “https://[URL]-app-staging.herokuapp.com”, “apiURL”: “https://[URL]-api-staging.herokuapp.com/graphql”
Add the following to each and every scenario (within the it() ):
it(‘1-Does something’, () => { const constants = getConstants(); const url = constants.apiURL; <- etc -> })
Then call this within the scripts:
//CLICK SAVE BUTTON cy.get('[data-testid="review-and-save-save-button"]').click() cy.intercept('POST',`${url}`).as('submit') cy.wait('@submit').wait(8500)