Skip to content

Commit 62b3ad0

Browse files
Add more API intercepts for better test stability
1 parent 31b33e6 commit 62b3ad0

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

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

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const textConstants = {
55
schedulesAccordionItem: 'Schedules',
66

77
// Field values
8-
initialScheduleName: 'Test name',
9-
editedScheduleName: 'Dummy name',
8+
initialScheduleName: 'Test-name',
9+
editedScheduleName: 'Dummy-name',
1010
initialDescription: 'Test description',
1111
editedDescription: 'Dummy description',
1212
actionTypeVmAnalysis: 'vm',
@@ -152,15 +152,20 @@ function addSchedule() {
152152
.click();
153153
cy.get('input#start_date').type(initialStartDate);
154154
cy.get('input#start_time').type(startTime);
155-
// Checks if Save button is enabled once all required fields are filled
155+
// Intercepting the API call for adding a new schedule
156+
cy.intercept('POST', '/ops/schedule_edit/new?button=save').as(
157+
'addScheduleApi'
158+
);
156159
cy.contains('#main-content .bx--btn-set button[type="submit"]', saveButton)
157-
.should('be.enabled')
160+
.should('be.enabled') // Checks if Save button is enabled once all required fields are filled
158161
.click();
162+
// Wait for the API call to complete
163+
cy.wait('@addScheduleApi');
159164
}
160165

161166
function deleteSchedule(scheduleName = initialScheduleName) {
162-
// Selecting the schedule
163-
cy.accordionItem(scheduleName);
167+
// Selecting the schedule and intercepting the API call to get schedule details
168+
interceptGetScheduleDetailsApi(scheduleName);
164169
// Listening for the browser confirm alert and confirming deletion
165170
cy.expect_browser_confirm_with_text({
166171
confirmTriggerFn: () => selectConfigMenu(deleteScheduleConfigOption),
@@ -169,6 +174,33 @@ function deleteSchedule(scheduleName = initialScheduleName) {
169174
cy.expect_flash(flashTypeSuccess, flashMessageScheduleDeleted);
170175
}
171176

177+
function interceptGetScheduleDetailsApi(scheduleName = initialScheduleName) {
178+
// Flag to check if the request is fired
179+
let requestFired = false;
180+
// Intercepting the API call
181+
cy.intercept(
182+
{
183+
method: 'POST',
184+
pathname: '/ops/tree_select',
185+
query: { text: scheduleName },
186+
},
187+
// This callback function will be called when the request is fired,
188+
// from which the requestFired flag will be set to true
189+
() => (requestFired = true)
190+
).as('getCreatedScheduleApi');
191+
// Triggering the action that will fire the API call,
192+
// which is selecting the created schedule
193+
cy.accordionItem(scheduleName);
194+
// Wait for the API call to complete if it was fired
195+
// This is to ensure that the test does not fail if the request was not fired
196+
cy.then(() => {
197+
// If the request was fired, wait for the alias
198+
if (requestFired) {
199+
cy.wait('@getCreatedScheduleApi');
200+
}
201+
});
202+
}
203+
172204
function invokeCleanupDeletion() {
173205
// Iterate and clean up any leftover schedules created during the test
174206
cy.get('li.list-group-item').each(($el) => {
@@ -199,9 +231,13 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
199231
beforeEach(() => {
200232
cy.login();
201233
cy.menu(settingsMenuOption, appSettingsMenuOption);
202-
cy.intercept('POST', '/ops/tree_select?id=xx-msc&text=Schedules').as(
203-
'getSchedules'
204-
);
234+
cy.intercept(
235+
{
236+
method: 'POST',
237+
pathname: '/ops/tree_select',
238+
query: { text: schedulesAccordionItem },
239+
},
240+
).as('getSchedules');
205241
cy.accordionItem(schedulesAccordionItem);
206242
cy.wait('@getSchedules');
207243
});
@@ -360,8 +396,8 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
360396
cy.expect_flash(flashTypeSuccess, flashMessageScheduleSaved);
361397

362398
/* ===== Editing a schedule ===== */
363-
// Selecting the created schedule
364-
cy.accordionItem(initialScheduleName);
399+
// Selecting the schedule and intercepting the API call to get schedule details
400+
interceptGetScheduleDetailsApi();
365401
selectConfigMenu(editScheduleConfigOption);
366402
// Editing name and description
367403
cy.get('input#name').clear().type(editedScheduleName);
@@ -380,8 +416,8 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
380416
addSchedule();
381417

382418
/* ===== Checking whether Cancel button works ===== */
383-
// Selecting the created schedule
384-
cy.accordionItem(initialScheduleName);
419+
// Selecting the schedule and intercepting the API call to get schedule details
420+
interceptGetScheduleDetailsApi();
385421
selectConfigMenu(editScheduleConfigOption);
386422
cy.contains(
387423
'#main-content .bx--btn-set button[type="button"]',
@@ -392,8 +428,8 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
392428
cy.expect_flash(flashTypeSuccess, flashMessageOperationCanceled);
393429

394430
/* ===== Checking whether Reset button works ===== */
395-
// Selecting the created schedule
396-
cy.accordionItem(initialScheduleName);
431+
// Selecting the schedule and intercepting the API call to get schedule details
432+
interceptGetScheduleDetailsApi();
397433
selectConfigMenu(editScheduleConfigOption);
398434
// Editing description and start date
399435
cy.get('input#description').clear().type(editedDescription);
@@ -422,8 +458,8 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
422458
it('Checking whether Disabling, Enabling & Queueing up the schedule works', () => {
423459
/* ===== Adding a schedule ===== */
424460
addSchedule();
425-
// Selecting the created schedule
426-
cy.accordionItem(initialScheduleName);
461+
// Selecting the schedule and intercepting the API call to get schedule details
462+
interceptGetScheduleDetailsApi();
427463

428464
/* ===== Disabling the schedule ===== */
429465
selectConfigMenu(disableScheduleConfigOption);

0 commit comments

Comments
 (0)