|
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:
|
@@ -34,24 +87,23 @@ Cypress.Commands.add(
|
34 | 87 | /* ======================================================= */
|
35 | 88 |
|
36 | 89 | // Check if this request is already registered
|
37 |
| - const isAlreadyRegistered = !!Cypress.env('interceptedAliases')[alias]; |
38 |
| - |
39 |
| - // Register the intercept if not already done |
40 |
| - if (!isAlreadyRegistered) { |
41 |
| - cy.intercept(method, urlPattern).as(alias); |
| 90 | + cy.getInterceptedApiAliases().then((interceptedAliasesMap) => { |
| 91 | + // Check if this request is already registered |
| 92 | + const isAlreadyRegistered = !!interceptedAliasesMap[alias]; |
42 | 93 |
|
43 |
| - // Store the alias in the tracking object |
44 |
| - const interceptedAliases = Cypress.env('interceptedAliases'); |
45 |
| - interceptedAliases[alias] = alias; |
46 |
| - Cypress.env('interceptedAliases', interceptedAliases); |
47 |
| - } |
| 94 | + // Register the intercept if not already done |
| 95 | + if (!isAlreadyRegistered) { |
| 96 | + cy.intercept(method, urlPattern).as(alias); |
| 97 | + cy.setInterceptedApiAlias(alias); |
| 98 | + } |
48 | 99 |
|
49 |
| - // Execute the function that triggers the API call |
50 |
| - triggerFn(); |
| 100 | + // Execute the function that triggers the API call |
| 101 | + triggerFn(); |
51 | 102 |
|
52 |
| - // Wait for the intercepted request to complete |
53 |
| - cy.wait(`@${alias}`).then((interception) => { |
54 |
| - onApiResponse(interception); |
| 103 | + // Wait for the intercepted request to complete |
| 104 | + cy.wait(`@${alias}`).then((interception) => { |
| 105 | + onApiResponse(interception); |
| 106 | + }); |
55 | 107 | });
|
56 | 108 | }
|
57 | 109 | );
|
0 commit comments