|
1 | 1 | /* eslint-disable no-undef */
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * Custom command to get the intercepted API aliases stored in Cypress environment variables. |
| 5 | + * This command returns the object containing all registered API interception aliases. |
| 6 | + * |
| 7 | + * @returns {Object} An object where keys are the alias names and values are typically the same alias names |
| 8 | + * @example |
| 9 | + * cy.getInterceptedApiAliases().then((aliases) => { |
| 10 | + * Check if a specific alias exists |
| 11 | + * expect(aliases).to.have.property('myApiAlias'); |
| 12 | + * |
| 13 | + * Get the number of registered aliases |
| 14 | + * const aliasCount = Object.keys(aliases).length; |
| 15 | + * }); |
| 16 | + */ |
| 17 | +Cypress.Commands.add('getInterceptedApiAliases', () => |
| 18 | + Cypress.env('interceptedAliases') |
| 19 | +); |
| 20 | + |
| 21 | +/** |
| 22 | + * Custom command to set an intercepted API alias in the Cypress environment variables. |
| 23 | + * This command adds an alias in the intercepted aliases tracking object. |
| 24 | + * |
| 25 | + * @param {string} aliasKey - The key/name of the alias to set |
| 26 | + * @param {string} [aliasValue=aliasKey] - The value to store for the alias (defaults to the same as the key) |
| 27 | + * @example |
| 28 | + * Set a new alias |
| 29 | + * cy.setInterceptedApiAlias('getUsersApi'); |
| 30 | + * |
| 31 | + * Set an alias with a custom value |
| 32 | + * cy.setInterceptedApiAlias('getUsersApi', 'customValue'); |
| 33 | + */ |
| 34 | +Cypress.Commands.add( |
| 35 | + 'setInterceptedApiAlias', |
| 36 | + (aliasKey, aliasValue = aliasKey) => { |
| 37 | + cy.getInterceptedApiAliases().then((interceptedAliasesMap) => { |
| 38 | + interceptedAliasesMap[aliasKey] = aliasValue; |
| 39 | + Cypress.env('interceptedAliases', interceptedAliasesMap); |
| 40 | + }); |
| 41 | + } |
| 42 | +); |
| 43 | + |
| 44 | +/** |
| 45 | + * Custom command to reset all intercepted API aliases stored in Cypress environment variables. |
| 46 | + * This command clears the tracking object by setting it to an empty object. |
| 47 | + * Useful for cleaning up between tests or test suites. |
| 48 | + * @example |
| 49 | + * Reset all intercepted API aliases |
| 50 | + * cy.resetInterceptedApiAliases(); |
| 51 | + */ |
| 52 | +Cypress.Commands.add('resetInterceptedApiAliases', () => |
| 53 | + Cypress.env('interceptedAliases', {}) |
| 54 | +); |
| 55 | + |
3 | 56 | /**
|
4 | 57 | * Custom command to intercept API calls and wait for them to complete.
|
5 | 58 | * This command will:
|
|
12 | 65 | * @param {string} options.method - HTTP method (default: 'POST')
|
13 | 66 | * @param {string|RegExp} options.urlPattern - URL pattern to intercept
|
14 | 67 | * @param {Function} options.triggerFn - Function that triggers the API call
|
| 68 | + * @param {Function} [options.onApiResponse] - Optional callback function that receives the interception object after the API call completes. |
| 69 | + * Use this to perform assertions on the response, extract data, or perform additional actions based on the API result. |
| 70 | + * Default is a no-op function. e.g. { onApiResponse: (interception) => { expect(interception.response.statusCode).to.equal(200); } } |
15 | 71 | */
|
16 | 72 | Cypress.Commands.add(
|
17 | 73 | 'interceptApi',
|
18 |
| - ({ alias, method = 'POST', urlPattern, triggerFn }) => { |
| 74 | + ({ |
| 75 | + alias, |
| 76 | + method = 'POST', |
| 77 | + urlPattern, |
| 78 | + triggerFn, |
| 79 | + onApiResponse = () => { |
| 80 | + /* default implementation */ |
| 81 | + }, |
| 82 | + }) => { |
19 | 83 | /* ===== TODO: Remove this block once interceptApi command becomes stable ===== */
|
20 | 84 | const envVars = Cypress.env();
|
21 | 85 | cy.log('Cypress Environment Variables:');
|
22 | 86 | cy.log(JSON.stringify(envVars, null, 2));
|
23 | 87 | /* ======================================================= */
|
24 | 88 |
|
25 | 89 | // Check if this request is already registered
|
26 |
| - const isAlreadyRegistered = !!Cypress.env('interceptedAliases')[alias]; |
27 |
| - |
28 |
| - // Register the intercept if not already done |
29 |
| - if (!isAlreadyRegistered) { |
30 |
| - cy.intercept(method, urlPattern).as(alias); |
| 90 | + cy.getInterceptedApiAliases().then((interceptedAliasesMap) => { |
| 91 | + // Check if this request is already registered |
| 92 | + const isAlreadyRegistered = !!interceptedAliasesMap[alias]; |
31 | 93 |
|
32 |
| - // Store the alias in the tracking object |
33 |
| - const interceptedAliases = Cypress.env('interceptedAliases'); |
34 |
| - interceptedAliases[alias] = alias; |
35 |
| - Cypress.env('interceptedAliases', interceptedAliases); |
36 |
| - } |
| 94 | + // Register the intercept if not already done |
| 95 | + if (!isAlreadyRegistered) { |
| 96 | + cy.intercept(method, urlPattern).as(alias); |
| 97 | + cy.setInterceptedApiAlias(alias); |
| 98 | + } |
37 | 99 |
|
38 |
| - // Execute the function that triggers the API call |
39 |
| - triggerFn(); |
| 100 | + // Execute the function that triggers the API call |
| 101 | + triggerFn(); |
40 | 102 |
|
41 |
| - // Wait for the intercepted request to complete |
42 |
| - cy.wait(`@${alias}`); |
| 103 | + // Wait for the intercepted request to complete |
| 104 | + cy.wait(`@${alias}`).then((interception) => { |
| 105 | + onApiResponse(interception); |
| 106 | + }); |
| 107 | + }); |
43 | 108 | }
|
44 | 109 | );
|
0 commit comments