Dynamically setting the environment under test
Some of the tests I execute happen pre-login. The way I was previously setting which URL to visit was via a custom command. This required that I have multiple copies of the same test but per-environment. Using the intercept as an example, I was able to change this to only needing one test.
To do this, I ensure that getConstants is being called via the header:
import { getConstants } from '../../../fixtures/getConstants'
which has this
“baseURL”: “https://[URL]-app-[DOMAIN]”, “apiURL”: “https://[URL]-app-api-[DOMAIN]”
Then add the following in the beforeEach (or within the actual scenario if not recognized in the beforeEach):
const constants = getConstants(); const baseURL = constants.baseURL; cy.visit(`${baseURL}`)
So now the system will navigate to whatever host is specified as the baseURL
The reference to the API can be used to intercept the call and make the script wait for a response before continuing like
const constants = getConstants() const apiurl = constants.apiURL
cy.get('[data-testid="review-and-save-save-button"]').click({force: true}) cy.intercept('POST',`${apiurl}`).as('submit') cy.wait('@submit', {timeout:8500})
If the response from the API is received before the timeout (8500) the script should continue running at that point