@@ -33,12 +33,55 @@ describe('Send an email', () => {
3333
3434 const TEST_EMAIL_SUBJECT = `Smoke Test Email ${ timestamp } `
3535
36+ function ensureLoggedInToGrantApplications ( ) {
37+ // Headless runs specs sequentially in the same browser process.
38+ // Do not assume logged-out or logged-in. Detect UI state like chefsdata.cy.ts does.
39+ cy . visit ( Cypress . env ( 'webapp.url' ) )
40+
41+ cy . get ( 'body' , { timeout : STANDARD_TIMEOUT } ) . then ( ( $body ) => {
42+ // Already authenticated
43+ if ( $body . find ( 'button:contains("VIEW APPLICATIONS")' ) . length > 0 ) {
44+ cy . contains ( 'VIEW APPLICATIONS' , { timeout : STANDARD_TIMEOUT } ) . click ( { force : true } )
45+ return
46+ }
47+
48+ // Not authenticated
49+ if ( $body . find ( 'button:contains("LOGIN")' ) . length > 0 ) {
50+ cy . contains ( 'LOGIN' , { timeout : STANDARD_TIMEOUT } ) . should ( 'exist' ) . click ( { force : true } )
51+
52+ cy . get ( 'body' , { timeout : STANDARD_TIMEOUT } ) . then ( ( $loginBody ) => {
53+ // IDIR chooser may or may not appear
54+ if ( $loginBody . find ( ':contains("IDIR")' ) . length > 0 ) {
55+ cy . contains ( 'IDIR' , { timeout : STANDARD_TIMEOUT } ) . click ( { force : true } )
56+ }
57+
58+ cy . get ( 'body' , { timeout : STANDARD_TIMEOUT } ) . then ( ( $authBody ) => {
59+ // Only type creds if the login form is actually present
60+ if ( $authBody . find ( '#user' ) . length > 0 ) {
61+ cy . get ( '#user' , { timeout : STANDARD_TIMEOUT } ) . type ( Cypress . env ( 'test1username' ) )
62+ cy . get ( '#password' , { timeout : STANDARD_TIMEOUT } ) . type ( Cypress . env ( 'test1password' ) )
63+ cy . contains ( 'Continue' , { timeout : STANDARD_TIMEOUT } ) . click ( { force : true } )
64+ } else {
65+ cy . log ( 'Already authenticated' )
66+ }
67+ } )
68+ } )
69+
70+ return
71+ }
72+
73+ throw new Error ( 'Unable to determine authentication state' )
74+ } )
75+
76+ cy . location ( 'pathname' , { timeout : 30000 } ) . should ( 'include' , '/GrantApplications' )
77+ }
78+
3679 function switchToDefaultGrantsProgramIfAvailable ( ) {
3780 cy . get ( 'body' ) . then ( ( $body ) => {
3881 const hasUserInitials = $body . find ( '.unity-user-initials' ) . length > 0
3982
4083 if ( ! hasUserInitials ) {
41- cy . log ( 'Skipping tenant switch : no user initials menu found' )
84+ cy . log ( 'Skipping tenant: no user initials menu found' )
4285 return
4386 }
4487
@@ -50,7 +93,7 @@ describe('Send an email', () => {
5093 } )
5194
5295 if ( switchLink . length === 0 ) {
53- cy . log ( 'Skipping tenant switch : "Switch Grant Programs" not present for this user/session' )
96+ cy . log ( 'Skipping tenant: "Switch Grant Programs" not present for this user/session' )
5497 cy . get ( 'body' ) . click ( 0 , 0 )
5598 return
5699 }
@@ -97,15 +140,13 @@ describe('Send an email', () => {
97140 return
98141 }
99142
100- // Fallback: find the subject anywhere in a TD (scoped to avoid brittle class names)
101143 cy . contains ( 'td' , subject , { timeout : STANDARD_TIMEOUT } )
102144 . should ( 'exist' )
103145 . click ( )
104146 } )
105147 }
106148
107149 function confirmSendDialogIfPresent ( ) {
108- // Wait until either a bootstrap modal is shown, or SweetAlert container appears, or confirm button exists.
109150 cy . get ( 'body' , { timeout : STANDARD_TIMEOUT } ) . should ( ( $b ) => {
110151 const hasBootstrapShownModal = $b . find ( '.modal.show' ) . length > 0
111152 const hasSwal = $b . find ( '.swal2-container' ) . length > 0
@@ -116,11 +157,9 @@ describe('Send an email', () => {
116157 cy . get ( 'body' , { timeout : STANDARD_TIMEOUT } ) . then ( ( $b ) => {
117158 const hasSwal = $b . find ( '.swal2-container' ) . length > 0
118159 if ( hasSwal ) {
119- // SweetAlert2 style
120160 cy . get ( '.swal2-container' , { timeout : STANDARD_TIMEOUT } ) . should ( 'be.visible' )
121161 cy . contains ( '.swal2-container' , 'Are you sure' , { timeout : STANDARD_TIMEOUT } ) . should ( 'exist' )
122162
123- // Typical confirm button class, with fallback to text match
124163 if ( $b . find ( '.swal2-confirm' ) . length > 0 ) {
125164 cy . get ( '.swal2-confirm' , { timeout : STANDARD_TIMEOUT } ) . should ( 'be.visible' ) . click ( )
126165 } else {
@@ -131,14 +170,12 @@ describe('Send an email', () => {
131170
132171 const hasBootstrapShownModal = $b . find ( '.modal.show' ) . length > 0
133172 if ( hasBootstrapShownModal ) {
134- // Bootstrap modal: assert the shown modal, not the inner content div
135173 cy . get ( '.modal.show' , { timeout : STANDARD_TIMEOUT } )
136174 . should ( 'be.visible' )
137175 . within ( ( ) => {
138176 cy . contains ( 'Are you sure you want to send this email?' , { timeout : STANDARD_TIMEOUT } )
139177 . should ( 'exist' )
140178
141- // Prefer the known id if present, otherwise click a button with expected intent text
142179 if ( Cypress . $ ( '#btn-confirm-send' ) . length > 0 ) {
143180 cy . get ( '#btn-confirm-send' , { timeout : STANDARD_TIMEOUT } )
144181 . should ( 'exist' )
@@ -151,15 +188,14 @@ describe('Send an email', () => {
151188 return
152189 }
153190
154- // Last resort: confirm button exists but modal might not be "visible" by Cypress standards
155191 cy . get ( '#btn-confirm-send' , { timeout : STANDARD_TIMEOUT } )
156192 . should ( 'exist' )
157193 . click ( { force : true } )
158194 } )
159195 }
160196
161197 it ( 'Login' , ( ) => {
162- cy . login ( )
198+ ensureLoggedInToGrantApplications ( )
163199 } )
164200
165201 it ( 'Switch to Default Grants Program if available' , ( ) => {
0 commit comments