Skip to content

Commit e438ba7

Browse files
authored
chore: Added context menu item tests. (#575)
* chore: Added context menu item tests. * Maybe clicking a different location fixes the github test failure. * Assigned the expected actions to constants at top of context menu test. * Fix shadow block action test in github action. * Fix shadow block action test in github action attempt 2.
1 parent 7991855 commit e438ba7

File tree

3 files changed

+161
-70
lines changed

3 files changed

+161
-70
lines changed

test/loadTestBlocks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,12 @@ const moreBlocks = {
317317
'next': {
318318
'block': {
319319
'type': 'text_print',
320-
'id': 'J`*)bq?#`_Vq^X(DQF2t',
320+
'id': 'text_print_1',
321321
'inputs': {
322322
'TEXT': {
323323
'shadow': {
324324
'type': 'text',
325-
'id': '6fW_sIt1t|63j}nPE1ge',
325+
'id': 'text_print_shadow_text_1',
326326
'fields': {
327327
'TEXT': 'abc',
328328
},

test/webdriverio/test/actions_test.ts

Lines changed: 141 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import * as chai from 'chai';
88
import {Key} from 'webdriverio';
99
import {
10+
clickBlock,
1011
contextMenuExists,
1112
moveToToolboxCategory,
1213
PAUSE_TIME,
1314
focusOnBlock,
15+
focusWorkspace,
16+
rightClickOnFlyoutBlockType,
1417
tabNavigateToWorkspace,
1518
testFileLocations,
1619
testSetup,
@@ -19,6 +22,70 @@ import {
1922
contextMenuItems,
2023
} from './test_setup.js';
2124

25+
const isDarwin = process.platform === 'darwin';
26+
27+
const blockActionsViaKeyboard = [
28+
{'text': 'Duplicate D'},
29+
{'text': 'Add Comment'},
30+
{'text': 'External Inputs'},
31+
{'text': 'Collapse Block'},
32+
{'text': 'Disable Block'},
33+
{'text': 'Delete 2 Blocks Delete'},
34+
{'text': 'Move Block M'},
35+
{'text': 'Edit Block contents Right'},
36+
{'text': isDarwin ? 'Cut ⌘ X' : 'Cut Ctrl + X'},
37+
{'text': isDarwin ? 'Copy ⌘ C' : 'Copy Ctrl + C'},
38+
{'disabled': true, 'text': isDarwin ? 'Paste ⌘ V' : 'Paste Ctrl + V'},
39+
];
40+
41+
const blockActionsViaMouse = [
42+
{'text': 'Duplicate D'},
43+
{'text': 'Add Comment'},
44+
{'text': 'External Inputs'},
45+
{'text': 'Collapse Block'},
46+
{'text': 'Disable Block'},
47+
{'text': 'Delete 2 Blocks Delete'},
48+
{'text': isDarwin ? 'Cut ⌘ X' : 'Cut Ctrl + X'},
49+
{'text': isDarwin ? 'Copy ⌘ C' : 'Copy Ctrl + C'},
50+
{'disabled': true, 'text': isDarwin ? 'Paste ⌘ V' : 'Paste Ctrl + V'},
51+
];
52+
53+
const shadowBlockActionsViaKeyboard = [
54+
{'text': 'Add Comment'},
55+
{'text': 'Collapse Block'},
56+
{'text': 'Disable Block'},
57+
{'text': 'Help'},
58+
{'text': 'Move Block M'},
59+
{'text': 'Edit Block contents Right'},
60+
{'disabled': true, 'text': isDarwin ? 'Cut ⌘ X' : 'Cut Ctrl + X'},
61+
{'text': isDarwin ? 'Copy ⌘ C' : 'Copy Ctrl + C'},
62+
{'disabled': true, 'text': isDarwin ? 'Paste ⌘ V' : 'Paste Ctrl + V'},
63+
];
64+
65+
const toolboxBlockActionsViaKeyboard = [
66+
{'text': 'Help'},
67+
{'disabled': true, 'text': 'Move Block M'},
68+
{'disabled': true, 'text': isDarwin ? 'Cut ⌘ X' : 'Cut Ctrl + X'},
69+
{'text': isDarwin ? 'Copy ⌘ C' : 'Copy Ctrl + C'},
70+
];
71+
72+
const flyoutBlockActionsViaMouse = [
73+
{'text': 'Help'},
74+
{'disabled': true, 'text': isDarwin ? 'Cut ⌘ X' : 'Cut Ctrl + X'},
75+
{'text': isDarwin ? 'Copy ⌘ C' : 'Copy Ctrl + C'},
76+
];
77+
78+
const workspaceActionsViaKeyboard = [
79+
{'disabled': true, 'text': 'Undo'},
80+
{'disabled': true, 'text': 'Redo'},
81+
{'text': 'Clean up Blocks'},
82+
{'text': 'Collapse Blocks'},
83+
{'disabled': true, 'text': 'Expand Blocks'},
84+
{'text': 'Delete 14 Blocks'},
85+
{'text': 'Add Comment'},
86+
{'disabled': true, 'text': isDarwin ? 'Paste ⌘ V' : 'Paste Ctrl + V'},
87+
];
88+
2289
suite('Menus test', function () {
2390
// Disable timeouts when non-zero PAUSE_TIME is used to watch tests run.
2491
if (PAUSE_TIME) this.timeout(0);
@@ -30,72 +97,72 @@ suite('Menus test', function () {
3097
// seconds. Allow 30s just in case.
3198
this.timeout(30000);
3299

33-
this.browser = await testSetup(testFileLocations.BASE);
100+
this.browser = await testSetup(testFileLocations.MORE_BLOCKS);
34101
await this.browser.pause(PAUSE_TIME);
35102
});
36103

37-
test('Menu on block', async function () {
104+
test('Menu action via keyboard on block opens menu', async function () {
38105
// Navigate to draw_circle_1.
39106
await focusOnBlock(this.browser, 'draw_circle_1');
40107
await this.browser.pause(PAUSE_TIME);
41108
await sendKeyAndWait(this.browser, [Key.Ctrl, Key.Return]);
42109

43110
chai.assert.deepEqual(
44-
process.platform === 'darwin'
45-
? [
46-
{'text': 'Duplicate D'},
47-
{'text': 'Add Comment'},
48-
{'text': 'External Inputs'},
49-
{'text': 'Collapse Block'},
50-
{'text': 'Disable Block'},
51-
{'text': 'Delete 2 Blocks Delete'},
52-
{'text': 'Move Block M'},
53-
{'text': 'Edit Block contents Right'},
54-
{'text': 'Cut ⌘ X'},
55-
{'text': 'Copy ⌘ C'},
56-
{'disabled': true, 'text': 'Paste ⌘ V'},
57-
]
58-
: [
59-
{'text': 'Duplicate D'},
60-
{'text': 'Add Comment'},
61-
{'text': 'External Inputs'},
62-
{'text': 'Collapse Block'},
63-
{'text': 'Disable Block'},
64-
{'text': 'Delete 2 Blocks Delete'},
65-
{'text': 'Move Block M'},
66-
{'text': 'Edit Block contents Right'},
67-
{'text': 'Cut Ctrl + X'},
68-
{'text': 'Copy Ctrl + C'},
69-
{'disabled': true, 'text': 'Paste Ctrl + V'},
70-
],
71111
await contextMenuItems(this.browser),
112+
blockActionsViaKeyboard,
72113
);
73114
});
74115

75-
test('Menu on block in the toolbox', async function () {
76-
// Navigate to draw_circle_1.
77-
await focusOnBlock(this.browser, 'draw_circle_1');
116+
test('Block menu via mouse displays expected items', async function () {
117+
await tabNavigateToWorkspace(this.browser);
118+
await clickBlock(this.browser, 'draw_circle_1', {button: 'right'});
119+
120+
chai.assert.deepEqual(
121+
await contextMenuItems(this.browser),
122+
blockActionsViaMouse,
123+
);
124+
});
125+
126+
test('Shadow block menu via keyboard displays expected items', async function () {
127+
await tabNavigateToWorkspace(this.browser);
128+
await focusOnBlock(this.browser, 'text_print_1');
129+
await this.browser.keys(Key.ArrowRight);
130+
await this.browser.keys([Key.Ctrl, Key.Return]);
131+
await this.browser.pause(PAUSE_TIME);
132+
133+
chai.assert.deepEqual(
134+
await contextMenuItems(this.browser),
135+
shadowBlockActionsViaKeyboard,
136+
);
137+
});
138+
139+
test('Menu action on block in the toolbox', async function () {
140+
await tabNavigateToWorkspace(this.browser);
78141
// Navigate to a toolbox category
79142
await moveToToolboxCategory(this.browser, 'Functions');
80143
// Move to flyout.
81144
await keyRight(this.browser);
82145
await sendKeyAndWait(this.browser, [Key.Ctrl, Key.Return]);
83146

84147
chai.assert.deepEqual(
85-
process.platform === 'darwin'
86-
? [
87-
{'text': 'Help'},
88-
{'disabled': true, 'text': 'Move Block M'},
89-
{'disabled': true, 'text': 'Cut ⌘ X'},
90-
{'text': 'Copy ⌘ C'},
91-
]
92-
: [
93-
{'text': 'Help'},
94-
{'disabled': true, 'text': 'Move Block M'},
95-
{'disabled': true, 'text': 'Cut Ctrl + X'},
96-
{'text': 'Copy Ctrl + C'},
97-
],
98148
await contextMenuItems(this.browser),
149+
toolboxBlockActionsViaKeyboard,
150+
);
151+
});
152+
153+
test('Flyout block menu via mouse displays expected items', async function () {
154+
await tabNavigateToWorkspace(this.browser);
155+
// Navigate to a toolbox category
156+
await moveToToolboxCategory(this.browser, 'Math');
157+
// Move to flyout.
158+
await keyRight(this.browser);
159+
await this.browser.pause(PAUSE_TIME);
160+
await rightClickOnFlyoutBlockType(this.browser, 'math_number');
161+
await this.browser.pause(PAUSE_TIME);
162+
163+
chai.assert.deepEqual(
164+
await contextMenuItems(this.browser),
165+
flyoutBlockActionsViaMouse,
99166
);
100167
});
101168

@@ -106,28 +173,8 @@ suite('Menus test', function () {
106173
await sendKeyAndWait(this.browser, [Key.Ctrl, Key.Return]);
107174

108175
chai.assert.deepEqual(
109-
process.platform === 'darwin'
110-
? [
111-
{'disabled': true, 'text': 'Undo'},
112-
{'disabled': true, 'text': 'Redo'},
113-
{'text': 'Clean up Blocks'},
114-
{'text': 'Collapse Blocks'},
115-
{'disabled': true, 'text': 'Expand Blocks'},
116-
{'text': 'Delete 4 Blocks'},
117-
{'text': 'Add Comment'},
118-
{'disabled': true, 'text': 'Paste ⌘ V'},
119-
]
120-
: [
121-
{'disabled': true, 'text': 'Undo'},
122-
{'disabled': true, 'text': 'Redo'},
123-
{'text': 'Clean up Blocks'},
124-
{'text': 'Collapse Blocks'},
125-
{'disabled': true, 'text': 'Expand Blocks'},
126-
{'text': 'Delete 4 Blocks'},
127-
{'text': 'Add Comment'},
128-
{'disabled': true, 'text': 'Paste Ctrl + V'},
129-
],
130176
await contextMenuItems(this.browser),
177+
workspaceActionsViaKeyboard,
131178
);
132179
});
133180

@@ -143,4 +190,32 @@ suite('Menus test', function () {
143190
'The menu should not be openable during a move',
144191
);
145192
});
193+
194+
test('Escape key dismisses menu', async function () {
195+
await tabNavigateToWorkspace(this.browser);
196+
await focusOnBlock(this.browser, 'draw_circle_1');
197+
await this.browser.pause(PAUSE_TIME);
198+
await this.browser.keys([Key.Ctrl, Key.Return]);
199+
await this.browser.pause(PAUSE_TIME);
200+
await this.browser.keys(Key.Escape);
201+
await this.browser.pause(PAUSE_TIME);
202+
203+
chai.assert.isTrue(
204+
await contextMenuExists(this.browser, 'Duplicate', /* reverse= */ true),
205+
'The menu should be closed',
206+
);
207+
});
208+
209+
test('Clicking workspace dismisses menu', async function () {
210+
await tabNavigateToWorkspace(this.browser);
211+
await clickBlock(this.browser, 'draw_circle_1', {button: 'right'});
212+
await this.browser.pause(PAUSE_TIME);
213+
await focusWorkspace(this.browser);
214+
await this.browser.pause(PAUSE_TIME);
215+
216+
chai.assert.isTrue(
217+
await contextMenuExists(this.browser, 'Duplicate', /* reverse= */ true),
218+
'The menu should be closed',
219+
);
220+
});
146221
});

test/webdriverio/test/test_setup.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ export const testFileLocations = {
153153
new URLSearchParams({scenario: 'navigationTestBlocks'}),
154154
),
155155
// eslint-disable-next-line @typescript-eslint/naming-convention
156+
MORE_BLOCKS: createTestUrl(new URLSearchParams({scenario: 'moreBlocks'})),
157+
// eslint-disable-next-line @typescript-eslint/naming-convention
156158
MOVE_TEST_BLOCKS: createTestUrl(
157159
new URLSearchParams({scenario: 'moveTestBlocks'}),
158160
),
@@ -188,7 +190,7 @@ export async function focusWorkspace(browser: WebdriverIO.Browser) {
188190
const workspaceElement = await browser.$(
189191
'#blocklyDiv > div > svg.blocklySvg > g',
190192
);
191-
await workspaceElement.click();
193+
await workspaceElement.click({x: 100});
192194
}
193195

194196
/**
@@ -573,7 +575,7 @@ export async function isDragging(
573575
}
574576

575577
/**
576-
* Returns the result of the specificied action precondition.
578+
* Returns the result of the specified action precondition.
577579
*
578580
* @param browser The active WebdriverIO Browser object.
579581
* @param action The action to check the precondition for.
@@ -710,3 +712,17 @@ export async function clickBlock(
710712
document.getElementById(elemId)?.removeAttribute('id');
711713
}, findableId);
712714
}
715+
716+
/**
717+
* Right-clicks on a block with the provided type in the flyout.
718+
*
719+
* @param browser The active WebdriverIO Browser object.
720+
* @param blockType The name of the type block to right click on.
721+
*/
722+
export async function rightClickOnFlyoutBlockType(
723+
browser: WebdriverIO.Browser,
724+
blockType: string,
725+
) {
726+
const elem = await browser.$(`.blocklyFlyout .${blockType}`);
727+
await elem.click({button: 'right'});
728+
}

0 commit comments

Comments
 (0)