chore(tests): rewrite getClickableBlockElement as clickBlock

This commit is contained in:
Rachel Fenichel
2023-09-13 12:36:39 -07:00
parent 5ee26c9051
commit 67f42f017a
2 changed files with 40 additions and 49 deletions

View File

@@ -10,7 +10,7 @@ const {
testFileLocations, testFileLocations,
getAllBlocks, getAllBlocks,
getBlockElementById, getBlockElementById,
getClickableBlockElement, clickBlock,
contextMenuSelect, contextMenuSelect,
PAUSE_TIME, PAUSE_TIME,
} = require('./test_setup'); } = require('./test_setup');
@@ -137,11 +137,7 @@ suite('Delete blocks', function (done) {
test('Delete block using backspace key', async function () { test('Delete block using backspace key', async function () {
const before = (await getAllBlocks(this.browser)).length; const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using backspace key. // Get first print block, click to select it, and delete it using backspace key.
const clickEl = await getClickableBlockElement( await clickBlock(this.browser, this.firstBlock, {button: 1});
this.browser,
this.firstBlock,
);
await clickEl.click();
await this.browser.keys([Key.Backspace]); await this.browser.keys([Key.Backspace]);
const after = (await getAllBlocks(this.browser)).length; const after = (await getAllBlocks(this.browser)).length;
chai.assert.equal( chai.assert.equal(
@@ -154,11 +150,7 @@ suite('Delete blocks', function (done) {
test('Delete block using delete key', async function () { test('Delete block using delete key', async function () {
const before = (await getAllBlocks(this.browser)).length; const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using delete key. // Get first print block, click to select it, and delete it using delete key.
const clickEl = await getClickableBlockElement( await clickBlock(this.browser, this.firstBlock, {button: 1});
this.browser,
this.firstBlock,
);
await clickEl.click();
await this.browser.keys([Key.Delete]); await this.browser.keys([Key.Delete]);
const after = (await getAllBlocks(this.browser)).length; const after = (await getAllBlocks(this.browser)).length;
chai.assert.equal( chai.assert.equal(
@@ -183,11 +175,7 @@ suite('Delete blocks', function (done) {
test('Undo block deletion', async function () { test('Undo block deletion', async function () {
const before = (await getAllBlocks(this.browser)).length; const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using backspace key. // Get first print block, click to select it, and delete it using backspace key.
const clickEl = await getClickableBlockElement( await clickBlock(this.browser, this.firstBlock, {button: 1});
this.browser,
this.firstBlock,
);
await clickEl.click();
await this.browser.keys([Key.Backspace]); await this.browser.keys([Key.Backspace]);
await this.browser.pause(PAUSE_TIME); await this.browser.pause(PAUSE_TIME);
// Undo // Undo
@@ -203,11 +191,7 @@ suite('Delete blocks', function (done) {
test('Redo block deletion', async function () { test('Redo block deletion', async function () {
const before = (await getAllBlocks(this.browser)).length; const before = (await getAllBlocks(this.browser)).length;
// Get first print block, click to select it, and delete it using backspace key. // Get first print block, click to select it, and delete it using backspace key.
const clickEl = await getClickableBlockElement( await clickBlock(this.browser, this.firstBlock, {button: 1});
this.browser,
this.firstBlock,
);
await clickEl.click();
await this.browser.keys([Key.Backspace]); await this.browser.keys([Key.Backspace]);
await this.browser.pause(PAUSE_TIME); await this.browser.pause(PAUSE_TIME);
// Undo // Undo

View File

@@ -156,35 +156,47 @@ async function getBlockElementById(browser, id) {
} }
/** /**
* Get a clickable element on the block. We can't always use the block's SVG root * Find a clickable element on the block and click it.
* because clicking will always happen in the middle of the block's bounds * We can't always use the block's SVG root because clicking will always happen
* (including children) by default, which causes problems if it has holes * in the middle of the block's bounds (including children) by default, which
* (e.g. statement inputs). Instead, this tries to get the first text field on the * causes problems if it has holes (e.g. statement inputs). Instead, this tries
* block. It falls back on the block's SVG root. * to get the first text field on the block. It falls back on the block's SVG root.
* @param browser The active WebdriverIO Browser object. * @param browser The active WebdriverIO Browser object.
* @param block The block to click, as an interactable element. * @param block The block to click, as an interactable element.
* @return A Promise that resolves to the text element of the first label * @param clickOptions The options to pass to webdriverio's element.click function.
* field on the block, or the block's SVG root if no label field was found. * @return A Promise that resolves when the actions are completed.
*/ */
async function getClickableBlockElement(browser, block) { async function clickBlock(browser, block, clickOptions) {
const findableId = 'clickTargetElement';
// In the browser context, find the element that we want and give it a findable ID. // In the browser context, find the element that we want and give it a findable ID.
await browser.execute((blockId) => { await browser.execute(
const block = Blockly.getMainWorkspace().getBlockById(blockId); (blockId, newElemId) => {
for (const input of block.inputList) { const block = Blockly.getMainWorkspace().getBlockById(blockId);
for (const field of input.fieldRow) { for (const input of block.inputList) {
if (field instanceof Blockly.FieldLabel) { for (const field of input.fieldRow) {
field.getSvgRoot().id = 'clickTargetElement'; if (field instanceof Blockly.FieldLabel) {
return; field.getSvgRoot().id = newElemId;
return;
}
} }
} }
} // No label field found. Fall back to the block's SVG root.
// No label field found. Fall back to the block's SVG root. block.getSvgRoot().id = findableId;
block.getSvgRoot().id = 'clickTargetElement'; },
}, block.id); block.id,
findableId,
);
// In the test context, get the Webdriverio Element that we've identified. // In the test context, get the Webdriverio Element that we've identified.
const elem = await browser.$('#clickTargetElement'); const elem = await browser.$(`#${findableId}`);
return elem;
await elem.click(clickOptions);
// In the browser context, remove the ID.
await browser.execute((elemId) => {
const clickElem = document.getElementById(elemId);
clickElem.removeAttribute('id');
}, findableId);
} }
/** /**
@@ -466,12 +478,7 @@ async function dragBlockFromMutatorFlyout(browser, mutatorBlock, type, x, y) {
* @return A Promise that resolves when the actions are completed. * @return A Promise that resolves when the actions are completed.
*/ */
async function contextMenuSelect(browser, block, itemText) { async function contextMenuSelect(browser, block, itemText) {
const clickEl = await getClickableBlockElement(browser, block); await clickBlock(browser, block, {button: 2});
// Even though the element should definitely already exist,
// one specific test breaks if you remove this...
await clickEl.waitForExist();
await clickEl.click({button: 2});
const item = await browser.$(`div=${itemText}`); const item = await browser.$(`div=${itemText}`);
await item.waitForExist(); await item.waitForExist();
@@ -542,7 +549,7 @@ module.exports = {
getSelectedBlockElement, getSelectedBlockElement,
getSelectedBlockId, getSelectedBlockId,
getBlockElementById, getBlockElementById,
getClickableBlockElement, clickBlock,
getCategory, getCategory,
getNthBlockOfCategory, getNthBlockOfCategory,
getBlockTypeFromCategory, getBlockTypeFromCategory,