Skip to content

Commit a1c9d49

Browse files
Add response handler callback to interceptApi command
1 parent 0fa3a7a commit a1c9d49

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

cypress/support/commands/api_commands.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@
1212
* @param {string} options.method - HTTP method (default: 'POST')
1313
* @param {string|RegExp} options.urlPattern - URL pattern to intercept
1414
* @param {Function} options.triggerFn - Function that triggers the API call
15+
* @param {Function} [options.onApiResponse] - Optional callback function that receives the interception object after the API call completes.
16+
* Use this to perform assertions on the response, extract data, or perform additional actions based on the API result.
17+
* Default is a no-op function. e.g. { onApiResponse: (interception) => { expect(interception.response.statusCode).to.equal(200); } }
1518
*/
1619
Cypress.Commands.add(
1720
'interceptApi',
18-
({ alias, method = 'POST', urlPattern, triggerFn }) => {
21+
({
22+
alias,
23+
method = 'POST',
24+
urlPattern,
25+
triggerFn,
26+
onApiResponse = () => {
27+
/* default implementation */
28+
},
29+
}) => {
1930
/* ===== TODO: Remove this block once interceptApi command becomes stable ===== */
2031
const envVars = Cypress.env();
2132
cy.log('Cypress Environment Variables:');
@@ -39,6 +50,8 @@ Cypress.Commands.add(
3950
triggerFn();
4051

4152
// Wait for the intercepted request to complete
42-
cy.wait(`@${alias}`);
53+
cy.wait(`@${alias}`).then((interception) => {
54+
onApiResponse(interception);
55+
});
4356
}
4457
);

0 commit comments

Comments
 (0)