Skip to content

Commit 611b461

Browse files
authored
Merge pull request #9653 from asirvadAbrahamVarghese/enhance-expect-alerts-command
Enhance expect alerts command
2 parents 1fd6ec8 + 336a4e4 commit 611b461

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

cypress/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,6 @@ ManageIQ implements the following cypress extensions:
103103
* `cy.expect_search_box()` - check if searchbox is present on the screen.
104104
* `cy.expect_text(element, text)` - check if the text in the element found by doing cy.get on the element String matches the provided text. `element`: String for the Cypress selector to get a specific element on the screen. `text`: String for the text that should be found within the selected element.
105105
* `cy.expect_flash(flashType, containsText)` - command to validate flash messages. `flashType` is the type of flash. It is recommended to use values from `flashClassMap`.`containsText` is the optional text that the flash-message should contain. e.g. `expect_flash(flashClassMap.warning, 'cancelled');`
106-
* `cy.expect_browser_confirm_with_text({ confirmTriggerFn, containsText, proceed })` - command to validate browser confirm alerts. `confirmTriggerFn` is the function that triggers the confirm dialog. This function **must return a Cypress.Chainable**, like `cy.get(...).click()` so that Cypress can properly wait and chain .then() afterward. `containsText` is the optional text that the confirm alert should contain. `proceed` is the flag to determine whether to proceed with the confirm (true = OK, false = Cancel). e.g. `cy.expect_browser_confirm_with_text({containsText: 'sure to proceed?', proceed: true, confirmTriggerFn: () => { return cy.get('[data-testid="delete"]').click()}});`, `cy.expect_browser_confirm_with_text({ confirmTriggerFn: () => cy.contains('deleted').click()});`
106+
* `cy.expect_browser_confirm_with_text({ confirmTriggerFn, containsText, proceed })` - command to validate browser confirm alerts. `confirmTriggerFn` is the function that triggers the confirm dialog. This function **must return a Cypress.Chainable**, like `cy.get(...).click()` so that Cypress can properly wait and chain .then() afterward. `containsText` is the optional text that the confirm alert should contain. `proceed` is the flag to determine whether to proceed with the confirm (true = OK, false = Cancel). e.g. `cy.expect_browser_confirm_with_text({containsText: 'sure to proceed?', proceed: true, confirmTriggerFn: () => { return cy.get('[data-testid="delete"]').click()}});`, `cy.expect_browser_confirm_with_text({ confirmTriggerFn: () => cy.contains('deleted').click()});`
107+
* `cy.expect_modal({ modalHeaderText, modalContentExpectedTexts, targetFooterButtonText })` - command to validate and interact with modal dialogs. Verifies the modal content and clicks a specified button in the modal footer. `modalHeaderText` is the optional text to verify in the modal header (case insensitive). `modalContentExpectedTexts` is an optional array of text strings that should be present in the modal content (case insensitive). `targetFooterButtonText` is the text of the button in the modal footer to click (required). e.g. `cy.expect_modal({ modalHeaderText: 'Confirmation', modalContentExpectedTexts: ['you want to continue?'], targetFooterButtonText: 'Confirm' });`, `cy.expect_modal({ modalContentExpectedTexts: ['cannot be undone.', 'data will be permanently deleted.'], targetFooterButtonText: 'Cancel' });`, `cy.expect_modal({ targetFooterButtonText: 'OK' });`
108+
* `cy.expect_inline_field_errors({ containsText })` - command to validate inline field error messages. `containsText` is the text that the error message should contain (required). e.g. `cy.expect_inline_field_errors({ containsText: 'blank' });`, `cy.expect_inline_field_errors({ containsText: 'taken' });`

cypress/support/assertions/expect_alerts.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,91 @@ Cypress.Commands.add(
7878
});
7979
}
8080
);
81+
82+
/**
83+
* Custom Cypress command to validate and interact with modal dialogs.
84+
* This command verifies the modal content and clicks a specified button in the modal footer.
85+
*
86+
* @param {Object} options - Options for the command.
87+
* @param {string} [options.modalHeaderText] - Optional text to verify in the modal header.
88+
* @param {string[]} [options.modalContentExpectedTexts] - Optional array of text strings that should be present in the modal content.
89+
* @param {string} options.targetFooterButtonText - Text of the button in the modal footer to click (required).
90+
*
91+
* @example
92+
* // Verify a confirmation modal and click "Confirm"
93+
* cy.expect_modal({
94+
* modalHeaderText: 'Confirmation',
95+
* modalContentExpectedTexts: ['Are you sure you want to proceed?'],
96+
* targetFooterButtonText: 'Confirm'
97+
* });
98+
*
99+
* @example
100+
* // Verify a modal with multiple content texts and click "Cancel"
101+
* cy.expect_modal({
102+
* modalHeaderText: 'Warning',
103+
* modalContentExpectedTexts: [
104+
* 'action cannot be undone',
105+
* 'data will be permanently deleted'
106+
* ],
107+
* targetFooterButtonText: 'Cancel'
108+
* });
109+
*
110+
* @example
111+
* // Just click "OK" in a modal without verifying content
112+
* cy.expect_modal({
113+
* targetFooterButtonText: 'OK'
114+
* });
115+
*/
116+
Cypress.Commands.add(
117+
'expect_modal',
118+
({
119+
modalHeaderText,
120+
modalContentExpectedTexts = [],
121+
targetFooterButtonText,
122+
}) => {
123+
if (!targetFooterButtonText) {
124+
cy.logAndThrowError(
125+
'targetFooterButtonText must be provided to identify the button that dismisses the modal'
126+
);
127+
}
128+
129+
if (modalHeaderText) {
130+
cy.get('.bx--modal-container .bx--modal-header').should((header) => {
131+
const headerText = header.text().toLowerCase();
132+
expect(headerText).to.include(modalHeaderText.toLowerCase());
133+
});
134+
}
135+
136+
if (modalContentExpectedTexts && modalContentExpectedTexts.length) {
137+
modalContentExpectedTexts.forEach((text) => {
138+
cy.get('.bx--modal-container .bx--modal-content').should((content) => {
139+
const contentText = content.text().toLowerCase();
140+
expect(contentText).to.include(text.toLowerCase());
141+
});
142+
});
143+
}
144+
145+
return cy
146+
.contains(
147+
'.bx--modal-container .bx--modal-footer button',
148+
targetFooterButtonText
149+
)
150+
.click();
151+
}
152+
);
153+
154+
/**
155+
* Custom Cypress command to validate inline field error messages.
156+
* @param {Object} options - Options for the command.
157+
* @param {string} options.containsText - Text that the error message should contain. This parameter is required.
158+
* @returns {Cypress.Chainable} - The error message element if found, or an assertion failure.
159+
* @example
160+
* cy.expect_inline_field_errors({ containsText: 'blank' });
161+
* cy.expect_inline_field_errors({ containsText: 'taken' });
162+
*/
163+
Cypress.Commands.add('expect_inline_field_errors', ({ containsText }) => {
164+
if (!containsText) {
165+
cy.logAndThrowError('containsText is required');
166+
}
167+
return cy.contains('#name-error-msg', containsText);
168+
});

0 commit comments

Comments
 (0)