Skip to content

Commit 8241fca

Browse files
chore(tests): cleanup and toolbox drag tests (#7350)
* chore(tests): use helpers for the basic drag test in the playground * chore(tests): miscellaneous test cleanup * chore: format * feat(tests): add test that drags out every block from the toolbox * feat(tests): add RTL version of toolbox drag tests * chore: lint * chore(tests): respond to PR feedback
1 parent 18e0d53 commit 8241fca

6 files changed

Lines changed: 273 additions & 66 deletions

File tree

tests/browser/test/basic_playground_test.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,13 @@ suite('Testing Connecting Blocks', function () {
4646
this.browser = await testSetup(testFileLocations.PLAYGROUND);
4747
});
4848

49-
test('Testing Block Flyout', async function () {
50-
const logicButton = await this.browser.$('#blockly-0');
51-
logicButton.click();
52-
const ifDoBlock = await this.browser.$(
53-
'#blocklyDiv > div > svg:nth-child(7) > g > g.blocklyBlockCanvas > g:nth-child(3)',
54-
);
55-
await ifDoBlock.dragAndDrop({x: 20, y: 20});
56-
await this.browser.pause(200);
57-
const blockOnWorkspace = await this.browser.execute(() => {
58-
const newBlock = Blockly.getMainWorkspace().getAllBlocks(false)[0];
59-
if (newBlock.id) {
60-
return true;
61-
} else {
62-
return false;
63-
}
49+
test('dragging a block from the flyout results in a block on the workspace', async function () {
50+
await dragBlockTypeFromFlyout(this.browser, 'Logic', 'controls_if', 20, 20);
51+
const blockCount = await this.browser.execute(() => {
52+
return Blockly.getMainWorkspace().getAllBlocks(false).length;
6453
});
6554

66-
chai.assert.isTrue(blockOnWorkspace);
55+
chai.assert.equal(blockCount, 1);
6756
});
6857
});
6958

tests/browser/test/block_undo_test.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,38 @@ const {Key} = require('webdriverio');
1313
const {
1414
testSetup,
1515
testFileLocations,
16-
switchRTL,
1716
dragBlockTypeFromFlyout,
1817
screenDirection,
18+
getAllBlocks,
1919
} = require('./test_setup');
2020

2121
suite('Testing undo block movement', function (done) {
2222
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
2323
this.timeout(0);
2424

25-
// Setup Selenium for all of the tests
26-
suiteSetup(async function () {
27-
this.browser = await testSetup(testFileLocations.PLAYGROUND);
28-
});
29-
3025
test('Undoing Block Movement LTR', async function () {
26+
this.browser = await testSetup(testFileLocations.PLAYGROUND);
3127
await testUndoBlock(this.browser, screenDirection.LTR);
3228
});
3329

3430
test('Undoing Block Movement RTL', async function () {
35-
await switchRTL(this.browser);
31+
this.browser = await testSetup(testFileLocations.PLAYGROUND_RTL);
3632
await testUndoBlock(this.browser, screenDirection.RTL);
3733
});
3834
});
3935

40-
async function testUndoBlock(browser, delta) {
36+
async function testUndoBlock(browser, direction) {
4137
// Drag out first function
42-
const defReturnBlock = await dragBlockTypeFromFlyout(
38+
await dragBlockTypeFromFlyout(
4339
browser,
4440
'Functions',
4541
'procedures_defreturn',
46-
50 * delta,
42+
50 * direction,
4743
20,
4844
);
4945

5046
await browser.keys([Key.Ctrl, 'z']);
5147

52-
const blockOnWorkspace = await browser.execute(() => {
53-
return !!Blockly.getMainWorkspace().getAllBlocks(false)[0];
54-
});
55-
56-
chai.assert.isFalse(blockOnWorkspace);
48+
const allBlocks = await getAllBlocks(browser);
49+
chai.assert.equal(allBlocks.length, 0);
5750
}

tests/browser/test/extensive_test.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
testSetup,
1414
testFileLocations,
1515
getBlockElementById,
16+
getAllBlocks,
1617
} = require('./test_setup');
1718
const {Key} = require('webdriverio');
1819

@@ -25,32 +26,28 @@ suite('This tests loading Large Configuration and Deletion', function (done) {
2526
this.browser = await testSetup(testFileLocations.PLAYGROUND);
2627
});
2728

28-
test('This test loading from JSON results in the correct number of blocks', async function () {
29+
test('loading from JSON results in the correct number of blocks', async function () {
2930
const blockNum = await testingJSONLoad(this.browser);
3031
chai.assert.equal(blockNum, 13);
3132
});
3233

33-
test('This test deleting block results in the correct number of blocks', async function () {
34+
test('deleting block results in the correct number of blocks', async function () {
3435
const fourthRepeatDo = await getBlockElementById(
3536
this.browser,
3637
'E8bF[-r:B~cabGLP#QYd',
3738
);
3839
await fourthRepeatDo.click({x: -100, y: -40});
3940
await this.browser.keys([Key.Delete]);
4041
await this.browser.pause(100);
41-
const blockNum = await this.browser.execute(() => {
42-
return Blockly.getMainWorkspace().getAllBlocks(false).length;
43-
});
44-
chai.assert.equal(blockNum, 10);
42+
const allBlocks = await getAllBlocks(this.browser);
43+
chai.assert.equal(allBlocks.length, 10);
4544
});
4645

47-
test('This test undoing delete block results in the correct number of blocks', async function () {
46+
test('undoing delete block results in the correct number of blocks', async function () {
4847
await this.browser.keys([Key.Ctrl, 'z']);
4948
await this.browser.pause(100);
50-
const blockNum = await this.browser.execute(() => {
51-
return Blockly.getMainWorkspace().getAllBlocks(false).length;
52-
});
53-
chai.assert.equal(blockNum, 13);
49+
const allBlocks = await getAllBlocks(this.browser);
50+
chai.assert.equal(allBlocks.length, 13);
5451
});
5552
});
5653

tests/browser/test/field_edits_test.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const chai = require('chai');
1212
const {
1313
testSetup,
1414
testFileLocations,
15-
getSelectedBlockElement,
16-
switchRTL,
1715
dragBlockTypeFromFlyout,
1816
screenDirection,
1917
} = require('./test_setup');
@@ -23,47 +21,42 @@ suite('Testing Field Edits', function (done) {
2321
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
2422
this.timeout(0);
2523

26-
// Setup Selenium for all of the tests
27-
suiteSetup(async function () {
28-
this.browser = await testSetup(testFileLocations.PLAYGROUND);
29-
});
30-
3124
test('Testing Field Edits LTR', async function () {
25+
this.browser = await testSetup(testFileLocations.PLAYGROUND);
3226
await testFieldEdits(this.browser, screenDirection.LTR);
3327
});
3428

3529
test('Testing Field Edits RTL', async function () {
36-
await switchRTL(this.browser);
30+
this.browser = await testSetup(testFileLocations.PLAYGROUND_RTL);
3731
await testFieldEdits(this.browser, screenDirection.RTL);
3832
});
3933
});
4034

41-
async function testFieldEdits(browser, delta) {
42-
const mathNumber = await dragBlockTypeFromFlyout(
35+
async function testFieldEdits(browser, direction) {
36+
const numberBlock = await dragBlockTypeFromFlyout(
4337
browser,
4438
'Math',
4539
'math_number',
46-
50 * delta,
40+
50 * direction,
4741
20,
4842
);
49-
await browser.pause(200);
5043

5144
// Click on the field to change the value
52-
const numeric = await getSelectedBlockElement(browser);
53-
await numeric.doubleClick();
45+
await numberBlock.click();
5446
await browser.keys([Key.Delete]);
55-
await numeric.doubleClick();
47+
await numberBlock.click();
5648
await browser.keys(['1093']);
57-
// Click on the workspace
49+
// Click on the workspace to exit the field editor
5850
const workspace = await browser.$('#blocklyDiv > div > svg.blocklySvg > g');
5951
await workspace.click();
6052
await browser.pause(200);
61-
// Get value of the number
62-
const numericText = await browser
63-
.$(
64-
'#blocklyDiv > div > svg.blocklySvg > g > g.blocklyBlockCanvas > g.blocklyDraggable > g > text',
65-
)
66-
.getHTML();
6753

68-
chai.assert.isTrue(numericText.includes('1093'));
54+
const fieldValue = await browser.execute((id) => {
55+
return Blockly.getMainWorkspace()
56+
.getBlockById(id)
57+
.getField('NUM')
58+
.getValue();
59+
}, numberBlock.id);
60+
61+
chai.assert.equal(fieldValue, '1093');
6962
}

tests/browser/test/test_setup.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ const testFileLocations = {
9494
'file://' +
9595
posixPath(path.join(__dirname, '..', '..')) +
9696
'/playground.html',
97+
PLAYGROUND_RTL:
98+
'file://' +
99+
posixPath(path.join(__dirname, '..', '..')) +
100+
'/playground.html?dir=rtl',
97101
};
98102

99103
/**
@@ -164,7 +168,6 @@ async function getCategory(browser, categoryName) {
164168
async function getNthBlockOfCategory(browser, categoryName, n) {
165169
const category = await getCategory(browser, categoryName);
166170
await category.click();
167-
await browser.pause(100);
168171
const block = await browser.$(
169172
`.blocklyFlyout .blocklyBlockCanvas > g:nth-child(${3 + n * 2})`,
170173
);
@@ -440,6 +443,29 @@ async function getAllBlocks(browser) {
440443
});
441444
}
442445

446+
/**
447+
* Find the flyout's scrollbar and scroll by the specified amount.
448+
* This makes several assumptions:
449+
* - A flyout with a valid scrollbar exists, is open, and is in view.
450+
* - The workspace has a trash can, which means it has a second (hidden) flyout.
451+
* @param browser The active WebdriverIO Browser object.
452+
* @param xDelta How far to drag the flyout in the x direction. Positive is right.
453+
* @param yDelta How far to drag thte flyout in the y direction. Positive is down.
454+
* @return A Promise that resolves when the actions are completed.
455+
*/
456+
async function scrollFlyout(browser, xDelta, yDelta) {
457+
// There are two flyouts on the playground workspace: one for the trash can
458+
// and one for the toolbox. We want the second one.
459+
// This assumes there is only one scrollbar handle in the flyout, but it could
460+
// be either horizontal or vertical.
461+
await browser.pause(50);
462+
const scrollbarHandle = await browser
463+
.$$(`.blocklyFlyoutScrollbar`)[1]
464+
.$(`rect.blocklyScrollbarHandle`);
465+
await scrollbarHandle.dragAndDrop({x: xDelta, y: yDelta});
466+
await browser.pause(50);
467+
}
468+
443469
module.exports = {
444470
testSetup,
445471
testFileLocations,
@@ -459,4 +485,5 @@ module.exports = {
459485
screenDirection,
460486
getBlockTypeFromWorkspace,
461487
getAllBlocks,
488+
scrollFlyout,
462489
};

0 commit comments

Comments
 (0)