Skip to content

Commit 9613839

Browse files
Add setter, getter & reset utils for intercepted aliases
1 parent a1c9d49 commit 9613839

File tree

3 files changed

+86
-26
lines changed

3 files changed

+86
-26
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ describe('Validate intercept command', () => {
1616
triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]),
1717
}).then(() => {
1818
// verifies that the alias is set and the request is intercepted & awaited
19-
expect(Cypress.env('interceptedAliases')).to.have.property(
20-
'treeSelectApi'
21-
);
19+
cy.getInterceptedApiAliases().then((interceptedAliasesObject) => {
20+
expect(interceptedAliasesObject).to.have.property('treeSelectApi');
21+
});
2222
});
2323
});
2424

@@ -36,10 +36,12 @@ describe('Validate intercept command', () => {
3636
triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]),
3737
}).then(() => {
3838
// verifies that both the aliases are set and the request is intercepted & awaited
39-
expect(Cypress.env('interceptedAliases')).to.include.all.keys(
40-
'accordionSelectApi',
41-
'treeSelectApi'
42-
);
39+
cy.getInterceptedApiAliases().then((interceptedAliasesObject) => {
40+
expect(interceptedAliasesObject).to.include.all.keys(
41+
'accordionSelectApi',
42+
'treeSelectApi'
43+
);
44+
});
4345
});
4446
});
4547

@@ -50,15 +52,19 @@ describe('Validate intercept command', () => {
5052
urlPattern: /\/ops\/accordion_select\?id=.*/,
5153
triggerFn: () => cy.accordion('Diagnostics'),
5254
}).then(() => {
53-
expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(1);
55+
cy.getInterceptedApiAliases().then((interceptedAliasesObject) => {
56+
expect(Object.keys(interceptedAliasesObject).length).to.equal(1);
57+
});
5458
});
5559
// second first api with alias 'treeSelectApi'
5660
cy.interceptApi({
5761
alias: 'treeSelectApi',
5862
urlPattern: /\/ops\/tree_select\?id=.*&text=.*/,
5963
triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]),
6064
}).then(() => {
61-
expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(2);
65+
cy.getInterceptedApiAliases().then((interceptedAliasesObject) => {
66+
expect(Object.keys(interceptedAliasesObject).length).to.equal(2);
67+
});
6268
});
6369
// third api with a duplicate alias as above 'accordionSelectApi'
6470
cy.interceptApi({
@@ -67,7 +73,9 @@ describe('Validate intercept command', () => {
6773
triggerFn: () => cy.accordion('Access Control'),
6874
}).then(() => {
6975
// assert that the alias is not overwritten
70-
expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(2);
76+
cy.getInterceptedApiAliases().then((interceptedAliasesObject) => {
77+
expect(Object.keys(interceptedAliasesObject).length).to.equal(2);
78+
});
7179
});
7280
});
7381
});

cypress/support/commands/api_commands.js

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
/* eslint-disable no-undef */
22

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+
356
/**
457
* Custom command to intercept API calls and wait for them to complete.
558
* This command will:
@@ -34,24 +87,23 @@ Cypress.Commands.add(
3487
/* ======================================================= */
3588

3689
// 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];
4293

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+
}
4899

49-
// Execute the function that triggers the API call
50-
triggerFn();
100+
// Execute the function that triggers the API call
101+
triggerFn();
51102

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+
});
55107
});
56108
}
57109
);

cypress/support/e2e.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,5 @@ beforeEach(() => {
7979
// cy.stub_notifications();
8080

8181
// Reset the intercepted aliases tracking object
82-
Cypress.env('interceptedAliases', {});
82+
cy.resetInterceptedApiAliases();
8383
})

0 commit comments

Comments
 (0)