Skip to content

Commit 9e83306

Browse files
committed
Build/Test Tools: Use user-facing locators in E2E tests.
Many E2E specs found elements by CSS class, by position with `.first()`, or by counting Tab presses. After a markup change, a test could act on the wrong element or fail far from the real problem. Most of these now use role, label and text locators. The media grid specs find tiles by attachment name, so they check that the uploaded file is there rather than any finished tile. Upload errors are found by file name. The Quick Edit test keeps its keyboard path but checks that the Quick Edit button has focus before pressing Enter. The bulk-action and Save Menu controls use their IDs, since the top and bottom controls share an accessible name. Developed in: #15513 Props adrianmoldovanwp. See #66455. git-svn-id: https://develop.svn.wordpress.org/trunk@65275 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1288084 commit 9e83306

13 files changed

Lines changed: 281 additions & 220 deletions

‎tests/e2e/specs/classic-menus.test.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ test.describe( 'Classic Menus', () => {
2525
name: 'Menu Name',
2626
} );
2727
await menuNameField.fill( 'E2E Menu' );
28-
await page
29-
.getByRole( 'button', { name: 'Create Menu' } )
30-
.first()
31-
.click();
28+
await page.locator( '#save_menu_footer' ).click();
3229

3330
// The screen reloads into the edit state for the new menu.
3431
await expect(
@@ -68,7 +65,7 @@ test.describe( 'Classic Menus', () => {
6865
// Activate via keyboard: the sticky footer bar can shift on focus
6966
// and swallow the first pointer click at certain window heights.
7067
// See https://core.trac.wordpress.org/ticket/65684.
71-
await page.getByRole( 'button', { name: 'Save Menu' } ).first().focus();
68+
await page.locator( '#save_menu_footer' ).focus();
7269
await page.keyboard.press( 'Enter' );
7370

7471
// The location assignment persists.

‎tests/e2e/specs/dashboard.test.js‎

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,31 @@ test.describe( 'Quick Draft', () => {
4949

5050
// Check that the new draft title appears in the 'Your Recent Drafts' section.
5151
await expect(
52-
page.locator( '.drafts .draft-title' ).first().getByRole( 'link' )
52+
page.getByRole( 'link', {
53+
name: 'Edit “Quick Draft test title”',
54+
exact: true,
55+
} )
5356
).toHaveText( 'Quick Draft test title' );
5457

5558
// Check that the new draft content appears in the 'Your Recent Drafts' section.
5659
await expect(
57-
page.locator( '.drafts .draft-content' ).first()
58-
).toHaveText( 'Quick Draft test content' );
60+
page.getByText( 'Quick Draft test content', { exact: true } )
61+
).toBeVisible();
5962

6063
// Check that the new draft appears in the Posts page.
6164
await admin.visitAdminPage( '/edit.php' );
6265

6366
await expect(
64-
page.locator( '.type-post.status-draft .title' ).first()
65-
).toContainText( 'Quick Draft test title' );
67+
page
68+
.getByRole( 'row' )
69+
.filter( {
70+
has: page.getByRole( 'link', {
71+
name: 'Quick Draft test title',
72+
exact: true,
73+
} ),
74+
} )
75+
.getByText( 'Draft', { exact: true } )
76+
).toBeVisible();
6677
} );
6778

6879
test( 'should prevent Quick Draft from being created without Title and Content', async ( {
@@ -88,8 +99,8 @@ test.describe( 'Quick Draft', () => {
8899
await admin.visitAdminPage( '/edit.php' );
89100

90101
await expect(
91-
page.locator( '#the-list .no-items .colspanchange' )
92-
).toContainText( 'No posts found.' );
102+
page.getByRole( 'cell', { name: 'No posts found.' } )
103+
).toBeVisible();
93104
} );
94105

95106
test( 'should allow Quick Draft to be created with only the Title', async ( {
@@ -118,15 +129,26 @@ test.describe( 'Quick Draft', () => {
118129

119130
// Check that the new draft title appears in the 'Your Recent Drafts' section.
120131
await expect(
121-
page.locator( '.drafts .draft-title' ).first().getByRole( 'link' )
132+
page.getByRole( 'link', {
133+
name: 'Edit “Quick Draft test title”',
134+
exact: true,
135+
} )
122136
).toHaveText( 'Quick Draft test title' );
123137

124138
// Check that the new draft appears in the Posts page.
125139
await admin.visitAdminPage( '/edit.php' );
126140

127141
await expect(
128-
page.locator( '.type-post.status-draft .title' ).first()
129-
).toContainText( 'Quick Draft test title' );
142+
page
143+
.getByRole( 'row' )
144+
.filter( {
145+
has: page.getByRole( 'link', {
146+
name: 'Quick Draft test title',
147+
exact: true,
148+
} ),
149+
} )
150+
.getByText( 'Draft', { exact: true } )
151+
).toBeVisible();
130152
} );
131153

132154
test( 'should allow Quick Draft to be created with only the Content', async ( {
@@ -155,19 +177,24 @@ test.describe( 'Quick Draft', () => {
155177

156178
// Check that the new draft title appears in the 'Your Recent Drafts' section.
157179
await expect(
158-
page.locator( '.drafts .draft-title' ).first().getByRole( 'link' )
180+
page.getByRole( 'link', { name: 'Edit “(no title)”', exact: true } )
159181
).toHaveText( '(no title)' );
160182

161183
await expect(
162-
page.locator( '.drafts .draft-content' ).first()
163-
).toHaveText( 'Quick Draft test content' );
184+
page.getByText( 'Quick Draft test content', { exact: true } )
185+
).toBeVisible();
164186

165187
// Check that the new draft appears in the Posts page.
166188
await admin.visitAdminPage( '/edit.php' );
167189

168190
await expect(
169-
page.locator( '.type-post.status-draft .title' ).first()
170-
).toContainText( '(no title)' );
191+
page
192+
.getByRole( 'row' )
193+
.filter( {
194+
has: page.getByRole( 'link', { name: '(no title)' } ),
195+
} )
196+
.getByText( 'Draft', { exact: true } )
197+
).toBeVisible();
171198
} );
172199
} );
173200

@@ -328,7 +355,7 @@ test.describe( 'Dashboard preferences', () => {
328355

329356
test( 'saves a widget collapse and expansion', async () => {
330357
const widget = dashboard.locator( '#dashboard_right_now' );
331-
const content = widget.locator( '.inside' );
358+
const content = widget.locator( '#wp-version-message' );
332359
const toggle = widget.getByRole( 'button', {
333360
name: 'Show or hide panel',
334361
exact: true,

‎tests/e2e/specs/edit-posts.test.js‎

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ test.describe( 'Edit Posts', () => {
3333
await expect( listTable ).toBeVisible();
3434

3535
// Expect there to be one row in the post list.
36-
const posts = listTable.locator( '.row-title' );
37-
await expect( posts ).toHaveCount( 1 );
36+
await expect(
37+
listTable.locator( '#the-list' ).getByRole( 'row' )
38+
).toHaveCount( 1 );
3839

3940
// Expect the title of the post to be correct.
40-
await expect( posts.first() ).toHaveText( title );
41+
const postLink = listTable.getByRole( 'link', {
42+
name: title,
43+
exact: true,
44+
} );
45+
await expect( postLink ).toBeVisible();
4146

42-
await posts.first().hover();
47+
await postLink.hover();
4348
await listTable.getByRole( 'link', { name: `View “${ title }”`, exact: true } ).click();
4449
await expect( page.getByRole( 'heading', { name: title, exact: true } ) ).toBeVisible();
4550
await expect( page.getByText( content, { exact: true } ) ).toBeVisible();
@@ -87,7 +92,13 @@ test.describe( 'Edit Posts', () => {
8792
await listTable.getByRole( 'link', { name: title, exact: true } ).focus();
8893

8994
// Tab to the Quick Edit button and press Enter to quick edit.
90-
await pageUtils.pressKeys( 'Tab', { times: 2 } )
95+
await pageUtils.pressKeys( 'Tab', { times: 2 } );
96+
await expect(
97+
page.getByRole( 'button', {
98+
name: `Quick edit “${ title }” inline`,
99+
exact: true,
100+
} )
101+
).toBeFocused();
91102
await page.keyboard.press( 'Enter' );
92103

93104
// Type in the currently focused (title) field to modify the title, testing that focus is moved to the input.
@@ -100,18 +111,23 @@ test.describe( 'Edit Posts', () => {
100111
await expect( page.getByRole( 'button', { name: 'Quick Edit' } ) ).toBeVisible();
101112

102113
// Expect there to be one row in the post list.
103-
const posts = listTable.locator( '.row-title' );
104-
await expect( posts ).toHaveCount( 1 );
114+
await expect(
115+
listTable.locator( '#the-list' ).getByRole( 'row' )
116+
).toHaveCount( 1 );
105117

106118
// Expect the title of the post to be correct.
107-
await expect( posts.first() ).toHaveText( `${ title } Edited` );
119+
await expect(
120+
listTable.getByRole( 'link', {
121+
name: `${ title } Edited`,
122+
exact: true,
123+
} )
124+
).toBeVisible();
108125
} );
109126

110127
test( 'allows an existing post to be deleted using the Trash button', async ( {
111128
admin,
112129
editor,
113130
page,
114-
pageUtils
115131
} ) => {
116132
const title = 'Test Title';
117133
await admin.createNewPost( { title } );
@@ -121,12 +137,13 @@ test.describe( 'Edit Posts', () => {
121137
const listTable = page.getByRole( 'table', { name: 'Table ordered by' } );
122138
await expect( listTable ).toBeVisible();
123139

124-
// Focus on the post title link.
125-
await listTable.getByRole( 'link', { name: title, exact: true } ).focus();
126-
127-
// Tab to the Trash button and press Enter to delete the post.
128-
await pageUtils.pressKeys( 'Tab', { times: 3 } )
129-
await page.keyboard.press( 'Enter' );
140+
await listTable.getByRole( 'link', { name: title, exact: true } ).hover();
141+
await listTable
142+
.getByRole( 'link', {
143+
name: `Move “${ title }” to the Trash`,
144+
exact: true,
145+
} )
146+
.click();
130147

131148
await expect(
132149
page.getByRole( 'cell', { name: 'No posts found.' } )
@@ -163,16 +180,9 @@ test( 'bulk edits only the selected posts', async ( {
163180
.check();
164181
}
165182
await page
166-
.getByRole( 'combobox', {
167-
name: 'Select bulk action',
168-
exact: true,
169-
} )
170-
.first()
183+
.locator( '#bulk-action-selector-top' )
171184
.selectOption( 'edit' );
172-
await page
173-
.getByRole( 'button', { name: 'Apply', exact: true } )
174-
.first()
175-
.click();
185+
await page.locator( '#doaction' ).click();
176186
const bulkEdit = page.getByRole( 'region', {
177187
name: 'Bulk Edit',
178188
exact: true,

0 commit comments

Comments
 (0)