|
4 | 4 | * Custom command to get the intercepted API aliases stored in Cypress environment variables.
|
5 | 5 | * This command returns the object containing all registered API interception aliases.
|
6 | 6 | *
|
7 |
| - * @returns {Object} An object where keys are the alias names and values are typically the same alias names |
| 7 | + * @returns {Object} An object where keys are in format method-alias(e.g. post-myApiAlias) and values are typically the same alias names |
8 | 8 | * @example
|
9 | 9 | * cy.getInterceptedApiAliases().then((aliases) => {
|
10 | 10 | * Check if a specific alias exists
|
11 |
| - * expect(aliases).to.have.property('myApiAlias'); |
| 11 | + * expect(aliases).to.have.property('post-myApiAlias'); |
12 | 12 | *
|
13 | 13 | * Get the number of registered aliases
|
14 | 14 | * const aliasCount = Object.keys(aliases).length;
|
@@ -53,6 +53,28 @@ Cypress.Commands.add('resetInterceptedApiAliases', () =>
|
53 | 53 | Cypress.env('interceptedAliases', {})
|
54 | 54 | );
|
55 | 55 |
|
| 56 | +/** |
| 57 | + * Sets the request interception flag in Cypress environment. |
| 58 | + * This flag is used to track whether a request matching an intercept pattern was detected. |
| 59 | + * |
| 60 | + * @param {boolean} value - The value to set for the flag (true if request was intercepted, false otherwise) |
| 61 | + * @example |
| 62 | + * // Mark a request as intercepted |
| 63 | + * setRequestIntercepted(true); |
| 64 | + * |
| 65 | + * // Reset the interception flag |
| 66 | + * setRequestIntercepted(false); |
| 67 | + */ |
| 68 | +const setRequestIntercepted = (value) => |
| 69 | + Cypress.env('wasRequestIntercepted', value); |
| 70 | + |
| 71 | +/** |
| 72 | + * Gets the current value of the request interception flag from Cypress environment. |
| 73 | + * This flag indicates whether a request matching an intercept pattern was detected. |
| 74 | + * @returns {boolean} The current value of the request interception flag |
| 75 | + */ |
| 76 | +const getRequestIntercepted = () => Cypress.env('wasRequestIntercepted'); |
| 77 | + |
56 | 78 | /**
|
57 | 79 | * Custom command to intercept API calls and wait for them to complete.
|
58 | 80 | * This command will:
|
@@ -88,21 +110,29 @@ Cypress.Commands.add(
|
88 | 110 |
|
89 | 111 | // Check if this request is already registered
|
90 | 112 | cy.getInterceptedApiAliases().then((interceptedAliasesMap) => {
|
| 113 | + const aliasObjectKey = `${method.toLowerCase()}-${alias}`; |
91 | 114 | // Check if this request is already registered
|
92 |
| - const isAlreadyRegistered = !!interceptedAliasesMap[alias]; |
93 |
| - |
| 115 | + const isAlreadyRegistered = !!interceptedAliasesMap[aliasObjectKey]; |
| 116 | + // Setting wasRequestIntercepted flag to false initially |
| 117 | + setRequestIntercepted(false); |
94 | 118 | // Register the intercept if not already done
|
95 | 119 | if (!isAlreadyRegistered) {
|
96 |
| - cy.intercept(method, urlPattern).as(alias); |
97 |
| - cy.setInterceptedApiAlias(alias); |
| 120 | + cy.intercept(method, urlPattern, () => { |
| 121 | + // Setting wasRequestIntercepted flag to true after request is intercepted |
| 122 | + setRequestIntercepted(true); |
| 123 | + }).as(alias); |
| 124 | + cy.setInterceptedApiAlias(aliasObjectKey, alias); |
98 | 125 | }
|
99 | 126 |
|
100 | 127 | // Execute the function that triggers the API call
|
101 | 128 | triggerFn();
|
102 | 129 |
|
103 | 130 | // Wait for the intercepted request to complete
|
104 |
| - cy.wait(`@${alias}`).then((interception) => { |
105 |
| - onApiResponse(interception); |
| 131 | + cy.then(() => { |
| 132 | + const isRequestIntercepted = getRequestIntercepted(); |
| 133 | + if (isRequestIntercepted) { |
| 134 | + cy.wait(`@${alias}`).then(onApiResponse); |
| 135 | + } |
106 | 136 | });
|
107 | 137 | });
|
108 | 138 | }
|
|
0 commit comments