My application is making it's calls to backend from the same url, but with different payloads. How can I test that those requests (that differ only in payload) are made with cypress when url is always the same? Here is what Ido
const requests = ProductList.initialRequestList;
const aliases = [];
requests.forEach((request, index) => {
cy.intercept('POST', `${baseUrl}${request.endpoint}`, (req) => {
if (req.body.query && req.body.query === request.payload.query) {
aliases.push(`@${request.payload.query}`);
}
}).as(request.payload.query);
});
cy.visit('/someurl');
cy.wait(aliases)
.then(() => {
aliases.forEach((alias, index) => {
// aliases ok/different here
console.log(alias);
cy.get(alias)
.then((interception: cy.intercept.Interception) => {
// here not ok all the same
console.log(interception.response.body.message);
});
});
});
Initially I posted the Cypress docs on dynamic alias, but it turns out they are not very dynamic.
A reproducible test
Here's a Cypress test to replicate the problem, based on the code you added above.
using a
fireFetches()function to send the requests instead ofcy.visit()stubbing the
cy.intercept()response since I have no API endpoint to hitadded
middleware: trueto allow all intercepts (a, b & c) to check the interception against the request data.only the request that matches
body.query === request.payload.querywill respondYour test - one intercept, checking at the cy.wait() end
For you the API server will provide the response, you don't need to mock the response and therefore you only need one `cy.intercept'
Waiting only once and expecting `cy.get(${request}.all)` to work
Summarizing the comments below, Artoo proposes to use
but the problem is
cy.get(${request}.all)doesn't actually wait for all responses, it only gets whatever has responded so far in the test.Since you are relying on actual responses from the API server (not mocking), the results will be asynchronous and not predicatble.
So, you must
cy.wait(${request})n-times as shown by Filip Hiric in his video Intercepting multiple requests