@@ -22,12 +22,27 @@ test.describe('Authentication', () => {
2222 await expect ( page . locator ( 'button[type="submit"]' ) ) . toBeVisible ( ) ;
2323 } ) ;
2424
25- test ( 'shows validation when submitting empty form' , async ( { page } ) => {
25+ test ( 'prevents submission and shows validation for empty form' , async ( { page } ) => {
2626 await page . goto ( '/login' ) ;
2727 await page . waitForSelector ( '#username' ) ;
2828
29+ // Click submit without filling any fields
30+ await page . click ( 'button[type="submit"]' ) ;
31+
32+ // Form should not submit - still on login page
33+ await expect ( page ) . toHaveURL ( / \/ l o g i n / ) ;
34+
35+ // Browser focuses first invalid required field and shows validation
2936 const usernameInput = page . locator ( '#username' ) ;
30- await expect ( usernameInput ) . toHaveAttribute ( 'required' , '' ) ;
37+ await expect ( usernameInput ) . toBeFocused ( ) ;
38+
39+ // Check HTML5 validity state
40+ const isInvalid = await usernameInput . evaluate ( ( el : HTMLInputElement ) => ! el . validity . valid ) ;
41+ expect ( isInvalid ) . toBe ( true ) ;
42+
43+ // Verify validation message exists (browser shows "Please fill out this field" or similar)
44+ const validationMessage = await usernameInput . evaluate ( ( el : HTMLInputElement ) => el . validationMessage ) ;
45+ expect ( validationMessage . length ) . toBeGreaterThan ( 0 ) ;
3146 } ) ;
3247
3348 test ( 'shows error with invalid credentials' , async ( { page } ) => {
@@ -124,12 +139,11 @@ test.describe('Logout', () => {
124139 } ) ;
125140
126141 test ( 'can logout from authenticated state' , async ( { page } ) => {
127- // Find and click logout button
128- const logoutButton = page . locator ( 'button:has-text("Logout"), a:has-text("Logout"), [data-testid="logout"]' ) ;
142+ // Logout button is in the header - must exist when authenticated
143+ const logoutButton = page . locator ( 'button:has-text("Logout")' ) . first ( ) ;
129144
130- if ( await logoutButton . isVisible ( ) ) {
131- await logoutButton . click ( ) ;
132- await expect ( page ) . toHaveURL ( / \/ l o g i n / ) ;
133- }
145+ await expect ( logoutButton ) . toBeVisible ( ) ;
146+ await logoutButton . click ( ) ;
147+ await expect ( page ) . toHaveURL ( / \/ l o g i n / ) ;
134148 } ) ;
135149} ) ;
0 commit comments