Skip to content

Add intercept api command #9552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions cypress/e2e/ui/validate-intercept-api-command.cy.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
44 changes: 44 additions & 0 deletions cypress/support/commands/api_commands.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
);
4 changes: 4 additions & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', {});
})
Loading