Skip to content

Commit 7cefa15

Browse files
authored
Merge pull request #9607 from asirvadAbrahamVarghese/enhance-intercept-api-command
Enhance intercept api command
2 parents e8d8dec + 3a3205c commit 7cefa15

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

cypress/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ManageIQ implements the following cypress extensions:
5959

6060
##### api_commands
6161

62-
* `cy.interceptApi({ alias, method = 'POST', urlPattern, triggerFn, onApiResponse })` - intercepts API calls and waits for them to complete. This command will: 1) Register an intercept for the given alias and URL pattern if not already registered, 2) Execute the trigger function that makes the API call, 3) Wait for the intercepted request to complete. `alias` is the string for a unique alias for this interception. `method` is the string for the HTTP method (default: 'POST'). `urlPattern` is the string or RegExp for the URL pattern to intercept. `triggerFn` is the function that triggers the API call. `onApiResponse` is an optional callback function that receives the interception object after the API call completes. Use this to perform assertions on the response, extract data, or perform additional actions based on the API result. Default is a no-op function. e.g. `cy.interceptApi({ alias: 'getUsers', method: 'GET', urlPattern: '/api/users', triggerFn: () => cy.get('#load-users').click(), onApiResponse: (interception) => { expect(interception.response.statusCode).to.equal(200); } });`
62+
* `cy.interceptApi({ alias, method = 'POST', urlPattern, triggerFn, onApiResponse })` - intercepts API calls and waits for them to complete. This command will: 1) Register an intercept(in method-alias format e.g. post-myApiAlias) for the given alias & URL pattern if not already registered, 2) Execute the trigger function that makes the API call, 3) Wait for the intercepted request to complete. `alias` is the string for a unique alias for this interception. `method` is the string for the HTTP method (default: 'POST'). `urlPattern` is the string or RegExp for the URL pattern to intercept. `triggerFn` is the function that triggers the API call. `onApiResponse` is an optional callback function that receives the interception object after the API call completes. Use this to perform assertions on the response, extract data, or perform additional actions based on the API result. Default is a no-op function. e.g. `cy.interceptApi({ alias: 'getUsers', method: 'GET', urlPattern: '/api/users', triggerFn: () => cy.get('#load-users').click(), onApiResponse: (interception) => { expect(interception.response.statusCode).to.equal(200); } });`
6363
* `cy.getInterceptedApiAliases()` - returns the intercepted API aliases stored in Cypress environment variables.
6464
* `cy.setInterceptedApiAlias(aliasKey, aliasValue)` - sets an intercepted API alias in the Cypress environment variables. `aliasKey` is the string for the key/name of the alias to set. `aliasValue` is an optional string for the value to store for the alias (defaults to the same as the key). e.g. `cy.setInterceptedApiAlias('getUsersApi');`, `cy.setInterceptedApiAlias('getUsersApi', 'customValue');`
6565
* `cy.resetInterceptedApiAliases()` - resets the intercepted API aliases stored in Cypress environment variables.

cypress/e2e/ui/validate-intercept-api-command.cy.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('Validate intercept command', () => {
2020
}).then(() => {
2121
// verifies that the alias is set and the request is intercepted & awaited
2222
cy.getInterceptedApiAliases().then((interceptedAliasesObject) => {
23-
expect(interceptedAliasesObject).to.have.property('treeSelectApi');
23+
expect(interceptedAliasesObject).to.have.property('post-treeSelectApi');
2424
});
2525
});
2626
});
@@ -41,8 +41,8 @@ describe('Validate intercept command', () => {
4141
// verifies that both the aliases are set and the request is intercepted & awaited
4242
cy.getInterceptedApiAliases().then((interceptedAliasesObject) => {
4343
expect(interceptedAliasesObject).to.include.all.keys(
44-
'accordionSelectApi',
45-
'treeSelectApi'
44+
'post-accordionSelectApi',
45+
'post-treeSelectApi'
4646
);
4747
});
4848
});

cypress/support/commands/api_commands.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* Custom command to get the intercepted API aliases stored in Cypress environment variables.
55
* This command returns the object containing all registered API interception aliases.
66
*
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
88
* @example
99
* cy.getInterceptedApiAliases().then((aliases) => {
1010
* Check if a specific alias exists
11-
* expect(aliases).to.have.property('myApiAlias');
11+
* expect(aliases).to.have.property('post-myApiAlias');
1212
*
1313
* Get the number of registered aliases
1414
* const aliasCount = Object.keys(aliases).length;
@@ -53,6 +53,28 @@ Cypress.Commands.add('resetInterceptedApiAliases', () =>
5353
Cypress.env('interceptedAliases', {})
5454
);
5555

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+
5678
/**
5779
* Custom command to intercept API calls and wait for them to complete.
5880
* This command will:
@@ -88,21 +110,29 @@ Cypress.Commands.add(
88110

89111
// Check if this request is already registered
90112
cy.getInterceptedApiAliases().then((interceptedAliasesMap) => {
113+
const aliasObjectKey = `${method.toLowerCase()}-${alias}`;
91114
// 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);
94118
// Register the intercept if not already done
95119
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);
98125
}
99126

100127
// Execute the function that triggers the API call
101128
triggerFn();
102129

103130
// 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+
}
106136
});
107137
});
108138
}

0 commit comments

Comments
 (0)