/
navigation.spec.js
119 lines (105 loc) · 3.41 KB
/
navigation.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
test.use( {
editorNavigationUtils: async ( { page, pageUtils }, use ) => {
await use( new EditorNavigationUtils( { page, pageUtils } ) );
},
} );
test.describe( 'Site editor navigation', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'emptytheme' );
} );
test.afterAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyone' );
} );
test( 'Can use keyboard to navigate the site editor', async ( {
admin,
editorNavigationUtils,
page,
pageUtils,
} ) => {
await admin.visitSiteEditor();
// Test: Can navigate to a sidebar item and into its subnavigation frame without losing focus
// Go to the Pages button
await editorNavigationUtils.tabToLabel( 'Pages' );
await expect(
page.getByRole( 'button', { name: 'Pages' } )
).toBeFocused();
await pageUtils.pressKeys( 'Enter' );
// We should be in the Pages sidebar
await expect(
page.getByRole( 'button', { name: 'Back', exact: true } )
).toBeFocused();
await pageUtils.pressKeys( 'Enter' );
// Go back to the main navigation
await expect(
page.getByRole( 'button', { name: 'Pages' } )
).toBeFocused();
// Test: Can navigate into the iframe using the keyboard
await editorNavigationUtils.tabToLabel( 'Editor Canvas' );
const editorCanvasButton = page.getByRole( 'button', {
name: 'Editor Canvas',
} );
await expect( editorCanvasButton ).toBeFocused();
// Enter into the site editor frame
await pageUtils.pressKeys( 'Enter' );
// Focus should be on the iframe without the button role
await expect(
page.locator( 'iframe[name="editor-canvas"]' )
).toBeFocused();
// The button role should have been removed from the iframe.
await expect( editorCanvasButton ).toBeHidden();
// Test to make sure a Tab keypress works as expected.
// As of this writing, we are in select mode and a tab
// keypress will reveal the header template select mode
// button. This test is not documenting that we _want_
// that action, but checking that we are within the site
// editor and keypresses work as intened.
await pageUtils.pressKeys( 'Tab' );
await expect(
page.getByRole( 'button', {
name: 'Template Part Block. Row 1. header',
} )
).toBeFocused();
// Test: We can go back to the main navigation from the editor frame
// Move to the document toolbar
await pageUtils.pressKeys( 'alt+F10' );
// Go to the open navigation button
await pageUtils.pressKeys( 'shift+Tab' );
// Open the sidebar again
await expect(
page.getByRole( 'button', {
name: 'Open Navigation',
exact: true,
} )
).toBeFocused();
await pageUtils.pressKeys( 'Enter' );
await expect(
page.getByLabel( 'Go to the Dashboard' ).first()
).toBeFocused();
// We should have our editor canvas button back
await expect( editorCanvasButton ).toBeVisible();
} );
} );
class EditorNavigationUtils {
constructor( { page, pageUtils } ) {
this.page = page;
this.pageUtils = pageUtils;
}
async tabToLabel( label, times = 10 ) {
for ( let i = 0; i < times; i++ ) {
await this.pageUtils.pressKeys( 'Tab' );
const activeLabel = await this.page.evaluate( () => {
return (
document.activeElement.getAttribute( 'aria-label' ) ||
document.activeElement.textContent
);
} );
if ( activeLabel === label ) {
return;
}
}
}
}