Skip to content

Commit e157b37

Browse files
authored
Merge pull request #9552 from asirvadAbrahamVarghese/add-intercept-api-command
Add intercept api command
2 parents 27d3e45 + af93f3e commit e157b37

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* eslint-disable no-undef */
2+
3+
// TODO: Remove this test once interceptApi command becomes stable
4+
describe('Validate intercept command', () => {
5+
beforeEach(() => {
6+
cy.login();
7+
// Navigate to Application settings
8+
cy.menu('Settings', 'Application Settings');
9+
});
10+
11+
it('Should register the alias, intercept & wait when API fired', () => {
12+
cy.accordion('Diagnostics');
13+
cy.interceptApi({
14+
alias: 'treeSelectApi',
15+
urlPattern: /\/ops\/tree_select\?id=.*&text=.*/,
16+
triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]),
17+
}).then(() => {
18+
// verifies that the alias is set and the request is intercepted & awaited
19+
expect(Cypress.env('interceptedAliases')).to.have.property(
20+
'treeSelectApi'
21+
);
22+
});
23+
});
24+
25+
it('Should register multiple unique aliases', () => {
26+
// first api with alias 'accordionSelectApi'
27+
cy.interceptApi({
28+
alias: 'accordionSelectApi',
29+
urlPattern: /\/ops\/accordion_select\?id=.*/,
30+
triggerFn: () => cy.accordion('Diagnostics'),
31+
});
32+
// second api with alias 'treeSelectApi'
33+
cy.interceptApi({
34+
alias: 'treeSelectApi',
35+
urlPattern: /\/ops\/tree_select\?id=.*&text=.*/,
36+
triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]),
37+
}).then(() => {
38+
// 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+
);
43+
});
44+
});
45+
46+
it('Should not register duplicate alias', () => {
47+
// add first api with alias 'accordionSelectApi'
48+
cy.interceptApi({
49+
alias: 'accordionSelectApi',
50+
urlPattern: /\/ops\/accordion_select\?id=.*/,
51+
triggerFn: () => cy.accordion('Diagnostics'),
52+
}).then(() => {
53+
expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(1);
54+
});
55+
// second first api with alias 'treeSelectApi'
56+
cy.interceptApi({
57+
alias: 'treeSelectApi',
58+
urlPattern: /\/ops\/tree_select\?id=.*&text=.*/,
59+
triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]),
60+
}).then(() => {
61+
expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(2);
62+
});
63+
// third api with a duplicate alias as above 'accordionSelectApi'
64+
cy.interceptApi({
65+
alias: 'accordionSelectApi',
66+
urlPattern: /\/ops\/accordion_select\?id=.*/,
67+
triggerFn: () => cy.accordion('Access Control'),
68+
}).then(() => {
69+
// assert that the alias is not overwritten
70+
expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(2);
71+
});
72+
});
73+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* eslint-disable no-undef */
2+
3+
/**
4+
* Custom command to intercept API calls and wait for them to complete.
5+
* This command will:
6+
* 1. Register an intercept for the given alias and URL pattern if not already registered
7+
* 2. Execute the trigger function that makes the API call
8+
* 3. Wait for the intercepted request to complete if it was triggered
9+
*
10+
* @param {Object} options - The options for the intercept
11+
* @param {string} options.alias - Unique alias for this interception
12+
* @param {string} options.method - HTTP method (default: 'POST')
13+
* @param {string|RegExp} options.urlPattern - URL pattern to intercept
14+
* @param {Function} options.triggerFn - Function that triggers the API call
15+
*/
16+
Cypress.Commands.add(
17+
'interceptApi',
18+
({ alias, method = 'POST', urlPattern, triggerFn }) => {
19+
/* ===== TODO: Remove this block once interceptApi command becomes stable ===== */
20+
const envVars = Cypress.env();
21+
cy.log('Cypress Environment Variables:');
22+
cy.log(JSON.stringify(envVars, null, 2));
23+
/* ======================================================= */
24+
25+
// 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);
31+
32+
// Store the alias in the tracking object
33+
const interceptedAliases = Cypress.env('interceptedAliases');
34+
interceptedAliases[alias] = alias;
35+
Cypress.env('interceptedAliases', interceptedAliases);
36+
}
37+
38+
// Execute the function that triggers the API call
39+
triggerFn();
40+
41+
// Wait for the intercepted request to complete
42+
cy.wait(`@${alias}`);
43+
}
44+
);

cypress/support/e2e.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
// ***********************************************************
4141

4242
// Commands
43+
import './commands/api_commands.js';
4344
import './commands/custom_logging_commands.js';
4445
import './commands/element_selectors.js';
4546
import './commands/explorer.js';
@@ -76,4 +77,7 @@ beforeEach(() => {
7677
// Global hook run once before each test
7778
// cy.throttle_response(500, 56);
7879
// cy.stub_notifications();
80+
81+
// Reset the intercepted aliases tracking object
82+
Cypress.env('interceptedAliases', {});
7983
})

0 commit comments

Comments
 (0)