Skip to content

Commit 0dfb536

Browse files
authored
chore: prefer getByRole, do not use soft (#25)
1 parent da714ea commit 0dfb536

16 files changed

+154
-174
lines changed

movies-app/components/UI/HamburgerButton/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Bar = withTheme(({ theme }) => (
1919

2020
const HamburgerButton = props => (
2121
<>
22-
<div aria-label="menu" className='hamburger-button' {...props}>
22+
<div role="menu" className='hamburger-button' {...props}>
2323
<Bar />
2424
<Bar />
2525
<Bar />

package-lock.json

Lines changed: 16 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"devDependencies": {
1717
"@eslint/js": "^9.18.0",
18-
"@playwright/test": "^1.51.0-alpha-2025-01-22",
18+
"@playwright/test": "^1.51.0-alpha-2025-01-29",
1919
"@types/eslint__js": "^8.42.3",
2020
"@types/node": "^22.5.4",
2121
"eslint": "^9.18.0",

tests/helpers/list-utilities.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export async function createList(
1818
listDescription: string,
1919
) {
2020
await test.step('create a new list', async () => {
21-
await page.getByLabel('User Profile').click();
21+
await page.getByRole('button', { name: 'User Profile' }).click();
2222
await page.getByRole('link', { name: 'Create New List' }).click();
23-
await page.getByLabel('Name').fill(listName);
24-
await page.getByLabel('Description').fill(listDescription);
23+
await page.getByRole('textbox', { name: 'Name' }).fill(listName);
24+
await page.getByRole('textbox', { name: 'Description' }).fill(listDescription);
2525
await page.getByRole('button', { name: 'Continue' }).click();
2626
// wait until the list was created and we're on its add/remove movies page
2727
await page.waitForURL(url => url.searchParams.has('listId'));
@@ -40,7 +40,7 @@ export async function createList(
4040
*/
4141
export async function openLists(page: Page, name: string = 'My Lists') {
4242
await test.step('open list from menu', async () => {
43-
await page.getByLabel('User Profile').click();
43+
await page.getByRole('button', { name: 'User Profile' }).click();
4444
await page.getByRole('link', { name }).click();
4545
const formattedName = name.toLowerCase().replace(/\s+/g, '-');
4646
await expect(page).toHaveURL(new RegExp(`/${formattedName}`));
@@ -60,7 +60,7 @@ export async function openLists(page: Page, name: string = 'My Lists') {
6060
*/
6161
export async function addMovie(page: Page, movieName: string) {
6262
await test.step('add movie to list', async () => {
63-
await page.getByLabel('Add Item').fill(movieName);
63+
await page.getByRole('textbox', { name: 'Add Item' }).fill(movieName);
6464
await page.getByRole('button', { name: movieName }).click();
6565
await expect(page.getByLabel('movies')).toMatchAriaSnapshot(`
6666
- list "movies" :
@@ -102,7 +102,7 @@ export async function addImageToList(page: Page, movieName: string) {
102102
await openLists(page, 'My Lists');
103103

104104
// Verify the movie list has been updated with the text "my favorite movies"
105-
await expect(page.getByLabel('movie list').getByRole('link')).toHaveText(
105+
await expect(page.getByRole('listitem', { name: 'movie list' }).getByRole('link')).toHaveText(
106106
/my favorite movies/,
107107
);
108108
});

tests/logged-in/login.setup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ setup('log user in and verify profile access', async ({ page }) => {
77

88
// Fill in the username and password fields and submit the form
99
await page
10-
.getByPlaceholder('[email protected]')
10+
.getByRole('textbox', { name: 'Email address' })
1111
.fill(process.env.MOVIES_USERNAME!);
12-
await page.getByPlaceholder('Password').fill(process.env.MOVIES_PASSWORD!);
12+
await page.getByRole('textbox', { name: 'Password' }).fill(process.env.MOVIES_PASSWORD!);
1313
await page.getByRole('button', { name: 'login' }).click();
1414

1515
// Click on the 'User Profile' label to ensure the user is logged in
16-
await page.getByLabel('User Profile').click();
16+
await page.getByRole('button', { name: 'User Profile' }).click();
1717

1818
// Verify that the 'Create New List' link is visible
1919
await expect(

tests/logged-in/manage-lists-before-each.spec.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ test('should create multiple lists', async ({ page }) => {
2626
await openLists(page);
2727

2828
// Verify that two lists have been created
29-
await expect.soft(movieList).toHaveCount(2);
30-
await expect.soft(movieList).toHaveText([/my favorite movies/, /need to watch/]);
29+
await expect(movieList).toHaveText([/my favorite movies/, /need to watch/]);
3130
});
3231

3332
test('should edit an existing list', async ({ page }) => {
@@ -38,14 +37,13 @@ test('should edit an existing list', async ({ page }) => {
3837
await page.getByRole('link', { name: 'Edit' }).click();
3938

4039
await test.step('update list name and description', async () => {
41-
await page.getByLabel('Name').fill('my action movies');
42-
await page.getByLabel('Description').fill('my favorite action movies');
40+
await page.getByRole('textbox', { name: 'Name' }).fill('my action movies');
41+
await page.getByRole('textbox', { name: 'Description' }).fill('my favorite action movies');
4342
await page.getByRole('button', { name: 'Save' }).click();
4443

4544
// Verify that the list name and description have been updated
46-
await expect.soft(page.getByLabel('Name')).toHaveValue('my action movies');
47-
await expect
48-
.soft(page.getByLabel('Description'))
45+
await expect(page.getByRole('textbox', { name: 'Name' })).toHaveValue('my action movies');
46+
await expect(page.getByRole('textbox', { name: 'Description' }))
4947
.toHaveValue('my favorite action movies');
5048
await page.getByRole('button', { name: 'Save' }).click();
5149
});
@@ -55,12 +53,10 @@ test('should edit an existing list', async ({ page }) => {
5553
await page.getByRole('link', { name: 'View List' }).click();
5654

5755
// Verify that the list heading contains the updated name and description
58-
await expect
59-
.soft(page.getByRole('heading', { level: 1 }))
60-
.toHaveText('my action movies');
61-
await expect.soft(page.getByRole('heading', { level: 2 })).toHaveText(
62-
'my favorite action movies',
63-
);
56+
await expect(page.locator('main')).toMatchAriaSnapshot(`
57+
- heading "my action movies" [level=1]
58+
- heading "my favorite action movies" [level=2]
59+
`);
6460
});
6561
});
6662

@@ -108,22 +104,22 @@ test('should add an image to a list', async ({ page }) => {
108104

109105
await test.step('choose and verify image for the list', async () => {
110106
// Verify that the movie list heading contains the text "Twisters"
111-
await expect.soft(movie.getByRole('heading')).toHaveText('Twisters');
107+
await expect(movie.getByRole('heading')).toHaveText('Twisters');
112108

113109
// Hover over the movie list item and select image
114110
await movie.hover();
115111
await movie.getByRole('heading', { name: 'SELECT' }).click();
116112

117113
// Verify that the button text has changed to "SELECTED"
118-
await expect.soft(movie.getByRole('button')).toHaveText('SELECTED');
114+
await expect(movie.getByRole('button')).toHaveText('SELECTED');
119115
});
120116

121117
// Navigate back to the the "My Lists" section of the user profile
122-
await page.getByLabel('User Profile').click();
118+
await page.getByRole('button', { name: 'User Profile' }).click();
123119
await page.getByRole('link', { name: 'My Lists' }).click();
124120

125121
// Verify the movie list has been updated with the text "my favorite movies"
126-
await expect.soft(page.getByLabel('movie list').getByRole('link')).toHaveText(
122+
await expect(page.getByRole('listitem', { name: 'movie list' }).getByRole('link')).toHaveText(
127123
/my favorite movies/,
128124
);
129125
});
@@ -134,20 +130,18 @@ test('should share a list', async ({ page }) => {
134130

135131
// Click on the "Share" button and verify the share dialog is displayed
136132
await page.getByRole('button', { name: 'Share' }).click();
137-
await expect
138-
.soft(page.getByRole('dialog').getByRole('heading'))
133+
await expect(page.getByRole('dialog').getByRole('heading'))
139134
.toHaveText('Share my favorite movies');
140135

141136
// Verify that the URL input in the dialog contains a value with "list"
142-
await expect
143-
.soft(page.getByRole('dialog').getByLabel('URL'))
137+
await expect(page.getByRole('dialog').getByLabel('URL'))
144138
.toHaveValue(/list/);
145139

146140
// Close the dialog by clicking outside of it
147141
await page.locator('body').click({ position: { x: 0, y: 0 } });
148142

149143
// Verify that the dialog is no longer visible
150-
await expect.soft(page.getByRole('dialog')).not.toBeVisible();
144+
await expect(page.getByRole('dialog')).not.toBeVisible();
151145
});
152146

153147
test(
@@ -171,9 +165,8 @@ test(
171165
await page.getByRole('button', { name: 'Yes' }).click();
172166

173167
// Verify that the list has been deleted
174-
await expect
175-
.soft(page.getByRole('heading', { level: 3 }))
168+
await expect(page.getByRole('heading', { level: 3 }))
176169
.toHaveText(/no lists/);
177-
await expect.soft(page.getByRole('listitem', { name: 'movie' })).toHaveCount(0);
170+
await expect(page.getByRole('listitem', { name: 'movie' })).toHaveCount(0);
178171
},
179172
);

tests/logged-in/manage-lists-fixtures.spec.ts

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@ test('editing an existing list', async ({ listPage }) => {
99
await page.getByRole('link', { name: 'Edit' }).click();
1010

1111
await test.step('update list name and description', async () => {
12-
await page.getByLabel('Name').fill('my action movies');
13-
await page.getByLabel('Description').fill('my favorite action movies');
12+
await page.getByRole('textbox', { name: 'Name' }).fill('my action movies');
13+
await page.getByRole('textbox', { name: 'Description' }).fill('my favorite action movies');
1414
await page.getByRole('button', { name: 'Save' }).click();
1515

1616
// Verify that the list name and description have been updated
17-
await expect.soft(page.getByLabel('Name')).toHaveValue('my action movies');
18-
await expect
19-
.soft(page.getByLabel('Description'))
17+
await expect(page.getByRole('textbox', { name: 'Name' })).toHaveValue('my action movies');
18+
await expect(page.getByRole('textbox', { name: 'Description' }))
2019
.toHaveValue('my favorite action movies');
20+
// TODO: replace regex with text when Playwright is rolled.
21+
await expect(page.locator('main')).toMatchAriaSnapshot(`
22+
- heading "my action movies" [level=1]
23+
- textbox "Name": my action movies
24+
- textbox "Description": /my favorite action movies/
25+
`);
2126
await page.getByRole('button', { name: 'Save' }).click();
2227
});
2328

2429
await test.step('verify updated list name and description on my list page', async () => {
2530
await page.getByRole('link', { name: 'View List' }).click();
26-
await expect
27-
.soft(page.getByRole('heading', { level: 1 }))
28-
.toHaveText('my action movies');
29-
await expect.soft(page.getByRole('heading').nth(1)).toHaveText(
30-
'my favorite action movies',
31-
);
31+
await expect(page.getByRole('main')).toMatchAriaSnapshot(`
32+
- heading "my action movies" [level=1]
33+
- heading "my favorite action movies" [level=2]
34+
`);
3235
});
3336
});
3437

@@ -41,55 +44,54 @@ test('adding movies to a list', async ({ listPage }) => {
4144
await addMovie(page, 'Gunner');
4245

4346
// Verify that the movies have been added to the list
44-
await expect.soft(page.getByRole('listitem', { name: 'movie' })).toHaveText([
45-
/Twisters/,
46-
/The Garfield Movie/,
47-
/Bad Boys: Ride or Die/,
48-
/Inside Out 2/,
49-
/Gunner/,
50-
]);
47+
await expect(page.getByRole('list', { name: 'movies' })).toMatchAriaSnapshot(`
48+
- listitem: "Twisters"
49+
- listitem: "The Garfield Movie"
50+
- listitem: "Bad Boys: Ride or Die"
51+
- listitem: "Inside Out 2"
52+
- listitem: "Gunner"
53+
`);
5154
});
5255

5356
test('deleting movies from a list', async ({ listPage }) => {
5457
// set the page to the listPage fixture
5558
const page = listPage;
5659

5760
await page.getByRole('button', { name: 'Add/Remove Movies' }).click();
58-
await expect
59-
.soft(page.getByRole('listitem', { name: 'movie' }))
60-
.toHaveText([/Twisters/, /The Garfield Movie/, /Bad Boys: Ride or Die/]);
61+
await expect(page.getByRole('list', { name: 'movies' })).toMatchAriaSnapshot(`
62+
- listitem: "Twisters"
63+
- listitem: "The Garfield Movie"
64+
- listitem: "Bad Boys: Ride or Die"
65+
`);
6166

6267
await test.step('delete the second movie from list', async () => {
6368
const movie2 = page
6469
.getByRole('listitem')
6570
.filter({ hasText: /The Garfield Movie/ });
66-
await movie2.getByLabel('Remove').click();
71+
await movie2.getByRole('button', { name: 'Remove' }).click();
6772
});
6873

69-
await expect.soft(page.getByRole('listitem', { name: 'movie' })).toHaveText([
70-
/Twisters/,
71-
/Bad Boys: Ride or Die/,
72-
]);
74+
await expect(page.getByRole('list', { name: 'movies' })).toMatchAriaSnapshot(`
75+
- listitem: "Twisters"
76+
- listitem: "Bad Boys: Ride or Die"
77+
`);
78+
await expect(page.getByRole('listitem', { name: 'movie' })).toHaveCount(2);
7379
});
7480

7581
test('sharing a list', async ({ listPage }) => {
7682
// set the page to the listPage fixture
7783
const page = listPage;
7884

7985
await page.getByRole('button', { name: 'Share' }).click();
80-
await expect
81-
.soft(page.getByRole('dialog').getByRole('heading'))
82-
.toHaveText('Share my favorite movies');
83-
84-
// Verify that the URL input in the dialog contains a value with "list"
85-
await expect
86-
.soft(page.getByRole('dialog').getByLabel('URL'))
87-
.toHaveValue(/list/);
86+
await expect(page.getByRole('dialog')).toMatchAriaSnapshot(`
87+
- heading "Share my favorite movies" [level=2]
88+
- textbox "URL": /list/
89+
`);
8890

8991
await test.step('close the share dialog', async () => {
9092
// Close the dialog by clicking outside of it
9193
await page.locator('body').click({ position: { x: 0, y: 0 } });
92-
await expect.soft(page.getByRole('dialog')).not.toBeVisible();
94+
await expect(page.getByRole('dialog')).not.toBeVisible();
9395
});
9496
});
9597

@@ -101,14 +103,13 @@ test('deleting a list', async ({ listPage }) => {
101103
await page.getByRole('link', { name: 'Delete List' }).click();
102104

103105
await test.step('confirm the deletion', async () => {
104-
await page.getByLabel(/Click the button below/).click();
106+
await page.getByRole('button', { name: /Click the button below/ }).click();
105107
await page.getByRole('button', { name: 'Yes' }).click();
106108
});
107109

108110
// Verify that the list has been deleted
109-
await expect.soft(page).toHaveURL(/my-lists/);
110-
await expect
111-
.soft(page.getByRole('heading', { level: 3 }))
111+
await expect(page).toHaveURL(/my-lists/);
112+
await expect(page.getByRole('heading', { level: 3 }))
112113
.toHaveText(/no lists/);
113-
await expect.soft(page.getByRole('listitem', { name: 'movie' })).toHaveCount(0);
114+
await expect(page.getByRole('listitem', { name: 'movie' })).toHaveCount(0);
114115
});

0 commit comments

Comments
 (0)