Skip to content

Commit 62eb1ce

Browse files
Added expect_modal command
1 parent 3f8fb12 commit 62eb1ce

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

cypress/support/assertions/expect_alerts.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,75 @@ 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+
);

0 commit comments

Comments
 (0)