@@ -1113,6 +1113,22 @@ export class App {
11131113 await result [ 0 ] . click ( ) ;
11141114 }
11151115
1116+ async tabOutOfEditorForwards ( ) : Promise < void > {
1117+ const content = await this . focusEditorContent ( ) ;
1118+ await content . press ( "Escape" ) ;
1119+ await content . press ( "Tab" ) ;
1120+ }
1121+
1122+ async tabOutOfEditorBackwards ( ) : Promise < void > {
1123+ const keyboard = ( await this . page ) . keyboard ;
1124+
1125+ const content = await this . focusEditorContent ( ) ;
1126+ await content . press ( "Escape" ) ;
1127+ await keyboard . down ( "Shift" ) ;
1128+ await content . press ( "Tab" ) ;
1129+ await keyboard . up ( "Shift" ) ;
1130+ }
1131+
11161132 private async document ( ) : Promise < puppeteer . ElementHandle < Element > > {
11171133 const page = await this . page ;
11181134 return page . getDocument ( ) ;
@@ -1163,12 +1179,6 @@ export class App {
11631179 await keyboard . press ( key ) ;
11641180 }
11651181
1166- private async getActiveElement ( ) : Promise < puppeteer . ElementHandle < Element > > {
1167- return ( await this . page ) . evaluateHandle < ElementHandle > (
1168- ( ) => document . activeElement
1169- ) ;
1170- }
1171-
11721182 private async getElementByRoleAndLabel (
11731183 role : string ,
11741184 name : string
@@ -1187,38 +1197,89 @@ export class App {
11871197 ) ;
11881198 }
11891199
1190- private async compareElementHandles (
1191- e1 : puppeteer . ElementHandle < Element > ,
1192- e2 : puppeteer . ElementHandle < Element >
1193- ) : Promise < boolean > {
1194- return ( await this . page ) . evaluate ( ( e1 , e2 ) => e1 === e2 , e1 , e2 ) ;
1200+ async assertActiveElement (
1201+ accessExpectedElement : ( ) => Promise < puppeteer . ElementHandle < Element > >
1202+ ) {
1203+ return waitFor ( async ( ) => {
1204+ const page = await this . page ;
1205+ const expectedElement = await accessExpectedElement ( ) ;
1206+
1207+ expect (
1208+ await page . evaluate ( ( e ) => {
1209+ return e === document . activeElement ;
1210+ } , expectedElement )
1211+ ) . toEqual ( true ) ;
1212+ } , defaultWaitForOptions ) ;
11951213 }
11961214
1197- async assertFocusOnLoad ( ) : Promise < boolean > {
1215+ async assertFocusOnLoad ( ) : Promise < void > {
11981216 await this . keyboardPress ( "Tab" ) ;
1199- const activeElement = await this . getActiveElement ( ) ;
1200- const firstFocusableElement = await this . getElementByRoleAndLabel (
1201- "link" ,
1202- "visit microbit.org (opens in a new tab)"
1217+ return this . assertActiveElement ( ( ) =>
1218+ this . getElementByRoleAndLabel (
1219+ "link" ,
1220+ "visit microbit.org (opens in a new tab)"
1221+ )
1222+ ) ;
1223+ }
1224+
1225+ collapseSimulator ( ) : Promise < void > {
1226+ return this . findAndClickButton ( "Collapse simulator" ) ;
1227+ }
1228+
1229+ expandSimulator ( ) : Promise < void > {
1230+ return this . findAndClickButton ( "Expand simulator" ) ;
1231+ }
1232+
1233+ collapseSidebar ( ) : Promise < void > {
1234+ return this . findAndClickButton ( "Collapse sidebar" ) ;
1235+ }
1236+
1237+ expandSidebar ( ) : Promise < void > {
1238+ return this . findAndClickButton ( "Expand sidebar" ) ;
1239+ }
1240+
1241+ async assertFocusOnExpandSimulator ( ) : Promise < void > {
1242+ const document = await this . document ( ) ;
1243+ return this . assertActiveElement ( ( ) =>
1244+ document . getByRole ( "button" , { name : "Expand simulator" } )
1245+ ) ;
1246+ }
1247+
1248+ assertFocusOnSimulator ( ) : Promise < void > {
1249+ return this . assertActiveElement ( ( ) =>
1250+ this . getElementByQuerySelector ( "iframe[name='Simulator']" )
1251+ ) ;
1252+ }
1253+
1254+ async assertFocusOnExpandSidebar ( ) : Promise < void > {
1255+ const document = await this . document ( ) ;
1256+ return this . assertActiveElement ( ( ) =>
1257+ document . findByRole ( "button" , { name : "Expand sidebar" } )
1258+ ) ;
1259+ }
1260+
1261+ assertFocusOnSidebar ( ) : Promise < void > {
1262+ return this . assertActiveElement ( ( ) =>
1263+ this . getElementByQuerySelector ( "[role='tabpanel']" )
1264+ ) ;
1265+ }
1266+
1267+ async assertFocusBeforeEditor ( ) : Promise < void > {
1268+ const document = await this . document ( ) ;
1269+ return this . assertActiveElement ( ( ) =>
1270+ document . findByRole ( "button" , {
1271+ name : "Zoom in" ,
1272+ } )
1273+ ) ;
1274+ }
1275+
1276+ async assertFocusAfterEditor ( ) : Promise < void > {
1277+ const document = await this . document ( ) ;
1278+ return this . assertActiveElement ( ( ) =>
1279+ document . findByRole ( "button" , {
1280+ name : "Send to micro:bit" ,
1281+ } )
12031282 ) ;
1204- return this . compareElementHandles ( activeElement , firstFocusableElement ) ;
1205- }
1206-
1207- async assertFocusOnAreaToggle (
1208- action : "Collapse" | "Expand" ,
1209- area : "simulator" | "sidebar"
1210- ) : Promise < boolean > {
1211- await this . findAndClickButton ( `${ action } ${ area } ` ) ;
1212- const activeElement = await this . getActiveElement ( ) ;
1213- const proposedActiveElement =
1214- action === "Collapse"
1215- ? await this . getElementByRoleAndLabel ( "button" , `Expand ${ area } ` )
1216- : await this . getElementByQuerySelector (
1217- area === "simulator"
1218- ? "iframe[name='Simulator']"
1219- : "[role='tabpanel']"
1220- ) ;
1221- return this . compareElementHandles ( activeElement , proposedActiveElement ) ;
12221283 }
12231284
12241285 // Simulator functions
0 commit comments