@@ -4,36 +4,35 @@ test.describe('Game Listing and Navigation', () => {
44 test ( 'should display games with titles on index page' , async ( { page } ) => {
55 await page . goto ( '/' ) ;
66
7- // Wait for the games to load
8- await page . waitForSelector ( '[data-testid="games-grid"]' , { timeout : 10000 } ) ;
7+ // Wait for the games to load using a more specific locator
8+ const gamesGrid = page . getByTestId ( 'games-grid' ) ;
9+ await expect ( gamesGrid ) . toBeVisible ( ) ;
910
1011 // Check that games are displayed
11- const gameCards = page . locator ( '[data-testid=" game-card"] ') ;
12+ const gameCards = page . getByTestId ( ' game-card') ;
1213
1314 // Wait for at least one game card to be visible
1415 await expect ( gameCards . first ( ) ) . toBeVisible ( ) ;
1516
1617 // Check that we have at least one game
17- const gameCount = await gameCards . count ( ) ;
18- expect ( gameCount ) . toBeGreaterThan ( 0 ) ;
18+ expect ( await gameCards . count ( ) ) . toBeGreaterThan ( 0 ) ;
1919
2020 // Check that each game card has a title
21- const firstGameCard = gameCards . first ( ) ;
22- await expect ( firstGameCard . locator ( '[data-testid="game-title"]' ) ) . toBeVisible ( ) ;
21+ await expect ( gameCards . first ( ) . getByTestId ( 'game-title' ) ) . toBeVisible ( ) ;
2322
2423 // Verify that game titles are not empty
25- const gameTitle = await firstGameCard . locator ( '[data-testid="game-title"]' ) . textContent ( ) ;
26- expect ( gameTitle ?. trim ( ) ) . toBeTruthy ( ) ;
24+ await expect ( gameCards . first ( ) . getByTestId ( 'game-title' ) ) . not . toBeEmpty ( ) ;
2725 } ) ;
2826
2927 test ( 'should navigate to correct game details page when clicking on a game' , async ( { page } ) => {
3028 await page . goto ( '/' ) ;
3129
3230 // Wait for games to load
33- await page . waitForSelector ( '[data-testid="games-grid"]' , { timeout : 10000 } ) ;
31+ const gamesGrid = page . getByTestId ( 'games-grid' ) ;
32+ await expect ( gamesGrid ) . toBeVisible ( ) ;
3433
3534 // Get the first game card and its data attributes
36- const firstGameCard = page . locator ( '[data-testid=" game-card"] ') . first ( ) ;
35+ const firstGameCard = page . getByTestId ( ' game-card') . first ( ) ;
3736 const gameId = await firstGameCard . getAttribute ( 'data-game-id' ) ;
3837 const gameTitle = await firstGameCard . getAttribute ( 'data-game-title' ) ;
3938
@@ -44,58 +43,55 @@ test.describe('Game Listing and Navigation', () => {
4443 await expect ( page ) . toHaveURL ( `/game/${ gameId } ` ) ;
4544
4645 // Verify the game details page loads
47- await page . waitForSelector ( '[data-testid=" game-details"]' , { timeout : 10000 } ) ;
46+ await expect ( page . getByTestId ( ' game-details' ) ) . toBeVisible ( ) ;
4847
4948 // Verify the title matches what we clicked on
50- const detailsTitle = page . locator ( '[data-testid="game-details-title"]' ) ;
51- await expect ( detailsTitle ) . toHaveText ( gameTitle || '' ) ;
49+ if ( gameTitle ) {
50+ await expect ( page . getByTestId ( 'game-details-title' ) ) . toHaveText ( gameTitle ) ;
51+ }
5252 } ) ;
5353
5454 test ( 'should display game details with all required information' , async ( { page } ) => {
5555 // Navigate to a specific game (we'll use game ID 1 as an example)
5656 await page . goto ( '/game/1' ) ;
5757
5858 // Wait for game details to load
59- await page . waitForSelector ( '[data-testid=" game-details"]' , { timeout : 10000 } ) ;
59+ await expect ( page . getByTestId ( ' game-details' ) ) . toBeVisible ( ) ;
6060
6161 // Check that the game title is present and not empty
62- const gameTitle = page . locator ( '[data-testid=" game-details-title"] ') ;
62+ const gameTitle = page . getByTestId ( ' game-details-title') ;
6363 await expect ( gameTitle ) . toBeVisible ( ) ;
64- const titleText = await gameTitle . textContent ( ) ;
65- expect ( titleText ?. trim ( ) ) . toBeTruthy ( ) ;
64+ await expect ( gameTitle ) . not . toBeEmpty ( ) ;
6665
6766 // Check that the game description is present and not empty
68- const gameDescription = page . locator ( '[data-testid=" game-details-description"] ') ;
67+ const gameDescription = page . getByTestId ( ' game-details-description') ;
6968 await expect ( gameDescription ) . toBeVisible ( ) ;
70- const descriptionText = await gameDescription . textContent ( ) ;
71- expect ( descriptionText ?. trim ( ) ) . toBeTruthy ( ) ;
69+ await expect ( gameDescription ) . not . toBeEmpty ( ) ;
7270
7371 // Check that either publisher or category (or both) are present
74- const publisherExists = await page . locator ( '[data-testid=" game-details-publisher"] ') . isVisible ( ) ;
75- const categoryExists = await page . locator ( '[data-testid=" game-details-category"] ') . isVisible ( ) ;
76- expect ( publisherExists && categoryExists ) . toBeTruthy ( ) ;
72+ const publisherExists = await page . getByTestId ( ' game-details-publisher') . isVisible ( ) ;
73+ const categoryExists = await page . getByTestId ( ' game-details-category') . isVisible ( ) ;
74+ expect ( publisherExists || categoryExists ) . toBeTruthy ( ) ;
7775
7876 // If publisher exists, check it has content
7977 if ( publisherExists ) {
80- const publisherText = await page . locator ( '[data-testid="game-details-publisher"]' ) . textContent ( ) ;
81- expect ( publisherText ?. trim ( ) ) . toBeTruthy ( ) ;
78+ await expect ( page . getByTestId ( 'game-details-publisher' ) ) . not . toBeEmpty ( ) ;
8279 }
8380
8481 // If category exists, check it has content
8582 if ( categoryExists ) {
86- const categoryText = await page . locator ( '[data-testid="game-details-category"]' ) . textContent ( ) ;
87- expect ( categoryText ?. trim ( ) ) . toBeTruthy ( ) ;
83+ await expect ( page . getByTestId ( 'game-details-category' ) ) . not . toBeEmpty ( ) ;
8884 }
8985 } ) ;
9086
9187 test ( 'should display a button to back the game' , async ( { page } ) => {
9288 await page . goto ( '/game/1' ) ;
9389
9490 // Wait for game details to load
95- await page . waitForSelector ( '[data-testid=" game-details"]' , { timeout : 10000 } ) ;
91+ await expect ( page . getByTestId ( ' game-details' ) ) . toBeVisible ( ) ;
9692
9793 // Check that the back game button is present
98- const backButton = page . locator ( '[data-testid=" back-game-button"] ') ;
94+ const backButton = page . getByTestId ( ' back-game-button') ;
9995 await expect ( backButton ) . toBeVisible ( ) ;
10096 await expect ( backButton ) . toContainText ( 'Support This Game' ) ;
10197
@@ -107,25 +103,24 @@ test.describe('Game Listing and Navigation', () => {
107103 await page . goto ( '/game/1' ) ;
108104
109105 // Wait for the page to load
110- await page . waitForSelector ( '[data-testid=" game-details"]' , { timeout : 10000 } ) ;
106+ await expect ( page . getByTestId ( ' game-details' ) ) . toBeVisible ( ) ;
111107
112- // Find and click the back to all games link
113- const backLink = page . locator ( 'a:has-text("Back to all games")' ) ;
108+ // Find and click the back to all games link using a more semantic locator
109+ const backLink = page . getByRole ( 'link' , { name : / b a c k t o a l l g a m e s / i } ) ;
114110 await expect ( backLink ) . toBeVisible ( ) ;
115111 await backLink . click ( ) ;
116112
117113 // Verify we're back on the home page
118114 await expect ( page ) . toHaveURL ( '/' ) ;
119- await page . waitForSelector ( '[data-testid=" games-grid"]' , { timeout : 10000 } ) ;
115+ await expect ( page . getByTestId ( ' games-grid' ) ) . toBeVisible ( ) ;
120116 } ) ;
121117
122118 test ( 'should handle navigation to non-existent game gracefully' , async ( { page } ) => {
123119 // Navigate to a game that doesn't exist
124- await page . goto ( '/game/99999' ) ;
120+ const response = await page . goto ( '/game/99999' ) ;
125121
126- // The page should load without crashing
127- // Check if there's an error message or if it handles gracefully
128- await page . waitForTimeout ( 3000 ) ;
122+ // Check the response status or ensure the page loads without crashing
123+ expect ( response ?. status ( ) ) . toBeLessThan ( 500 ) ;
129124
130125 // The page should either show an error or handle it gracefully
131126 // We expect the page to not crash and still have a valid title
0 commit comments