Skip to content

Commit 1f1e603

Browse files
jrafanieFryguy
authored andcommitted
Merge pull request #9557 from asirvadAbrahamVarghese/enhance-expect-alerts-command
Enhance expect alerts command (cherry picked from commit 30db374)
1 parent 961248a commit 1f1e603

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

cypress/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ ManageIQ implements the following cypress extensions:
7676
* `cy.expect_show_list_title(title)` - check the title on a show\_list screen matches the provided title. `title`: String for the title.
7777
* `cy.expect_search_box()` - check if searchbox is present on the screen.
7878
* `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.
79-
* `cy.expect_flash(flashType, containsText)` - command to validate flash messages. `flashType` is the type of flash (success, warning, error, info). `containsText` is the optional text that the flash-message should contain. e.g. `expect_flash('warning', 'cancelled');`
79+
* `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');`
8080
* `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()});`

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable no-undef */
2+
import { flashClassMap } from '../../../../support/assertions/assertion_constants';
23

34
const textConstants = {
45
// List items
@@ -49,12 +50,6 @@ const textConstants = {
4950
settingsMenuOption: 'Settings',
5051
appSettingsMenuOption: 'Application Settings',
5152

52-
// Flash message types
53-
flashTypeSuccess: 'success',
54-
flashTypeWarning: 'warning',
55-
flashTypeError: 'error',
56-
flashTypeInfo: 'info',
57-
5853
// Flash message text snippets
5954
flashMessageScheduleQueued: 'queued to run',
6055
flashMessageOperationCanceled: 'cancelled',
@@ -107,10 +102,6 @@ const {
107102
deleteScheduleConfigOption,
108103
schedulesAccordionItem,
109104
configToolbarButton,
110-
flashTypeSuccess,
111-
flashTypeWarning,
112-
flashTypeError,
113-
flashTypeInfo,
114105
flashMessageScheduleQueued,
115106
flashMessageOperationCanceled,
116107
flashMessageScheduleDisabled,
@@ -171,7 +162,7 @@ function deleteSchedule(scheduleName = initialScheduleName) {
171162
confirmTriggerFn: () => selectConfigMenu(deleteScheduleConfigOption),
172163
containsText: browserAlertDeleteConfirmText,
173164
});
174-
cy.expect_flash(flashTypeSuccess, flashMessageScheduleDeleted);
165+
cy.expect_flash(flashClassMap.success, flashMessageScheduleDeleted);
175166
}
176167

177168
function interceptGetScheduleDetailsApi(scheduleName = initialScheduleName) {
@@ -387,13 +378,13 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
387378
)
388379
.should('be.enabled')
389380
.click();
390-
cy.expect_flash(flashTypeSuccess, flashMessageOperationCanceled);
381+
cy.expect_flash(flashClassMap.success, flashMessageOperationCanceled);
391382
});
392383

393384
it('Checking whether add, edit & delete schedule works', () => {
394385
/* ===== Adding a schedule ===== */
395386
addSchedule();
396-
cy.expect_flash(flashTypeSuccess, flashMessageScheduleSaved);
387+
cy.expect_flash(flashClassMap.success, flashMessageScheduleSaved);
397388

398389
/* ===== Editing a schedule ===== */
399390
// Selecting the schedule and intercepting the API call to get schedule details
@@ -406,7 +397,7 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
406397
cy.contains('#main-content .bx--btn-set button[type="submit"]', saveButton)
407398
.should('be.enabled')
408399
.click();
409-
cy.expect_flash(flashTypeSuccess, flashMessageScheduleSaved);
400+
cy.expect_flash(flashClassMap.success, flashMessageScheduleSaved);
410401

411402
/* ===== Delete is already handled from afterEach hook ===== */
412403
});
@@ -425,7 +416,7 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
425416
)
426417
.should('be.enabled')
427418
.click();
428-
cy.expect_flash(flashTypeSuccess, flashMessageOperationCanceled);
419+
cy.expect_flash(flashClassMap.success, flashMessageOperationCanceled);
429420

430421
/* ===== Checking whether Reset button works ===== */
431422
// Selecting the schedule and intercepting the API call to get schedule details
@@ -437,7 +428,7 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
437428
cy.contains('#main-content .bx--btn-set button[type="button"]', resetButton)
438429
.should('be.enabled')
439430
.click();
440-
cy.expect_flash(flashTypeWarning, flashMessageResetSchedule);
431+
cy.expect_flash(flashClassMap.warning, flashMessageResetSchedule);
441432
// Confirming the edited fields contain the old values after resetting
442433
cy.get('input#description').should('have.value', initialDescription);
443434
cy.get('input#start_date').should('have.value', initialStartDate);
@@ -452,7 +443,7 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
452443

453444
/* ===== Trying to add the same schedule again ===== */
454445
addSchedule();
455-
cy.expect_flash(flashTypeError, flashMessageFailedToAddSchedule);
446+
cy.expect_flash(flashClassMap.error, flashMessageFailedToAddSchedule);
456447
});
457448

458449
it('Checking whether Disabling, Enabling & Queueing up the schedule works', () => {
@@ -463,15 +454,15 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
463454

464455
/* ===== Disabling the schedule ===== */
465456
selectConfigMenu(disableScheduleConfigOption);
466-
cy.expect_flash(flashTypeInfo, flashMessageScheduleDisabled);
457+
cy.expect_flash(flashClassMap.info, flashMessageScheduleDisabled);
467458

468459
/* ===== Enabling the schedule ===== */
469460
selectConfigMenu(enableScheduleConfigOption);
470-
cy.expect_flash(flashTypeInfo, flashMessageScheduleEnabled);
461+
cy.expect_flash(flashClassMap.info, flashMessageScheduleEnabled);
471462

472463
/* ===== Queueing-up the schedule ===== */
473464
selectConfigMenu(queueScheduleConfigOption);
474-
cy.expect_flash(flashTypeSuccess, flashMessageScheduleQueued);
465+
cy.expect_flash(flashClassMap.success, flashMessageScheduleQueued);
475466
});
476467

477468
afterEach(() => {

cypress/support/assertions/assertion_constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Map of flash message types to their corresponding CSS class names.
3+
* Used by the expect_flash command to validate flash messages.
4+
* @example
5+
* cy.expect_flash(flashClassMap.success);
6+
* cy.expect_flash(flashClassMap.error, 'failed');
7+
*/
18
export const flashClassMap = {
29
warning: 'warning',
310
error: 'danger',

cypress/support/assertions/expect_alerts.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,39 @@ import { flashClassMap } from './assertion_constants';
33

44
/**
55
* Custom Cypress command to validate flash messages.
6-
* @param {string} flashType - Type of flash (success, warning, error, info).
6+
* @param {string} flashType - Type of flash. Use values from flashClassMap (e.g., flashClassMap.success, flashClassMap.error).
77
* @param {string} [containsText] - Optional text that the flash-message should contain.
88
* @returns {Cypress.Chainable} - The flash-message element if found, or an assertion failure.
9+
* @example
10+
* cy.expect_flash(flashClassMap.success);
11+
* cy.expect_flash(flashClassMap.error, 'failed');
912
*/
1013
Cypress.Commands.add(
1114
'expect_flash',
1215
(flashType = flashClassMap.success, containsText) => {
13-
const flashMessageClassName = flashClassMap[flashType] || flashClassMap.success;
14-
const flashMessageElement = cy
15-
.get(`#main_div #flash_msg_div .alert-${flashMessageClassName}`)
16-
.should('be.visible');
16+
if (Object.values(flashClassMap).includes(flashType)) {
17+
const flashMessageElement = cy
18+
.get(`#main_div #flash_msg_div .alert-${flashType}`)
19+
.should('be.visible');
1720

18-
if (containsText) {
19-
return flashMessageElement.should(($el) => {
20-
const actualText = $el.text().toLowerCase();
21-
expect(actualText).to.include(containsText.toLowerCase());
22-
});
21+
if (containsText) {
22+
return flashMessageElement.should((flash) => {
23+
const actualText = flash.text().toLowerCase();
24+
expect(actualText).to.include(containsText.toLowerCase());
25+
});
26+
}
27+
28+
return flashMessageElement;
2329
}
2430

25-
return flashMessageElement;
31+
// If an invalid flash type is passed, throw an error
32+
cy.logAndThrowError(
33+
`Invalid flash type: "${flashType}". Valid flash types are: ${Object.values(
34+
flashClassMap
35+
).join(
36+
', '
37+
)}. It is recommended to use flashClassMap values (e.g., flashClassMap.error, flashClassMap.success) to pass flash types.`
38+
);
2639
}
2740
);
2841

0 commit comments

Comments
 (0)