11import { test , expect } from '@playwright/test' ;
2- import { loginAsRole } from '../../fixtures/auth.fixtures' ;
2+ import { loginAsRole , logout } from '../../fixtures/auth.fixtures' ;
33
44/**
55 * Dashboard Metrics Tests
@@ -34,10 +34,12 @@ test.describe('Dashboard Metrics', () => {
3434 } ) ;
3535
3636 test ( 'should display employee count metric' , async ( { page } ) => {
37- // Look for employee count metric card or widget
38- const employeeMetric = page . locator ( 'mat-card, .metric, .stat, .widget' ) . filter ( { hasText : / e m p l o y e e | t o t a l .* e m p l o y e e / i } ) ;
37+ // Look for employee count metric card - use more specific selector
38+ const employeeMetric = page . locator ( 'mat-card.metric-card , .metric, .stat, .widget' ) . filter ( { hasText : / t o t a l .* e m p l o y e e / i } ) . first ( ) ;
3939
40- if ( await employeeMetric . isVisible ( { timeout : 3000 } ) ) {
40+ const isVisible = await employeeMetric . isVisible ( { timeout : 3000 } ) . catch ( ( ) => false ) ;
41+
42+ if ( isVisible ) {
4143 // Verify it contains a number
4244 const metricText = await employeeMetric . textContent ( ) ;
4345 expect ( metricText ) . toMatch ( / \d + / ) ; // Contains at least one digit
@@ -54,10 +56,12 @@ test.describe('Dashboard Metrics', () => {
5456 } ) ;
5557
5658 test ( 'should display department count metric' , async ( { page } ) => {
57- // Look for department count metric card or widget
58- const departmentMetric = page . locator ( 'mat-card, .metric, .stat, .widget' ) . filter ( { hasText : / d e p a r t m e n t | t o t a l .* d e p a r t m e n t / i } ) ;
59+ // Look for department count metric card - use more specific selector
60+ const departmentMetric = page . locator ( 'mat-card.metric-card, .metric, .stat, .widget' ) . filter ( { hasText : / \d + .* d e p a r t m e n t / i } ) . first ( ) ;
61+
62+ const isVisible = await departmentMetric . isVisible ( { timeout : 3000 } ) . catch ( ( ) => false ) ;
5963
60- if ( await departmentMetric . isVisible ( { timeout : 3000 } ) ) {
64+ if ( isVisible ) {
6165 // Verify it contains a number
6266 const metricText = await departmentMetric . textContent ( ) ;
6367 expect ( metricText ) . toMatch ( / \d + / ) ;
@@ -74,10 +78,12 @@ test.describe('Dashboard Metrics', () => {
7478 } ) ;
7579
7680 test ( 'should display position count metric' , async ( { page } ) => {
77- // Look for position count metric card or widget
78- const positionMetric = page . locator ( 'mat-card, .metric, .stat, .widget' ) . filter ( { hasText : / p o s i t i o n | t o t a l .* p o s i t i o n / i } ) ;
81+ // Look for position count metric card - use more specific selector
82+ const positionMetric = page . locator ( 'mat-card.metric-card , .metric, .stat, .widget' ) . filter ( { hasText : / \d + .* p o s i t i o n / i } ) . first ( ) ;
7983
80- if ( await positionMetric . isVisible ( { timeout : 3000 } ) ) {
84+ const isVisible = await positionMetric . isVisible ( { timeout : 3000 } ) . catch ( ( ) => false ) ;
85+
86+ if ( isVisible ) {
8187 // Verify it contains a number
8288 const metricText = await positionMetric . textContent ( ) ;
8389 expect ( metricText ) . toMatch ( / \d + / ) ;
@@ -147,19 +153,21 @@ test.describe('Dashboard Metrics', () => {
147153 } ) ;
148154
149155 test ( 'should refresh metrics on page reload' , async ( { page } ) => {
150- // Get initial employee count
151- const employeeMetric = page . locator ( 'mat-card, .metric, .stat, .widget' ) . filter ( { hasText : / e m p l o y e e / i } ) ;
156+ // Get initial employee count - use first() to avoid strict mode violation
157+ const employeeMetric = page . locator ( 'mat-card.metric-card, .metric, .stat, .widget' ) . filter ( { hasText : / e m p l o y e e / i } ) . first ( ) ;
158+
159+ const isVisible = await employeeMetric . isVisible ( { timeout : 3000 } ) . catch ( ( ) => false ) ;
152160
153- if ( await employeeMetric . isVisible ( { timeout : 3000 } ) ) {
161+ if ( isVisible ) {
154162 const initialText = await employeeMetric . textContent ( ) ;
155163
156164 // Reload page
157165 await page . reload ( ) ;
158166 await page . waitForLoadState ( 'networkidle' ) ;
159167
160168 // Metrics should load again
161- const reloadedMetric = page . locator ( 'mat-card, .metric, .stat, .widget' ) . filter ( { hasText : / e m p l o y e e / i } ) ;
162- await expect ( reloadedMetric . first ( ) ) . toBeVisible ( { timeout : 5000 } ) ;
169+ const reloadedMetric = page . locator ( 'mat-card.metric-card , .metric, .stat, .widget' ) . filter ( { hasText : / e m p l o y e e / i } ) . first ( ) ;
170+ await expect ( reloadedMetric ) . toBeVisible ( { timeout : 5000 } ) ;
163171
164172 const reloadedText = await reloadedMetric . textContent ( ) ;
165173
@@ -223,8 +231,8 @@ test.describe('Dashboard Metrics', () => {
223231 } ) ;
224232
225233 test ( 'should allow Employee role to view dashboard' , async ( { page } ) => {
226- // Logout and login as Employee
227- await page . goto ( '/' ) ;
234+ // Logout current user and login as Employee
235+ await logout ( page ) ;
228236 await loginAsRole ( page , 'employee' ) ;
229237 await page . goto ( '/dashboard' ) ;
230238 await page . waitForLoadState ( 'networkidle' ) ;
0 commit comments