Skip to content

Commit 67f42f0

Browse files
chore(tests): rewrite getClickableBlockElement as clickBlock
1 parent 5ee26c9 commit 67f42f0

2 files changed

Lines changed: 40 additions & 49 deletions

File tree

tests/browser/test/delete_blocks_test.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
testFileLocations,
1111
getAllBlocks,
1212
getBlockElementById,
13-
getClickableBlockElement,
13+
clickBlock,
1414
contextMenuSelect,
1515
PAUSE_TIME,
1616
} = require('./test_setup');
@@ -137,11 +137,7 @@ suite('Delete blocks', function (done) {
137137
test('Delete block using backspace key', async function () {
138138
const before = (await getAllBlocks(this.browser)).length;
139139
// Get first print block, click to select it, and delete it using backspace key.
140-
const clickEl = await getClickableBlockElement(
141-
this.browser,
142-
this.firstBlock,
143-
);
144-
await clickEl.click();
140+
await clickBlock(this.browser, this.firstBlock, {button: 1});
145141
await this.browser.keys([Key.Backspace]);
146142
const after = (await getAllBlocks(this.browser)).length;
147143
chai.assert.equal(
@@ -154,11 +150,7 @@ suite('Delete blocks', function (done) {
154150
test('Delete block using delete key', async function () {
155151
const before = (await getAllBlocks(this.browser)).length;
156152
// Get first print block, click to select it, and delete it using delete key.
157-
const clickEl = await getClickableBlockElement(
158-
this.browser,
159-
this.firstBlock,
160-
);
161-
await clickEl.click();
153+
await clickBlock(this.browser, this.firstBlock, {button: 1});
162154
await this.browser.keys([Key.Delete]);
163155
const after = (await getAllBlocks(this.browser)).length;
164156
chai.assert.equal(
@@ -183,11 +175,7 @@ suite('Delete blocks', function (done) {
183175
test('Undo block deletion', async function () {
184176
const before = (await getAllBlocks(this.browser)).length;
185177
// Get first print block, click to select it, and delete it using backspace key.
186-
const clickEl = await getClickableBlockElement(
187-
this.browser,
188-
this.firstBlock,
189-
);
190-
await clickEl.click();
178+
await clickBlock(this.browser, this.firstBlock, {button: 1});
191179
await this.browser.keys([Key.Backspace]);
192180
await this.browser.pause(PAUSE_TIME);
193181
// Undo
@@ -203,11 +191,7 @@ suite('Delete blocks', function (done) {
203191
test('Redo block deletion', async function () {
204192
const before = (await getAllBlocks(this.browser)).length;
205193
// Get first print block, click to select it, and delete it using backspace key.
206-
const clickEl = await getClickableBlockElement(
207-
this.browser,
208-
this.firstBlock,
209-
);
210-
await clickEl.click();
194+
await clickBlock(this.browser, this.firstBlock, {button: 1});
211195
await this.browser.keys([Key.Backspace]);
212196
await this.browser.pause(PAUSE_TIME);
213197
// Undo

tests/browser/test/test_setup.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
468480
async 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

Comments
 (0)