forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_open-helpers.ts
156 lines (131 loc) · 5.33 KB
/
quick_open-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {$$, click, getBrowserAndPages, platform, typeText, waitFor} from '../../shared/helper.js';
import {SourceFileEvents, waitForSourceFiles} from './sources-helpers.js';
export const QUICK_OPEN_SELECTOR = '[aria-label="Quick open"]';
const QUICK_OPEN_ITEMS_SELECTOR = '.filtered-list-widget-item-wrapper';
const QUICK_OPEN_ITEM_TITLE_SELECTOR = '.filtered-list-widget-title';
const QUICK_OPEN_SELECTED_ITEM_SELECTOR = `${QUICK_OPEN_ITEMS_SELECTOR}.selected`;
export const openCommandMenu = async () => {
const {frontend} = getBrowserAndPages();
switch (platform) {
case 'mac':
await frontend.keyboard.down('Meta');
await frontend.keyboard.down('Shift');
break;
case 'linux':
case 'win32':
await frontend.keyboard.down('Control');
await frontend.keyboard.down('Shift');
break;
}
await frontend.keyboard.press('P');
switch (platform) {
case 'mac':
await frontend.keyboard.up('Meta');
await frontend.keyboard.up('Shift');
break;
case 'linux':
case 'win32':
await frontend.keyboard.up('Control');
await frontend.keyboard.up('Shift');
break;
}
await waitFor(QUICK_OPEN_SELECTOR);
};
export const openFileQuickOpen = async () => {
const {frontend} = getBrowserAndPages();
const modifierKey = platform === 'mac' ? 'Meta' : 'Control';
await frontend.keyboard.down(modifierKey);
await frontend.keyboard.press('P');
await frontend.keyboard.up(modifierKey);
await waitFor(QUICK_OPEN_SELECTOR);
};
export async function readQuickOpenResults(): Promise<string[]> {
const items = await $$('.filtered-list-widget-title');
return await Promise.all(items.map(element => element.evaluate(el => el.textContent as string)));
}
export const openFileWithQuickOpen = async (sourceFile: string, filePosition = 0) => {
await waitForSourceFiles(
SourceFileEvents.SOURCE_FILE_LOADED,
files => files.some(f => f.endsWith(sourceFile)),
async () => {
await openFileQuickOpen();
await typeIntoQuickOpen(sourceFile);
const firstItem = await getMenuItemAtPosition(filePosition);
await firstItem.click();
},
);
};
export async function runCommandWithQuickOpen(command: string): Promise<void> {
const {frontend} = getBrowserAndPages();
await openCommandMenu();
await frontend.keyboard.type(command);
await frontend.keyboard.press('Enter');
}
export const openGoToLineQuickOpen = async () => {
const {frontend} = getBrowserAndPages();
await frontend.keyboard.down('Control');
await frontend.keyboard.press('G');
await frontend.keyboard.up('Control');
await waitFor(QUICK_OPEN_SELECTOR);
};
export const showSnippetsAutocompletion = async () => {
const {frontend} = getBrowserAndPages();
// Clear the `>` character, as snippets use a `!` instead
await frontend.keyboard.press('Backspace');
await typeText('!');
};
export async function getAvailableSnippets() {
const quickOpenElement = await waitFor(QUICK_OPEN_SELECTOR);
const snippetsDOMElements = await $$(QUICK_OPEN_ITEMS_SELECTOR, quickOpenElement);
const snippets = await Promise.all(snippetsDOMElements.map(elem => elem.evaluate(elem => elem.textContent)));
return snippets;
}
export async function getMenuItemAtPosition(position: number) {
const quickOpenElement = await waitFor(QUICK_OPEN_SELECTOR);
await waitFor(QUICK_OPEN_ITEM_TITLE_SELECTOR);
const itemsHandles = await $$(QUICK_OPEN_ITEMS_SELECTOR, quickOpenElement);
const item = itemsHandles[position];
if (!item) {
assert.fail(`Quick open: could not find item at position: ${position}.`);
}
return item;
}
export async function getMenuItemTitleAtPosition(position: number) {
const quickOpenElement = await waitFor(QUICK_OPEN_SELECTOR);
await waitFor(QUICK_OPEN_ITEM_TITLE_SELECTOR);
const itemsHandles = await $$(QUICK_OPEN_ITEM_TITLE_SELECTOR, quickOpenElement);
const item = itemsHandles[position];
if (!item) {
assert.fail(`Quick open: could not find item at position: ${position}.`);
}
const title = await item.evaluate(elem => elem.textContent);
return title;
}
export const closeDrawer = async () => {
await click('[aria-label="Close drawer"]');
};
export const getSelectedItemText = async () => {
const quickOpenElement = await waitFor(QUICK_OPEN_SELECTOR);
const selectedRow = await waitFor(QUICK_OPEN_SELECTED_ITEM_SELECTOR, quickOpenElement);
const textContent = await selectedRow.getProperty('textContent');
if (!textContent) {
assert.fail('Quick open: could not get selected item textContent');
}
return await textContent.jsonValue();
};
export async function typeIntoQuickOpen(query: string, expectEmptyResults?: boolean) {
await openFileQuickOpen();
const prompt = await waitFor('[aria-label="Quick open prompt"]');
await prompt.type(query);
if (expectEmptyResults) {
await waitFor('.filtered-list-widget :not(.hidden).not-found-text');
} else {
// Because each highlighted character is in its own div, we can count the highlighted
// characters in one item to see that the list reflects the full query.
const highlightSelector = new Array(query.length).fill('.highlight').join(' ~ ');
await waitFor('.filtered-list-widget-title ' + highlightSelector);
}
}