Skip to content

Commit e4066e1

Browse files
committed
chore: Added context menu item tests.
1 parent 29e4b03 commit e4066e1

File tree

2 files changed

+192
-3
lines changed

2 files changed

+192
-3
lines changed

test/webdriverio/test/actions_test.ts

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import * as chai from 'chai';
88
import {Key} from 'webdriverio';
99
import {
1010
contextMenuExists,
11+
getContextMenuItemNames,
1112
moveToToolboxCategory,
1213
PAUSE_TIME,
1314
focusOnBlock,
15+
focusWorkspace,
16+
rightClickOnBlock,
17+
rightClickOnFlyoutBlockType,
1418
tabNavigateToWorkspace,
1519
testFileLocations,
1620
testSetup,
@@ -27,6 +31,18 @@ suite('Menus test', function () {
2731
await this.browser.pause(PAUSE_TIME);
2832
});
2933

34+
test('Menu keyboard shortcut on workspace does not open menu', async function () {
35+
await tabNavigateToWorkspace(this.browser);
36+
await this.browser.pause(PAUSE_TIME);
37+
await this.browser.keys([Key.Ctrl, Key.Return]);
38+
await this.browser.pause(PAUSE_TIME);
39+
40+
chai.assert.isTrue(
41+
await contextMenuExists(this.browser, 'Undo', /* reverse= */ true),
42+
'The menu should not be openable on the workspace',
43+
);
44+
});
45+
3046
test('Menu action opens menu', async function () {
3147
// Navigate to draw_circle_1.
3248
await tabNavigateToWorkspace(this.browser);
@@ -41,9 +57,7 @@ suite('Menus test', function () {
4157
});
4258

4359
test('Menu action returns true in the toolbox', async function () {
44-
// Navigate to draw_circle_1.
4560
await tabNavigateToWorkspace(this.browser);
46-
await focusOnBlock(this.browser, 'draw_circle_1');
4761
// Navigate to a toolbox category
4862
await moveToToolboxCategory(this.browser, 'Functions');
4963
// Move to flyout.
@@ -70,4 +84,133 @@ suite('Menus test', function () {
7084
'The menu should not be openable during a move',
7185
);
7286
});
87+
88+
test('Block menu via keyboard displays expected items', async function () {
89+
await tabNavigateToWorkspace(this.browser);
90+
await focusOnBlock(this.browser, 'draw_circle_1');
91+
await this.browser.keys([Key.Ctrl, Key.Return]);
92+
await this.browser.pause(PAUSE_TIME);
93+
94+
chai.assert.deepEqual(
95+
await getContextMenuItemNames(this.browser),
96+
[
97+
'Duplicate',
98+
'Add Comment',
99+
'External Inputs',
100+
'Collapse Block',
101+
'Disable Block',
102+
'Delete 2 Blocks',
103+
'Move Block',
104+
'Edit Block contents',
105+
'Cut',
106+
'Copy',
107+
'Paste',
108+
],
109+
'A block context menu should display certain items',
110+
);
111+
});
112+
113+
test('Block menu via mouse displays expected items', async function () {
114+
await tabNavigateToWorkspace(this.browser);
115+
await rightClickOnBlock(this.browser, 'draw_circle_1');
116+
117+
chai.assert.deepEqual(
118+
await getContextMenuItemNames(this.browser),
119+
[
120+
'Duplicate',
121+
'Add Comment',
122+
'External Inputs',
123+
'Collapse Block',
124+
'Disable Block',
125+
'Delete 2 Blocks',
126+
'Cut',
127+
'Copy',
128+
'Paste',
129+
],
130+
'A block context menu should display certain items',
131+
);
132+
});
133+
134+
test('Shadow block menu via keyboard displays expected items', async function () {
135+
await tabNavigateToWorkspace(this.browser);
136+
await focusOnBlock(this.browser, 'draw_circle_1_color');
137+
await this.browser.keys([Key.Ctrl, Key.Return]);
138+
await this.browser.pause(PAUSE_TIME);
139+
140+
chai.assert.deepEqual(
141+
await getContextMenuItemNames(this.browser),
142+
[
143+
'Add Comment',
144+
'Collapse Block',
145+
'Disable Block',
146+
'Help',
147+
'Move Block',
148+
'Cut',
149+
'Copy',
150+
'Paste',
151+
],
152+
'A shadow block context menu should display certain items',
153+
);
154+
});
155+
156+
test('Flyout block menu via keyboard displays expected items', async function () {
157+
await tabNavigateToWorkspace(this.browser);
158+
// Navigate to a toolbox category
159+
await moveToToolboxCategory(this.browser, 'Functions');
160+
// Move to flyout.
161+
await keyRight(this.browser);
162+
await this.browser.keys([Key.Ctrl, Key.Return]);
163+
await this.browser.pause(PAUSE_TIME);
164+
165+
chai.assert.deepEqual(
166+
await getContextMenuItemNames(this.browser),
167+
['Help', 'Move Block', 'Cut', 'Copy', 'Paste'],
168+
'A flyout block context menu should display certain items',
169+
);
170+
});
171+
172+
test('Flyout block menu via mouse displays expected items', async function () {
173+
await tabNavigateToWorkspace(this.browser);
174+
// Navigate to a toolbox category
175+
await moveToToolboxCategory(this.browser, 'Math');
176+
// Move to flyout.
177+
await keyRight(this.browser);
178+
await this.browser.pause(PAUSE_TIME);
179+
await rightClickOnFlyoutBlockType(this.browser, 'math_number');
180+
await this.browser.pause(PAUSE_TIME);
181+
182+
chai.assert.deepEqual(
183+
await getContextMenuItemNames(this.browser),
184+
['Help', 'Cut', 'Copy', 'Paste'],
185+
'A flyout block context menu should display certain items',
186+
);
187+
});
188+
189+
test('Escape key dismisses menu', async function () {
190+
await tabNavigateToWorkspace(this.browser);
191+
await focusOnBlock(this.browser, 'draw_circle_1');
192+
await this.browser.pause(PAUSE_TIME);
193+
await this.browser.keys([Key.Ctrl, Key.Return]);
194+
await this.browser.pause(PAUSE_TIME);
195+
await this.browser.keys(Key.Escape);
196+
await this.browser.pause(PAUSE_TIME);
197+
198+
chai.assert.isTrue(
199+
await contextMenuExists(this.browser, 'Duplicate', /* reverse= */ true),
200+
'The menu should be closed',
201+
);
202+
});
203+
204+
test('Clicking workspace dismisses menu', async function () {
205+
await tabNavigateToWorkspace(this.browser);
206+
await rightClickOnBlock(this.browser, 'draw_circle_1');
207+
await this.browser.pause(PAUSE_TIME);
208+
await focusWorkspace(this.browser);
209+
await this.browser.pause(PAUSE_TIME);
210+
211+
chai.assert.isTrue(
212+
await contextMenuExists(this.browser, 'Duplicate', /* reverse= */ true),
213+
'The menu should be closed',
214+
);
215+
});
73216
});

test/webdriverio/test/test_setup.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ export async function isDragging(
504504
}
505505

506506
/**
507-
* Returns the result of the specificied action precondition.
507+
* Returns the result of the specified action precondition.
508508
*
509509
* @param browser The active WebdriverIO Browser object.
510510
* @param action The action to check the precondition for.
@@ -555,3 +555,49 @@ export async function contextMenuExists(
555555
const item = await browser.$(`div=${itemText}`);
556556
return await item.waitForExist({timeout: 200, reverse: reverse});
557557
}
558+
559+
/**
560+
* Get a list of the text content of each displayed context menu item.
561+
*
562+
* Omits any keyboard shortcuts inside parentheses from all item text for
563+
* testing consistency across platforms.
564+
*
565+
* @param browser The active WebdriverIO Browser object.
566+
* @return A list of the text content of each displayed context menu item.
567+
*/
568+
export async function getContextMenuItemNames(
569+
browser: WebdriverIO.Browser,
570+
): Promise<string[]> {
571+
const items = await browser.$$(`.blocklyContextMenu .blocklyMenuItemContent`);
572+
return await items.map(async (e) =>
573+
(await e.getText()).replace(/\s*\([^)]+\)/, ''),
574+
);
575+
}
576+
577+
/**
578+
* Right-clicks on a block with the provided ID in the main workspace.
579+
*
580+
* @param browser The active WebdriverIO Browser object.
581+
* @param blockId The ID of the block to right click on.
582+
*/
583+
export async function rightClickOnBlock(
584+
browser: WebdriverIO.Browser,
585+
blockId: string,
586+
) {
587+
const elem = await browser.$(`[data-id="${blockId}"]`);
588+
await elem.click({button: 'right'});
589+
}
590+
591+
/**
592+
* Right-clicks on a block with the provided type in the flyout.
593+
*
594+
* @param browser The active WebdriverIO Browser object.
595+
* @param blockType The name of the type block to right click on.
596+
*/
597+
export async function rightClickOnFlyoutBlockType(
598+
browser: WebdriverIO.Browser,
599+
blockType: string,
600+
) {
601+
const elem = await browser.$(`.blocklyFlyout .${blockType}`);
602+
await elem.click({button: 'right'});
603+
}

0 commit comments

Comments
 (0)