From 033862c9141e6d4087ae716b54ce29551e99da02 Mon Sep 17 00:00:00 2001 From: asirvadAbrahamVarghese Date: Tue, 12 Aug 2025 12:10:57 +0530 Subject: [PATCH 1/2] Add interceptApi command to prevent duplicate intercept aliases --- cypress/support/commands/api_commands.js | 44 ++++++++++++++++++++++++ cypress/support/e2e.js | 4 +++ 2 files changed, 48 insertions(+) create mode 100644 cypress/support/commands/api_commands.js diff --git a/cypress/support/commands/api_commands.js b/cypress/support/commands/api_commands.js new file mode 100644 index 00000000000..2e78dc79984 --- /dev/null +++ b/cypress/support/commands/api_commands.js @@ -0,0 +1,44 @@ +/* eslint-disable no-undef */ + +/** + * Custom command to intercept API calls and wait 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 if it was triggered + * + * @param {Object} options - The options for the intercept + * @param {string} options.alias - Unique alias for this interception + * @param {string} options.method - HTTP method (default: 'POST') + * @param {string|RegExp} options.urlPattern - URL pattern to intercept + * @param {Function} options.triggerFn - Function that triggers the API call + */ +Cypress.Commands.add( + 'interceptApi', + ({ alias, method = 'POST', urlPattern, triggerFn }) => { + /* ===== TODO: Remove this block once interceptApi command becomes stable ===== */ + const envVars = Cypress.env(); + cy.log('Cypress Environment Variables:'); + cy.log(JSON.stringify(envVars, null, 2)); + /* ======================================================= */ + + // Check if this request is already registered + const isAlreadyRegistered = !!Cypress.env('interceptedAliases')[alias]; + + // Register the intercept if not already done + if (!isAlreadyRegistered) { + cy.intercept(method, urlPattern).as(alias); + + // Store the alias in the tracking object + const interceptedAliases = Cypress.env('interceptedAliases'); + interceptedAliases[alias] = alias; + Cypress.env('interceptedAliases', interceptedAliases); + } + + // Execute the function that triggers the API call + triggerFn(); + + // Wait for the intercepted request to complete + cy.wait(`@${alias}`); + } +); diff --git a/cypress/support/e2e.js b/cypress/support/e2e.js index 526746fe076..f243f145e77 100644 --- a/cypress/support/e2e.js +++ b/cypress/support/e2e.js @@ -40,6 +40,7 @@ // *********************************************************** // Commands +import './commands/api_commands.js'; import './commands/custom_logging_commands.js'; import './commands/element_selectors.js'; import './commands/explorer.js'; @@ -76,4 +77,7 @@ beforeEach(() => { // Global hook run once before each test // cy.throttle_response(500, 56); // cy.stub_notifications(); + + // Reset the intercepted aliases tracking object + Cypress.env('interceptedAliases', {}); }) From af93f3e04cd3ab0f2b6dcb10d2e1bf2d6b9e9b74 Mon Sep 17 00:00:00 2001 From: asirvadAbrahamVarghese Date: Tue, 12 Aug 2025 12:12:56 +0530 Subject: [PATCH 2/2] Add tests for interceptApi command --- .../ui/validate-intercept-api-command.cy.js | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 cypress/e2e/ui/validate-intercept-api-command.cy.js diff --git a/cypress/e2e/ui/validate-intercept-api-command.cy.js b/cypress/e2e/ui/validate-intercept-api-command.cy.js new file mode 100644 index 00000000000..efa31475031 --- /dev/null +++ b/cypress/e2e/ui/validate-intercept-api-command.cy.js @@ -0,0 +1,73 @@ +/* eslint-disable no-undef */ + +// TODO: Remove this test once interceptApi command becomes stable +describe('Validate intercept command', () => { + beforeEach(() => { + cy.login(); + // Navigate to Application settings + cy.menu('Settings', 'Application Settings'); + }); + + it('Should register the alias, intercept & wait when API fired', () => { + cy.accordion('Diagnostics'); + cy.interceptApi({ + alias: 'treeSelectApi', + urlPattern: /\/ops\/tree_select\?id=.*&text=.*/, + triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]), + }).then(() => { + // verifies that the alias is set and the request is intercepted & awaited + expect(Cypress.env('interceptedAliases')).to.have.property( + 'treeSelectApi' + ); + }); + }); + + it('Should register multiple unique aliases', () => { + // first api with alias 'accordionSelectApi' + cy.interceptApi({ + alias: 'accordionSelectApi', + urlPattern: /\/ops\/accordion_select\?id=.*/, + triggerFn: () => cy.accordion('Diagnostics'), + }); + // second api with alias 'treeSelectApi' + cy.interceptApi({ + alias: 'treeSelectApi', + urlPattern: /\/ops\/tree_select\?id=.*&text=.*/, + triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]), + }).then(() => { + // verifies that both the aliases are set and the request is intercepted & awaited + expect(Cypress.env('interceptedAliases')).to.include.all.keys( + 'accordionSelectApi', + 'treeSelectApi' + ); + }); + }); + + it('Should not register duplicate alias', () => { + // add first api with alias 'accordionSelectApi' + cy.interceptApi({ + alias: 'accordionSelectApi', + urlPattern: /\/ops\/accordion_select\?id=.*/, + triggerFn: () => cy.accordion('Diagnostics'), + }).then(() => { + expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(1); + }); + // second first api with alias 'treeSelectApi' + cy.interceptApi({ + alias: 'treeSelectApi', + urlPattern: /\/ops\/tree_select\?id=.*&text=.*/, + triggerFn: () => cy.selectAccordionItem([/^ManageIQ Region:/, /^Zone:/]), + }).then(() => { + expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(2); + }); + // third api with a duplicate alias as above 'accordionSelectApi' + cy.interceptApi({ + alias: 'accordionSelectApi', + urlPattern: /\/ops\/accordion_select\?id=.*/, + triggerFn: () => cy.accordion('Access Control'), + }).then(() => { + // assert that the alias is not overwritten + expect(Object.keys(Cypress.env('interceptedAliases')).length).to.equal(2); + }); + }); +});