Skip to content

Commit

Permalink
chore(tests): use a shared chrome instance for all browser tests (#7328)
Browse files Browse the repository at this point in the history
* chore(tests): use a shared chrome instance for all browser tests

* chore: format

* chore: lint
  • Loading branch information
rachel-fenichel authored Jul 25, 2023
1 parent 2546b01 commit b5911c2
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 139 deletions.
1 change: 1 addition & 0 deletions tests/browser/test/.mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

module.exports = {
ui: 'tdd',
require: 'tests/browser/test/hooks.js',
};
15 changes: 9 additions & 6 deletions tests/browser/test/basic_block_factory_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@
const chai = require('chai');
const {testSetup, testFileLocations} = require('./test_setup');

let browser;
suite('Testing Connecting Blocks', function (done) {
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
this.timeout(0);

// Setup Selenium for all of the tests
suiteSetup(async function () {
browser = await testSetup(testFileLocations.BLOCK_FACTORY);
this.browser = await testSetup(testFileLocations.BLOCK_FACTORY);
});

test('Testing Block Drag', async function () {
const startingBlock = await browser.$(
const startingBlock = await this.browser.$(
'#blockly > div > svg.blocklySvg > g > g.blocklyBlockCanvas > g:nth-child(2)',
);
const blocklyCanvas = await browser.$(
const blocklyCanvas = await this.browser.$(
'#blockly > div > svg.blocklySvg > g > g.blocklyBlockCanvas',
);
const firstPostion = await blocklyCanvas.getAttribute('transform');
Expand All @@ -34,8 +33,12 @@ suite('Testing Connecting Blocks', function (done) {
chai.assert.notEqual(firstPostion, secondPosition);
});

// Teardown entire suite after test are done running
suiteTeardown(async function () {
await browser.deleteSession();
await this.browser.execute(() => {
// If you leave blocks on the workspace, the block factory pops up an alert asking
// if you really want to lose your work when you try to load a new page.
// Clearing blocks resolves this and is easier than waiting for the alert.
Blockly.getMainWorkspace().clear();
});
});
});
13 changes: 3 additions & 10 deletions tests/browser/test/basic_block_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,22 @@ const {
} = require('./test_setup');
const {Key} = require('webdriverio');

let browser;

suite('Basic block tests', function (done) {
// Setting timeout to unlimited as the webdriver takes a longer time
// to run than most mocha test
this.timeout(0);

// Setup Selenium for all of the tests
suiteSetup(async function () {
browser = await testSetup(
this.browser = await testSetup(
testFileLocations.PLAYGROUND + '?toolbox=test-blocks',
);
});

test('Drag three blocks into the workspace', async function () {
for (let i = 1; i <= 3; i++) {
await dragNthBlockFromFlyout(browser, 'Align', 0, 250, 50 * i);
chai.assert.equal((await getAllBlocks(browser)).length, i);
await dragNthBlockFromFlyout(this.browser, 'Align', 0, 250, 50 * i);
chai.assert.equal((await getAllBlocks(this.browser)).length, i);
}
});

// Teardown entire suite after test are done running
suiteTeardown(async function () {
await browser.deleteSession();
});
});
87 changes: 36 additions & 51 deletions tests/browser/test/basic_playground_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,24 @@ async function getCommentText(browser, blockId) {
}, blockId);
}

let browser;
suite('Testing Connecting Blocks', function () {
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
this.timeout(0);

// Setup Selenium for all of the tests
suiteSetup(async function () {
browser = await testSetup(testFileLocations.PLAYGROUND);
this.browser = await testSetup(testFileLocations.PLAYGROUND);
});

test('Testing Block Flyout', async function () {
const logicButton = await browser.$('#blockly-0');
const logicButton = await this.browser.$('#blockly-0');
logicButton.click();
const ifDoBlock = await browser.$(
const ifDoBlock = await this.browser.$(
'#blocklyDiv > div > svg:nth-child(7) > g > g.blocklyBlockCanvas > g:nth-child(3)',
);
await ifDoBlock.dragAndDrop({x: 20, y: 20});
await new Promise((resolve) => setTimeout(resolve, 2000)); // 2 sec
const blockOnWorkspace = await browser.execute(() => {
await new Promise((resolve) => setTimeout(resolve, 200));
const blockOnWorkspace = await this.browser.execute(() => {
const newBlock = Blockly.getMainWorkspace().getAllBlocks(false)[0];
if (newBlock.id) {
return true;
Expand All @@ -66,11 +65,6 @@ suite('Testing Connecting Blocks', function () {

chai.assert.isTrue(blockOnWorkspace);
});

// Teardown entire suite after test are done running
suiteTeardown(async function () {
await browser.deleteSession();
});
});

/**
Expand All @@ -83,45 +77,40 @@ suite('Right Clicking on Blocks', function () {

// Setup Selenium for all of the tests
suiteSetup(async function () {
browser = await testSetup(testFileLocations.PLAYGROUND);
this.block = await dragNthBlockFromFlyout(browser, 'Loops', 0, 20, 20);
this.browser = await testSetup(testFileLocations.PLAYGROUND);
this.block = await dragNthBlockFromFlyout(this.browser, 'Loops', 0, 20, 20);
this.blockId = this.block.id;
});

test('clicking the collapse option collapses the block', async function () {
await contextMenuSelect(browser, this.block, 'Collapse Block');
chai.assert.isTrue(await getIsCollapsed(browser, this.blockId));
await contextMenuSelect(this.browser, this.block, 'Collapse Block');
chai.assert.isTrue(await getIsCollapsed(this.browser, this.blockId));
});

// Assumes that
test('clicking the expand option expands the block', async function () {
await contextMenuSelect(browser, this.block, 'Expand Block');
chai.assert.isFalse(await getIsCollapsed(browser, this.blockId));
await contextMenuSelect(this.browser, this.block, 'Expand Block');
chai.assert.isFalse(await getIsCollapsed(this.browser, this.blockId));
});

test('clicking the disable option disables the block', async function () {
await contextMenuSelect(browser, this.block, 'Disable Block');
chai.assert.isTrue(await getIsDisabled(browser, this.blockId));
await contextMenuSelect(this.browser, this.block, 'Disable Block');
chai.assert.isTrue(await getIsDisabled(this.browser, this.blockId));
});

test('clicking the enable option enables the block', async function () {
await contextMenuSelect(browser, this.block, 'Enable Block');
chai.assert.isFalse(await getIsDisabled(browser, this.block.id));
await contextMenuSelect(this.browser, this.block, 'Enable Block');
chai.assert.isFalse(await getIsDisabled(this.browser, this.block.id));
});

test('clicking the add comment option adds a comment to the block', async function () {
await contextMenuSelect(browser, this.block, 'Add Comment');
chai.assert.equal(await getCommentText(browser, this.block.id), '');
await contextMenuSelect(this.browser, this.block, 'Add Comment');
chai.assert.equal(await getCommentText(this.browser, this.block.id), '');
});

test('clicking the remove comment option removes a comment from the block', async function () {
await contextMenuSelect(browser, this.block, 'Remove Comment');
chai.assert.isNull(await getCommentText(browser, this.block.id));
});

// Teardown entire suite after test are done running
suiteTeardown(async function () {
await browser.deleteSession();
await contextMenuSelect(this.browser, this.block, 'Remove Comment');
chai.assert.isNull(await getCommentText(this.browser, this.block.id));
});
});

Expand All @@ -131,36 +120,36 @@ suite('Disabling', function () {
this.timeout(0);

suiteSetup(async function () {
browser = await testSetup(testFileLocations.PLAYGROUND);
this.browser = await testSetup(testFileLocations.PLAYGROUND);
});

setup(async function () {
await browser.refresh();
await this.browser.refresh();
});

test(
'children connected to value inputs are disabled when the ' +
'parent is diabled',
async function () {
const parent = await dragBlockTypeFromFlyout(
browser,
this.browser,
'Logic',
'controls_if',
10,
10,
);
const child = await dragBlockTypeFromFlyout(
browser,
this.browser,
'Logic',
'logic_boolean',
110,
110,
);
await connect(browser, child, 'OUTPUT', parent, 'IF0');
await connect(this.browser, child, 'OUTPUT', parent, 'IF0');

await contextMenuSelect(browser, parent, 'Disable Block');
await contextMenuSelect(this.browser, parent, 'Disable Block');

chai.assert.isTrue(await getIsDisabled(browser, child.id));
chai.assert.isTrue(await getIsDisabled(this.browser, child.id));
},
);

Expand All @@ -169,24 +158,24 @@ suite('Disabling', function () {
'parent is disabled',
async function () {
const parent = await dragBlockTypeFromFlyout(
browser,
this.browser,
'Logic',
'controls_if',
10,
10,
);
const child = await dragBlockTypeFromFlyout(
browser,
this.browser,
'Logic',
'controls_if',
110,
110,
);
await connect(browser, child, 'PREVIOUS', parent, 'DO0');
await connect(this.browser, child, 'PREVIOUS', parent, 'DO0');

await contextMenuSelect(browser, parent, 'Disable Block');
await contextMenuSelect(this.browser, parent, 'Disable Block');

chai.assert.isTrue(await getIsDisabled(browser, child.id));
chai.assert.isTrue(await getIsDisabled(this.browser, child.id));
},
);

Expand All @@ -195,28 +184,24 @@ suite('Disabling', function () {
'parent is disabled',
async function () {
const parent = await dragBlockTypeFromFlyout(
browser,
this.browser,
'Logic',
'controls_if',
10,
10,
);
const child = await dragBlockTypeFromFlyout(
browser,
this.browser,
'Logic',
'controls_if',
110,
110,
);
await connect(browser, child, 'PREVIOUS', parent, 'NEXT');
await connect(this.browser, child, 'PREVIOUS', parent, 'NEXT');

await contextMenuSelect(browser, parent, 'Disable Block');
await contextMenuSelect(this.browser, parent, 'Disable Block');

chai.assert.isFalse(await getIsDisabled(browser, child.id));
chai.assert.isFalse(await getIsDisabled(this.browser, child.id));
},
);

suiteTeardown(async function () {
await browser.deleteSession();
});
});
16 changes: 5 additions & 11 deletions tests/browser/test/block_undo_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,26 @@ const {
screenDirection,
} = require('./test_setup');

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

// Setup Selenium for all of the tests
suiteSetup(async function () {
browser = await testSetup(testFileLocations.PLAYGROUND);
this.browser = await testSetup(testFileLocations.PLAYGROUND);
});

test('Undoing Block Movement LTR', async function () {
await testUndoBlock(screenDirection.LTR);
await testUndoBlock(this.browser, screenDirection.LTR);
});

test('Undoing Block Movement RTL', async function () {
await switchRTL(browser);
await testUndoBlock(screenDirection.RTL);
});

// Teardown entire suite after test are done running
suiteTeardown(async function () {
await browser.deleteSession();
await switchRTL(this.browser);
await testUndoBlock(this.browser, screenDirection.RTL);
});
});

async function testUndoBlock(delta) {
async function testUndoBlock(browser, delta) {
// Drag out first function
const defReturnBlock = await dragBlockTypeFromFlyout(
browser,
Expand Down
26 changes: 10 additions & 16 deletions tests/browser/test/extensive_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,45 @@ const {
} = require('./test_setup');
const {Key} = require('webdriverio');

let browser;
suite('This tests loading Large Configuration and Deletion', function (done) {
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
this.timeout(0);

// Setup Selenium for all of the tests
suiteSetup(async function () {
browser = await testSetup(testFileLocations.PLAYGROUND);
this.browser = await testSetup(testFileLocations.PLAYGROUND);
});

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

test('This test deleting block results in the correct number of blocks', async function () {
const fourthRepeatDo = await getBlockElementById(
browser,
this.browser,
'E8bF[-r:B~cabGLP#QYd',
);
await fourthRepeatDo.click({x: -100, y: -40});
await browser.keys([Key.Delete]);
await browser.pause(100);
const blockNum = await browser.execute(() => {
await this.browser.keys([Key.Delete]);
await this.browser.pause(100);
const blockNum = await this.browser.execute(() => {
return Blockly.getMainWorkspace().getAllBlocks(false).length;
});
chai.assert.equal(blockNum, 10);
});

test('This test undoing delete block results in the correct number of blocks', async function () {
await browser.keys([Key.Ctrl, 'z']);
await browser.pause(100);
const blockNum = await browser.execute(() => {
await this.browser.keys([Key.Ctrl, 'z']);
await this.browser.pause(100);
const blockNum = await this.browser.execute(() => {
return Blockly.getMainWorkspace().getAllBlocks(false).length;
});
chai.assert.equal(blockNum, 13);
});

// Teardown entire suite after test are done running
suiteTeardown(async function () {
await browser.deleteSession();
});
});

async function testingJSONLoad() {
async function testingJSONLoad(browser) {
return await browser.execute(() => {
const myWorkspace = Blockly.getMainWorkspace();
const state = {
Expand Down
Loading

0 comments on commit b5911c2

Please sign in to comment.