Skip to content

Commit 32e94ed

Browse files
Introduced cypress commands to assert flash-alerts
1 parent 46fb33c commit 32e94ed

File tree

3 files changed

+66
-40
lines changed

3 files changed

+66
-40
lines changed

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

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,10 @@ function addSchedule() {
3535
function deleteSchedule(scheduleName = 'Test name') {
3636
// Selecting the schedule
3737
cy.contains('li.list-group-item', scheduleName).click();
38-
cy.on('window:confirm', (text) => {
39-
expect(text).to.eq(
40-
'Warning: This Schedule and ALL of its components will be permanently removed!'
41-
);
42-
return true;
43-
});
44-
selectConfigMenu('Delete this Schedule from the Database');
45-
cy.get('#main_div #flash_msg_div .alert-success').contains(
46-
`Schedule "${scheduleName}": Delete successful`
47-
);
38+
// Listening for the browser confirm alert and confirming
39+
cy.listen_for_browser_confirm_alert();
40+
selectConfigMenu(deleteScheduleConfigOption);
41+
cy.expect_flash('success');
4842
}
4943

5044
function invokeCleanupDeletion() {
@@ -224,17 +218,13 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
224218
cy.contains('#main-content .bx--btn-set button[type="button"]', 'Cancel')
225219
.should('be.enabled')
226220
.click();
227-
cy.get('#main_div #flash_msg_div .alert-success').contains(
228-
'Add was cancelled by the user'
229-
);
221+
cy.expect_flash('success');
230222
});
231223

232224
it('Checking whether add, edit & delete schedule works', () => {
233225
/* ===== Adding a schedule ===== */
234226
addSchedule();
235-
cy.get('#main_div #flash_msg_div .alert-success').contains(
236-
'Schedule "Test name" was saved'
237-
);
227+
cy.expect_flash('success');
238228

239229
/* ===== Editing a schedule ===== */
240230
// Selecting the created schedule
@@ -247,9 +237,7 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
247237
cy.contains('#main-content .bx--btn-set button[type="submit"]', 'Save')
248238
.should('be.enabled')
249239
.click();
250-
cy.get('#main_div #flash_msg_div .alert-success').contains(
251-
'Schedule "Dummy name" was saved'
252-
);
240+
cy.expect_flash('success');
253241

254242
/* ===== Delete is already handled from afterEach hook ===== */
255243
});
@@ -265,9 +253,7 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
265253
cy.contains('#main-content .bx--btn-set button[type="button"]', 'Cancel')
266254
.should('be.enabled')
267255
.click();
268-
cy.get('#main_div #flash_msg_div .alert-success').contains(
269-
'Edit of "Test name" was cancelled by the user'
270-
);
256+
cy.expect_flash('success');
271257

272258
/* ===== Checking whether Reset button works ===== */
273259
// Selecting the created schedule
@@ -279,9 +265,7 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
279265
cy.contains('#main-content .bx--btn-set button[type="button"]', 'Reset')
280266
.should('be.enabled')
281267
.click();
282-
cy.get('#main_div #flash_msg_div .alert-warning').contains(
283-
'All changes have been reset'
284-
);
268+
cy.expect_flash('warning');
285269
// Confirming the edited fields contain the old values after resetting
286270
cy.get('input#description').should('have.value', 'Test description');
287271
cy.get('input#start_date').should('have.value', '06/30/2025');
@@ -296,9 +280,7 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
296280

297281
/* ===== Trying to add the same schedule again ===== */
298282
addSchedule();
299-
cy.get('#main_div #flash_msg_div .alert-danger').contains(
300-
'Error when adding a new schedule: Validation failed: MiqSchedule: Name has already been taken'
301-
);
283+
cy.expect_flash('error');
302284
});
303285

304286
it('Checking whether Disabling, Enabling & Queueing up the schedule works', () => {
@@ -308,22 +290,16 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
308290
cy.contains('li.list-group-item', 'Test name').click();
309291

310292
/* ===== Disabling the schedule ===== */
311-
selectConfigMenu('Disable this Schedule');
312-
cy.get('#main_div #flash_msg_div .alert-info').contains(
313-
'The selected Schedules were disabled'
314-
);
293+
selectConfigMenu(disableScheduleConfigOption);
294+
cy.expect_flash('info');
315295

316296
/* ===== Enabling the schedule ===== */
317-
selectConfigMenu('Enable this Schedule');
318-
cy.get('#main_div #flash_msg_div .alert-info').contains(
319-
'The selected Schedules were enabled'
320-
);
297+
selectConfigMenu(enableScheduleConfigOption);
298+
cy.expect_flash('info');
321299

322300
/* ===== Queueing-up the schedule ===== */
323-
selectConfigMenu('Queue up this Schedule to run now');
324-
cy.get('#main_div #flash_msg_div .alert-success').contains(
325-
'The selected Schedule has been queued to run'
326-
);
301+
selectConfigMenu(queueScheduleConfigOption);
302+
cy.expect_flash('success');
327303
});
328304

329305
afterEach(() => {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-disable no-undef */
2+
3+
/**
4+
* Custom Cypress command to validate flash messages.
5+
* @param {string} alertType - Type of alert (success, warning, error, info).
6+
* @param {string} [containsText] - Optional text that the alert should contain.
7+
* @returns {Cypress.Chainable} - The alert element if found, or an assertion failure.
8+
*/
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');
28+
29+
if (containsText) {
30+
return alert.should(($el) => {
31+
const actualText = $el.text().toLowerCase();
32+
expect(actualText).to.include(containsText.toLowerCase());
33+
});
34+
}
35+
36+
return alert;
37+
});
38+
39+
Cypress.Commands.add(
40+
'listen_for_browser_confirm_alert',
41+
(containsText, proceed = true) => {
42+
cy.on('window:confirm', (actualText) => {
43+
if (containsText) {
44+
expect(actualText.toLowerCase()).to.include(containsText.toLowerCase());
45+
}
46+
return proceed; // true = OK, false = Cancel
47+
});
48+
}
49+
);

cypress/support/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import './assertions/expect_rates_table.js';
5353
import './assertions/expect_search_box.js'
5454
import './assertions/expect_title.js'
5555
import './assertions/expect_text.js'
56+
import './assertions/expect_alerts.js';
5657

5758
// This is needed to prevent Cypress tests from failing due to uncaught errors:
5859
// Undefined errors are occuring on every initial page load of Manage IQ

0 commit comments

Comments
 (0)