Skip to content

Commit 0695cfc

Browse files
committed
Make tests with mouse more robust
1 parent bd45dd7 commit 0695cfc

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

src/actions/enter.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ export class EnterAction {
8888
return this.handleEnterForWS(workspace);
8989
case Constants.STATE.FLYOUT:
9090
if (targetWorkspace.isReadOnly()) return false;
91-
flyoutCursor = this.navigation.getFlyoutCursor(
92-
targetWorkspace,
93-
);
91+
flyoutCursor = this.navigation.getFlyoutCursor(targetWorkspace);
9492
if (!flyoutCursor) {
9593
return false;
9694
}

test/webdriverio/test/keyboard_mode_test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
PAUSE_TIME,
1414
getBlockElementById,
1515
tabNavigateToWorkspace,
16+
clickBlock,
1617
} from './test_setup.js';
1718
import {Key} from 'webdriverio';
1819

@@ -125,8 +126,7 @@ suite(
125126

126127
await this.browser.pause(PAUSE_TIME);
127128
// Right click a block
128-
const element = await getBlockElementById(this.browser, 'controls_if_1');
129-
await element.click({button: 'right'});
129+
clickBlock(this.browser, 'controls_if_1', {button: 'right'});
130130
await this.browser.pause(PAUSE_TIME);
131131

132132
chai.assert.isFalse(await isKeyboardNavigating(this.browser));
@@ -140,6 +140,15 @@ suite(
140140
await this.browser.pause(PAUSE_TIME);
141141
// Drag a block
142142
const element = await getBlockElementById(this.browser, 'controls_if_1');
143+
144+
await this.browser.execute(() => {
145+
const ws = Blockly.getMainWorkspace() as Blockly.WorkspaceSvg;
146+
const block = ws.getBlockById('controls_if_1') as Blockly.BlockSvg;
147+
ws.scrollBoundsIntoView(
148+
block.getBoundingRectangleWithoutChildren(),
149+
10,
150+
);
151+
});
143152
await element.dragAndDrop({x: 10, y: 10});
144153
await this.browser.pause(PAUSE_TIME);
145154

test/webdriverio/test/test_setup.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,59 @@ export async function contextMenuExists(
555555
const item = await browser.$(`div=${itemText}`);
556556
return await item.waitForExist({timeout: 200, reverse: reverse});
557557
}
558+
559+
/**
560+
* Find a clickable element on the block and click it.
561+
* We can't always use the block's SVG root because clicking will always happen
562+
* in the middle of the block's bounds (including children) by default, which
563+
* causes problems if it has holes (e.g. statement inputs). Instead, this tries
564+
* to get the first text field on the block. It falls back on the block's SVG root.
565+
*
566+
* @param browser The active WebdriverIO Browser object.
567+
* @param blockId The id of the block to click, as an interactable element.
568+
* @param clickOptions The options to pass to webdriverio's element.click function.
569+
* @return A Promise that resolves when the actions are completed.
570+
*/
571+
export async function clickBlock(
572+
browser: WebdriverIO.Browser,
573+
blockId: string,
574+
clickOptions?: Partial<webdriverio.ClickOptions> | undefined,
575+
) {
576+
const findableId = 'clickTargetElement';
577+
// In the browser context, find the element that we want and give it a findable ID.
578+
await browser.execute(
579+
(blockId, newElemId) => {
580+
const ws = Blockly.getMainWorkspace() as Blockly.WorkspaceSvg;
581+
const block = ws.getBlockById(blockId) as Blockly.BlockSvg;
582+
// Ensure the block we want to click is within the viewport.
583+
ws.scrollBoundsIntoView(block.getBoundingRectangleWithoutChildren(), 10);
584+
if (!block.isCollapsed()) {
585+
for (const input of block.inputList) {
586+
for (const field of input.fieldRow) {
587+
if (field instanceof Blockly.FieldLabel) {
588+
const svgRoot = field.getSvgRoot();
589+
if (svgRoot) {
590+
svgRoot.id = newElemId;
591+
return;
592+
}
593+
}
594+
}
595+
}
596+
}
597+
// No label field found. Fall back to the block's SVG root.
598+
block.getSvgRoot().id = newElemId;
599+
},
600+
blockId,
601+
findableId,
602+
);
603+
604+
// In the test context, get the Webdriverio Element that we've identified.
605+
const elem = await browser.$(`#${findableId}`);
606+
607+
await elem.click(clickOptions);
608+
609+
// In the browser context, remove the ID.
610+
await browser.execute((elemId) => {
611+
document.getElementById(elemId)?.removeAttribute('id');
612+
}, findableId);
613+
}

0 commit comments

Comments
 (0)