11/* eslint-disable no-undef */
22
3- function resetProtocolForServer ( ) {
4- // Select Diagnostics
5- cy . get ( '.panel #control_diagnostics_accord .panel-title a' )
6- . contains ( 'Diagnostics' )
7- . click ( ) ;
8- // Open ManageIQ Region: - list view if not already open
9- cy . get (
10- '#diagnostics_accord .treeview li.list-group-item[title^="ManageIQ Region:"]'
11- ) . then ( ( $li ) => {
12- const span = $li . find ( 'span.fa-angle-right' ) ;
13- if ( span . length ) {
14- cy . wrap ( span ) . click ( ) ;
3+ function invokeAndAwaitDiagnosticsInfo ( ) {
4+ let requestFired = false ;
5+ cy . intercept (
6+ 'POST' ,
7+ '/ops/accordion_select?id=diagnostics_accord' ,
8+ ( ) => ( requestFired = true )
9+ ) . as ( 'getDiagnosticsInfo' ) ;
10+ cy . accordion ( 'Diagnostics' ) ;
11+ cy . then ( ( ) => {
12+ // If the request was fired, wait for the alias
13+ if ( requestFired ) {
14+ cy . wait ( '@getDiagnosticsInfo' ) ;
1515 }
1616 } ) ;
17- // Open Zone: - list view if not already open
18- cy . get (
19- '#diagnostics_accord .treeview li.list-group-item[title^="Zone:"]'
20- ) . then ( ( $li ) => {
21- const span = $li . find ( 'span.fa-angle-right' ) ;
22- if ( span . length ) {
23- cy . wrap ( span ) . click ( ) ;
24- }
17+ }
18+
19+ function interceptAndAwaitApi ( {
20+ alias,
21+ method = 'POST' ,
22+ urlPattern,
23+ triggerFn,
24+ currentApiIntercepts,
25+ } ) {
26+ // If the alias is already registered, do not register it again
27+ // This prevents multiple intercepts for the same API call
28+ // which can lead to unexpected behavior in tests.
29+ if ( ! currentApiIntercepts [ alias ] ) {
30+ cy . intercept ( method , urlPattern ) . as ( alias ) ;
31+ currentApiIntercepts [ alias ] = alias ;
32+ }
33+
34+ triggerFn ( ) ;
35+
36+ cy . wait ( `@${ alias } ` ) ;
37+ }
38+
39+ function invokeAndAwaitRegionInfo ( {
40+ currentApiIntercepts,
41+ } ) {
42+ interceptAndAwaitApi ( {
43+ alias : 'getRegionInfo' ,
44+ urlPattern : / o p s \/ t r e e _ s e l e c t \? i d = .* & t e x t = .* M a n a g e I Q .* R e g i o n .* R e g i o n .* / ,
45+ triggerFn : ( ) =>
46+ cy . accordionItem ( 'ManageIQ Region:' , true , 'diagnostics_accord' ) ,
47+ currentApiIntercepts,
2548 } ) ;
26- // Selecting Server: list view
27- cy . get (
28- '#diagnostics_accord .treeview li.list-group-item[title^="Server:"]'
29- ) . click ( ) ;
30- // Selecting Collect Logs nav bar
31- cy . get (
32- '#tab_all_tabs_div #ops_tabs .nav-tabs li#diagnostics_collect_logs_tab'
33- ) . click ( ) ;
34- // Clicking Edit button
35- cy . get (
36- '.miq-toolbar-actions .miq-toolbar-group button#log_depot_edit'
37- ) . click ( ) ;
38- cy . wait ( '@editEventForServer' ) ;
39- // Resetting Protocol dropdown value
40- cy . get ( '#log-depot-settings .bx--select select#log_protocol' ) . then (
41- ( $select ) => {
42- const currentValue = $select . val ( ) ;
43- // If the value is not default one(BLANK_VALUE), then setting it to blank
44- if ( currentValue !== 'BLANK_VALUE' ) {
45- cy . wrap ( $select ) . select ( 'BLANK_VALUE' ) ;
46- cy . get ( '#diagnostics_collect_logs .bx--btn-set button[type="Submit"]' )
47- . contains ( 'Save' )
48- . click ( ) ;
49- cy . get ( '#main_div #flash_msg_div .alert-success' ) . contains (
50- 'Log Depot Settings were saved'
51- ) ;
52- }
53- }
54- ) ;
5549}
5650
57- function resetProtocolForZone ( ) {
58- cy . get ( '.panel #control_diagnostics_accord .panel-title a' )
59- . contains ( 'Diagnostics' )
60- . click ( ) ;
61- // Open ManageIQ Region: - list view if not already open
62- cy . get (
63- '#diagnostics_accord .treeview li.list-group-item[title^="ManageIQ Region:"]'
64- ) . then ( ( $li ) => {
65- const span = $li . find ( 'span.fa-angle-right' ) ;
66- if ( span . length ) {
67- cy . wrap ( span ) . click ( ) ;
68- }
51+ function invokeAndAwaitZoneDefaultInfo ( {
52+ currentApiIntercepts,
53+ } ) {
54+ interceptAndAwaitApi ( {
55+ alias : 'getZoneDefaultInfo' ,
56+ urlPattern :
57+ / o p s \/ t r e e _ s e l e c t \? i d = .* & t e x t = .* Z o n e .* D e f a u l t .* Z o n e .* ( c u r r e n t ) .* / ,
58+ triggerFn : ( ) => cy . accordionItem ( 'Zone:' , true , 'diagnostics_accord' ) ,
59+ currentApiIntercepts,
6960 } ) ;
61+ }
62+
63+ function invokeAndAwaitServerInfo ( {
64+ currentApiIntercepts,
65+ } ) {
66+ interceptAndAwaitApi ( {
67+ alias : 'getServerInfo' ,
68+ urlPattern : / o p s \/ t r e e _ s e l e c t \? i d = .* & t e x t = .* S e r v e r .* E V M .* ( c u r r e n t ) .* / ,
69+ triggerFn : ( ) => cy . accordionItem ( 'Server:' , true , 'diagnostics_accord' ) ,
70+ currentApiIntercepts,
71+ } ) ;
72+ }
73+
74+ function invokeAndAwaitCollectLogsTabInfo ( {
75+ currentApiIntercepts,
76+ } ) {
77+ interceptAndAwaitApi ( {
78+ alias : 'getCollectLogsTabInfo' ,
79+ urlPattern : '/ops/change_tab?tab_id=diagnostics_collect_logs' ,
80+ triggerFn : ( ) =>
81+ cy
82+ . get (
83+ '#tab_all_tabs_div #ops_tabs .nav-tabs li#diagnostics_collect_logs_tab'
84+ )
85+ . click ( ) ,
86+ currentApiIntercepts,
87+ } ) ;
88+ }
89+
90+ function invokeAndAwaitEditEventForServer ( {
91+ currentApiIntercepts,
92+ } ) {
93+ interceptAndAwaitApi ( {
94+ alias : 'editEventForServer' ,
95+ urlPattern : / \/ o p s \/ x _ b u t t o n \/ [ ^ / ] + \? p r e s s e d = .* l o g _ d e p o t _ e d i t / , // matches both /ops/x_button/1?pressed=log_depot_edit & /ops/x_button/2?pressed=zone_log_depot_edit endpoints
96+ triggerFn : ( ) =>
97+ cy
98+ . get (
99+ '.miq-toolbar-actions .miq-toolbar-group button[id$="log_depot_edit"]' // matches both buttons log_depot_edit & zone_log_depot_edit
100+ )
101+ . click ( ) ,
102+ currentApiIntercepts,
103+ } ) ;
104+ }
105+
106+ function resetProtocolDropdown ( {
107+ currentApiIntercepts,
108+ needsServerInfoFetch = true ,
109+ } ) {
110+ // Select Diagnostics
111+ invokeAndAwaitDiagnosticsInfo ( ) ;
112+ // Open ManageIQ Region: - list view if not already open
113+ invokeAndAwaitRegionInfo ( { currentApiIntercepts } ) ;
114+
70115 // Open Zone: - list view if not already open
71- cy . get (
72- '#diagnostics_accord .treeview li.list-group-item[title^="Zone:"]'
73- ) . click ( ) ;
74- // Selecting Collect Logs navbar
75- cy . get (
76- '#tab_all_tabs_div #ops_tabs .nav-tabs li#diagnostics_collect_logs_tab'
77- ) . click ( ) ;
116+ invokeAndAwaitZoneDefaultInfo ( { currentApiIntercepts } ) ;
117+
118+ if ( needsServerInfoFetch ) {
119+ // Selecting Server: list view
120+ invokeAndAwaitServerInfo ( { currentApiIntercepts } ) ;
121+ }
122+ // Selecting Collect Logs nav bar
123+ invokeAndAwaitCollectLogsTabInfo ( { currentApiIntercepts } ) ;
78124 // Clicking Edit button
79- cy . get (
80- '.miq-toolbar-actions .miq-toolbar-group button#zone_log_depot_edit'
81- ) . click ( ) ;
82- cy . wait ( '@editEventForZone' ) ;
125+ invokeAndAwaitEditEventForServer ( { currentApiIntercepts } ) ;
126+
83127 // Resetting Protocol dropdown value
84128 cy . get ( '#log-depot-settings .bx--select select#log_protocol' ) . then (
85129 ( $select ) => {
86130 const currentValue = $select . val ( ) ;
87- // If the value is not default one(BLANK_VALUE), then setting it to blank and then saving
131+ // If the value is not default one(BLANK_VALUE), then setting it to blank
88132 if ( currentValue !== 'BLANK_VALUE' ) {
89133 cy . wrap ( $select ) . select ( 'BLANK_VALUE' ) ;
90134 cy . get ( '#diagnostics_collect_logs .bx--btn-set button[type="Submit"]' )
@@ -116,7 +160,9 @@ function resetButtonValidation() {
116160 . contains ( 'Reset' )
117161 . should ( 'be.disabled' ) ;
118162 // Selecting Samba option from dropdown
119- cy . get ( '#log-depot-settings .bx--select select#log_protocol' ) . select ( 'Samba' ) ;
163+ cy . get ( '#log-depot-settings .bx--select select#log_protocol' ) . select (
164+ 'FileDepotSmb'
165+ ) ;
120166 // Confirm Reset button is enabled once dropdown value is changed and then click on Reset
121167 cy . get ( '#diagnostics_collect_logs .bx--btn-set button[type="button"]' )
122168 . contains ( 'Reset' )
@@ -135,7 +181,9 @@ function saveButtonValidation() {
135181 . contains ( 'Save' )
136182 . should ( 'be.disabled' ) ;
137183 // Selecting Samba option from dropdown
138- cy . get ( '#log-depot-settings .bx--select select#log_protocol' ) . select ( 'Samba' ) ;
184+ cy . get ( '#log-depot-settings .bx--select select#log_protocol' ) . select (
185+ 'FileDepotSmb'
186+ ) ;
139187 // Confirm Save button is enabled once dropdown value is changed and then click on Save
140188 cy . get ( '#diagnostics_collect_logs .bx--btn-set button[type="Submit"]' )
141189 . contains ( 'Save' )
@@ -148,51 +196,40 @@ function saveButtonValidation() {
148196}
149197
150198describe ( 'Automate Collect logs Edit form operations' , ( ) => {
199+ // Map that keeps track of registered API intercepts
200+ // This is used to avoid registering the same API intercept multiple times
201+ // during the test run, which can lead to unexpected behavior.
202+ let registeredApiIntercepts ;
203+
151204 beforeEach ( ( ) => {
205+ registeredApiIntercepts = { } ;
152206 cy . login ( ) ;
153207 // Navigate to Application settings and Select Diagnostics
154208 cy . menu ( 'Settings' , 'Application Settings' ) ;
155- cy . get ( '.panel #control_diagnostics_accord .panel-title a' )
156- . contains ( 'Diagnostics' )
157- . click ( ) ;
209+ invokeAndAwaitDiagnosticsInfo ( ) ;
158210 // Open ManageIQ Region: - list view if not already open
159- cy . get (
160- '#diagnostics_accord .treeview li.list-group-item[title^="ManageIQ Region:"]'
161- ) . then ( ( $li ) => {
162- const span = $li . find ( 'span.fa-angle-right' ) ;
163- if ( span . length ) {
164- cy . wrap ( span ) . click ( ) ;
165- }
166- } ) ;
211+ invokeAndAwaitRegionInfo ( { currentApiIntercepts : registeredApiIntercepts } ) ;
167212 } ) ;
168213
169214 describe ( 'Settings > Application Settings > Diagnostics > Manage IQ Region > Zone > Server > Collect logs > Edit' , ( ) => {
170215 beforeEach ( ( ) => {
171216 // Open Zone: - list view if not already open
172- cy . get (
173- '#diagnostics_accord .treeview li.list-group-item[title^="Zone:"]'
174- ) . then ( ( $li ) => {
175- const span = $li . find ( 'span.fa-angle-right' ) ;
176- if ( span . length ) {
177- cy . wrap ( span ) . click ( ) ;
178- }
217+ invokeAndAwaitZoneDefaultInfo ( {
218+ currentApiIntercepts : registeredApiIntercepts ,
179219 } ) ;
180220 // Selecting Server: list view
181- cy . get (
182- '#diagnostics_accord .treeview li.list-group-item[title^="Server:"]'
183- ) . click ( ) ;
221+ invokeAndAwaitServerInfo ( {
222+ currentApiIntercepts : registeredApiIntercepts ,
223+ } ) ;
184224 // Selecting Collect Logs nav bar
185- cy . get (
186- '#tab_all_tabs_div #ops_tabs .nav-tabs li#diagnostics_collect_logs_tab'
187- ) . click ( ) ;
225+ invokeAndAwaitCollectLogsTabInfo ( {
226+ currentApiIntercepts : registeredApiIntercepts ,
227+ } ) ;
228+
188229 // Clicking Edit button
189- cy . intercept ( 'POST' , '/ops/x_button/1?pressed=log_depot_edit' ) . as (
190- 'editEventForServer'
191- ) ;
192- cy . get (
193- '.miq-toolbar-actions .miq-toolbar-group button#log_depot_edit'
194- ) . click ( ) ;
195- cy . wait ( '@editEventForServer' ) ;
230+ invokeAndAwaitEditEventForServer ( {
231+ currentApiIntercepts : registeredApiIntercepts ,
232+ } ) ;
196233 } ) ;
197234
198235 it ( 'Validate Cancel button' , ( ) => {
@@ -211,11 +248,15 @@ describe('Automate Collect logs Edit form operations', () => {
211248 cy ?. url ( ) ?. then ( ( url ) => {
212249 // Ensures navigation to Settings -> Application-Settings in the UI
213250 if ( url ?. includes ( '/ops/explorer' ) ) {
214- resetProtocolForServer ( ) ;
251+ resetProtocolDropdown ( {
252+ currentApiIntercepts : registeredApiIntercepts ,
253+ } ) ;
215254 } else {
216255 // Navigate to Settings -> Application-Settings before selecting Diagnostics
217256 cy . menu ( 'Settings' , 'Application Settings' ) ;
218- resetProtocolForServer ( ) ;
257+ resetProtocolDropdown ( {
258+ currentApiIntercepts : registeredApiIntercepts ,
259+ } ) ;
219260 }
220261 } ) ;
221262 } ) ;
@@ -224,23 +265,17 @@ describe('Automate Collect logs Edit form operations', () => {
224265 describe ( 'Settings > Application Settings > Diagnostics > Manage IQ Region > Zone > Collect logs > Edit' , ( ) => {
225266 beforeEach ( ( ) => {
226267 // Selecting Zone: - list view
227- cy . intercept ( 'POST' , '/ops/tree_select?id=z-2*' ) . as ( 'treeSelectApi' ) ;
228- cy . get (
229- '#diagnostics_accord .treeview li.list-group-item[title^="Zone:"]'
230- ) . click ( ) ;
231- cy . wait ( '@treeSelectApi' ) ;
268+ invokeAndAwaitZoneDefaultInfo ( {
269+ currentApiIntercepts : registeredApiIntercepts ,
270+ } ) ;
232271 // Selecting Collect Logs navbar
233- cy . get (
234- '#tab_all_tabs_div #ops_tabs .nav-tabs li#diagnostics_collect_logs_tab'
235- ) . click ( ) ;
272+ invokeAndAwaitCollectLogsTabInfo ( {
273+ currentApiIntercepts : registeredApiIntercepts ,
274+ } ) ;
236275 // Clicking Edit button
237- cy . intercept ( 'POST' , '/ops/x_button/2?pressed=zone_log_depot_edit' ) . as (
238- 'editEventForZone'
239- ) ;
240- cy . get (
241- '.miq-toolbar-actions .miq-toolbar-group button#zone_log_depot_edit'
242- ) . click ( ) ;
243- cy . wait ( '@editEventForZone' ) ;
276+ invokeAndAwaitEditEventForServer ( {
277+ currentApiIntercepts : registeredApiIntercepts ,
278+ } ) ;
244279 } ) ;
245280
246281 it ( 'Validate Cancel button' , ( ) => {
@@ -259,11 +294,17 @@ describe('Automate Collect logs Edit form operations', () => {
259294 cy ?. url ( ) ?. then ( ( url ) => {
260295 // Ensures navigation to Settings -> Application-Settings in the UI
261296 if ( url ?. includes ( '/ops/explorer' ) ) {
262- resetProtocolForZone ( ) ;
297+ resetProtocolDropdown ( {
298+ currentApiIntercepts : registeredApiIntercepts ,
299+ needsServerInfoFetch : false ,
300+ } ) ;
263301 } else {
264302 // Navigate to Settings -> Application-Settings before selecting Diagnostics
265303 cy . menu ( 'Settings' , 'Application Settings' ) ;
266- resetProtocolForZone ( ) ;
304+ resetProtocolDropdown ( {
305+ currentApiIntercepts : registeredApiIntercepts ,
306+ needsServerInfoFetch : false ,
307+ } ) ;
267308 }
268309 } ) ;
269310 } ) ;
0 commit comments