Skip to content

Commit df5dcbc

Browse files
Reworked browser-confirm command to validate alert presence
1 parent 933a62d commit df5dcbc

File tree

2 files changed

+65
-34
lines changed

2 files changed

+65
-34
lines changed

cypress/e2e/ui/Settings/Application-Settings/schedule.cy.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ const {
8989
} = textConstants;
9090

9191
function selectConfigMenu(configuration = addScheduleConfigOption) {
92-
cy.get('#miq_schedule_vmdb_choice').click();
93-
cy.get(`ul[aria-label="Configuration"] [title="${configuration}"]`).click();
92+
cy.get(
93+
`.miq-toolbar-actions .miq-toolbar-group button[title="Configuration"]`
94+
).click();
95+
return cy
96+
.get(
97+
`ul#overflow-menu-1__menu-body button[title="${configuration}"][role="menuitem"]`
98+
)
99+
.click();
94100
}
95101

96102
function addSchedule() {
@@ -128,9 +134,10 @@ function addSchedule() {
128134
function deleteSchedule(scheduleName = initialScheduleName) {
129135
// Selecting the schedule
130136
cy.accordionItem(scheduleName);
131-
// Listening for the browser confirm alert and confirming
132-
cy.listen_for_browser_confirm_alert();
133-
selectConfigMenu(deleteScheduleConfigOption);
137+
// Listening for the browser confirm alert and confirming deletion
138+
cy.expect_browser_confirm_with_text({
139+
confirmTriggerFn: () => selectConfigMenu(deleteScheduleConfigOption),
140+
});
134141
cy.expect_flash('success');
135142
}
136143

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,73 @@
11
/* eslint-disable no-undef */
22

3+
const flashClassMap = {
4+
warning: 'warning',
5+
error: 'danger',
6+
info: 'info',
7+
success: 'success',
8+
};
9+
310
/**
411
* Custom Cypress command to validate flash messages.
512
* @param {string} alertType - Type of alert (success, warning, error, info).
613
* @param {string} [containsText] - Optional text that the alert should contain.
714
* @returns {Cypress.Chainable} - The alert element if found, or an assertion failure.
815
*/
9-
Cypress.Commands.add('expect_flash', (alertType = 'success', containsText) => {
10-
let alertClassName;
11-
switch (alertType) {
12-
case 'warning':
13-
alertClassName = 'warning';
14-
break;
15-
case 'error':
16-
alertClassName = 'danger';
17-
break;
18-
case 'info':
19-
alertClassName = 'info';
20-
break;
21-
default:
22-
alertClassName = 'success';
23-
break;
24-
}
25-
const alert = cy
26-
.get(`#main_div #flash_msg_div .alert-${alertClassName}`)
27-
.should('be.visible');
16+
Cypress.Commands.add(
17+
'expect_flash',
18+
(flashType = flashClassMap.success, containsText) => {
19+
const alertClassName = flashClassMap[flashType] || flashClassMap.success;
20+
const alert = cy
21+
.get(`#main_div #flash_msg_div .alert-${alertClassName}`)
22+
.should('be.visible');
2823

29-
if (containsText) {
30-
return alert.should(($el) => {
31-
const actualText = $el.text().toLowerCase();
32-
expect(actualText).to.include(containsText.toLowerCase());
33-
});
34-
}
24+
if (containsText) {
25+
return alert.should(($el) => {
26+
const actualText = $el.text().toLowerCase();
27+
expect(actualText).to.include(containsText.toLowerCase());
28+
});
29+
}
3530

36-
return alert;
37-
});
31+
return alert;
32+
}
33+
);
3834

35+
/**
36+
* Custom Cypress command to validate browser confirm alerts.
37+
* @param {Object} options - Options for the command.
38+
* @param {Function<Cypress.Chainable>} options.confirmTriggerFn - A function that triggers the confirm dialog.
39+
* This function **must return a Cypress.Chainable**, like `cy.get(...).click()`,
40+
* so that Cypress can properly wait and chain `.then()` afterward.
41+
* @example
42+
* cy.expectBrowserConfirm({
43+
* containsText: 'sure to proceed?',
44+
* proceed: true,
45+
* confirmTriggerFn: () => {
46+
* return cy.get('[data-testid="delete"]').click()
47+
* }
48+
* });
49+
* @example
50+
* cy.expectBrowserConfirm({
51+
* confirmTriggerFn: () => cy.contains('deleted').click()
52+
* });
53+
* @param {string} [options.containsText] - Optional text that the confirm alert should contain.
54+
* @param {boolean} [options.proceed=true] - Whether to proceed with the confirm (true = OK, false = Cancel).
55+
*/
3956
Cypress.Commands.add(
40-
'listen_for_browser_confirm_alert',
41-
(containsText, proceed = true) => {
57+
'expect_browser_confirm_with_text',
58+
({ confirmTriggerFn, containsText, proceed = true }) => {
59+
let alertTriggered = false;
4260
cy.on('window:confirm', (actualText) => {
61+
alertTriggered = true;
4362
if (containsText) {
4463
expect(actualText.toLowerCase()).to.include(containsText.toLowerCase());
4564
}
4665
return proceed; // true = OK, false = Cancel
4766
});
67+
// Fires the event that triggers the confirm dialog
68+
confirmTriggerFn().then(() => {
69+
expect(alertTriggered, 'Expected browser confirm alert to be triggered')
70+
.to.be.true;
71+
});
4872
}
4973
);

0 commit comments

Comments
 (0)