@@ -156,35 +156,47 @@ async function getBlockElementById(browser, id) {
156156}
157157
158158/**
159- * Get a clickable element on the block. We can't always use the block's SVG root
160- * because clicking will always happen in the middle of the block's bounds
161- * (including children) by default, which causes problems if it has holes
162- * (e.g. statement inputs). Instead, this tries to get the first text field on the
163- * block. It falls back on the block's SVG root.
159+ * Find a clickable element on the block and click it.
160+ * We can't always use the block's SVG root because clicking will always happen
161+ * in the middle of the block's bounds (including children) by default, which
162+ * causes problems if it has holes (e.g. statement inputs). Instead, this tries
163+ * to get the first text field on the block. It falls back on the block's SVG root.
164164 * @param browser The active WebdriverIO Browser object.
165165 * @param block The block to click, as an interactable element.
166- * @return A Promise that resolves to the text element of the first label
167- * field on the block, or the block's SVG root if no label field was found .
166+ * @param clickOptions The options to pass to webdriverio's element.click function.
167+ * @return A Promise that resolves when the actions are completed .
168168 */
169- async function getClickableBlockElement ( browser , block ) {
169+ async function clickBlock ( browser , block , clickOptions ) {
170+ const findableId = 'clickTargetElement' ;
170171 // In the browser context, find the element that we want and give it a findable ID.
171- await browser . execute ( ( blockId ) => {
172- const block = Blockly . getMainWorkspace ( ) . getBlockById ( blockId ) ;
173- for ( const input of block . inputList ) {
174- for ( const field of input . fieldRow ) {
175- if ( field instanceof Blockly . FieldLabel ) {
176- field . getSvgRoot ( ) . id = 'clickTargetElement' ;
177- return ;
172+ await browser . execute (
173+ ( blockId , newElemId ) => {
174+ const block = Blockly . getMainWorkspace ( ) . getBlockById ( blockId ) ;
175+ for ( const input of block . inputList ) {
176+ for ( const field of input . fieldRow ) {
177+ if ( field instanceof Blockly . FieldLabel ) {
178+ field . getSvgRoot ( ) . id = newElemId ;
179+ return ;
180+ }
178181 }
179182 }
180- }
181- // No label field found. Fall back to the block's SVG root.
182- block . getSvgRoot ( ) . id = 'clickTargetElement' ;
183- } , block . id ) ;
183+ // No label field found. Fall back to the block's SVG root.
184+ block . getSvgRoot ( ) . id = findableId ;
185+ } ,
186+ block . id ,
187+ findableId ,
188+ ) ;
184189
185190 // In the test context, get the Webdriverio Element that we've identified.
186- const elem = await browser . $ ( '#clickTargetElement' ) ;
187- return elem ;
191+ const elem = await browser . $ ( `#${ findableId } ` ) ;
192+
193+ await elem . click ( clickOptions ) ;
194+
195+ // In the browser context, remove the ID.
196+ await browser . execute ( ( elemId ) => {
197+ const clickElem = document . getElementById ( elemId ) ;
198+ clickElem . removeAttribute ( 'id' ) ;
199+ } , findableId ) ;
188200}
189201
190202/**
@@ -466,12 +478,7 @@ async function dragBlockFromMutatorFlyout(browser, mutatorBlock, type, x, y) {
466478 * @return A Promise that resolves when the actions are completed.
467479 */
468480async function contextMenuSelect ( browser , block , itemText ) {
469- const clickEl = await getClickableBlockElement ( browser , block ) ;
470- // Even though the element should definitely already exist,
471- // one specific test breaks if you remove this...
472- await clickEl . waitForExist ( ) ;
473-
474- await clickEl . click ( { button : 2 } ) ;
481+ await clickBlock ( browser , block , { button : 2 } ) ;
475482
476483 const item = await browser . $ ( `div=${ itemText } ` ) ;
477484 await item . waitForExist ( ) ;
@@ -542,7 +549,7 @@ module.exports = {
542549 getSelectedBlockElement,
543550 getSelectedBlockId,
544551 getBlockElementById,
545- getClickableBlockElement ,
552+ clickBlock ,
546553 getCategory,
547554 getNthBlockOfCategory,
548555 getBlockTypeFromCategory,
0 commit comments