Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/automation/src/debug.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Viewlet } from './viewlet';
7
import { Commands } from './workbench';
8
import { Code, findElement } from './code';
9
import { Editors } from './editors';
10
import { Editor } from './editor';
11
import { IElement } from './driver';
12
13
const VIEWLET = 'div[id="workbench.view.debug"]';
14
const DEBUG_VIEW = `${VIEWLET}`;
15
const CONFIGURE = `div[id="workbench.parts.sidebar"] .actions-container .codicon-gear`;
16
const STOP = `.debug-toolbar .action-label[title*="Stop"]`;
17
const STEP_OVER = `.debug-toolbar .action-label[title*="Step Over"]`;
18
const STEP_IN = `.debug-toolbar .action-label[title*="Step Into"]`;
19
const STEP_OUT = `.debug-toolbar .action-label[title*="Step Out"]`;
20
const CONTINUE = `.debug-toolbar .action-label[title*="Continue"]`;
21
const GLYPH_AREA = '.margin-view-overlays>:nth-child';
22
const BREAKPOINT_GLYPH = '.codicon-debug-breakpoint';
23
const PAUSE = `.debug-toolbar .action-label[title*="Pause"]`;
24
const DEBUG_STATUS_BAR = `.statusbar.debugging`;
25
const NOT_DEBUG_STATUS_BAR = `.statusbar:not(debugging)`;
26
const TOOLBAR_HIDDEN = `.debug-toolbar[aria-hidden="true"]`;
27
const STACK_FRAME = `${VIEWLET} .monaco-list-row .stack-frame`;
28
const SPECIFIC_STACK_FRAME = (filename: string) => `${STACK_FRAME} .file[title*="${filename}"]`;
29
const VARIABLE = `${VIEWLET} .debug-variables .monaco-list-row .expression`;
30
const CONSOLE_OUTPUT = `.repl .output.expression .value`;
31
const CONSOLE_EVALUATION_RESULT = `.repl .evaluation-result.expression .value`;
32
const CONSOLE_LINK = `.repl .value a.link`;
33
34
const REPL_FOCUSED_NATIVE_EDIT_CONTEXT = '.repl-input-wrapper .monaco-editor .native-edit-context';
35
const REPL_FOCUSED_TEXTAREA = '.repl-input-wrapper .monaco-editor textarea';
36
37
export interface IStackFrame {
38
name: string;
39
lineNumber: number;
40
}
41
42
function toStackFrame(element: IElement): IStackFrame {
43
const name = findElement(element, e => /\bfile-name\b/.test(e.className))!;
44
const line = findElement(element, e => /\bline-number\b/.test(e.className))!;
45
const lineNumber = line.textContent ? parseInt(line.textContent.split(':').shift() || '0') : 0;
46
47
return {
48
name: name.textContent || '',
49
lineNumber
50
};
51
}
52
53
export class Debug extends Viewlet {
54
55
constructor(code: Code, private commands: Commands, private editors: Editors, private editor: Editor) {
56
super(code);
57
}
58
59
async openDebugViewlet(): Promise<any> {
60
await this.code.dispatchKeybinding(process.platform === 'darwin' ? 'cmd+shift+d' : 'ctrl+shift+d', async () => {
61
await this.code.waitForElement(DEBUG_VIEW);
62
});
63
}
64
65
async configure(): Promise<any> {
66
await this.code.waitAndClick(CONFIGURE);
67
await this.editors.waitForEditorFocus('launch.json');
68
}
69
70
async setBreakpointOnLine(lineNumber: number): Promise<any> {
71
await this.code.waitForElement(`${GLYPH_AREA}(${lineNumber})`);
72
await this.code.waitAndClick(`${GLYPH_AREA}(${lineNumber})`, 5, 5);
73
await this.code.waitForElement(BREAKPOINT_GLYPH);
74
}
75
76
async startDebugging(): Promise<number> {
77
await this.code.dispatchKeybinding('f5', async () => {
78
await this.code.waitForElement(PAUSE);
79
await this.code.waitForElement(DEBUG_STATUS_BAR);
80
});
81
const portPrefix = 'Port: ';
82
83
const output = await this.waitForOutput(output => output.some(line => line.indexOf(portPrefix) >= 0));
84
const lastOutput = output.filter(line => line.indexOf(portPrefix) >= 0)[0];
85
86
return lastOutput ? parseInt(lastOutput.substr(portPrefix.length)) : 3000;
87
}
88
89
async stepOver(): Promise<any> {
90
await this.code.waitAndClick(STEP_OVER);
91
}
92
93
async stepIn(): Promise<any> {
94
await this.code.waitAndClick(STEP_IN);
95
}
96
97
async stepOut(): Promise<any> {
98
await this.code.waitAndClick(STEP_OUT);
99
}
100
101
async continue(): Promise<any> {
102
await this.code.waitAndClick(CONTINUE);
103
await this.waitForStackFrameLength(0);
104
}
105
106
async stopDebugging(): Promise<any> {
107
await this.code.waitAndClick(STOP);
108
await this.code.waitForElement(TOOLBAR_HIDDEN);
109
await this.code.waitForElement(NOT_DEBUG_STATUS_BAR);
110
}
111
112
async waitForStackFrame(func: (stackFrame: IStackFrame) => boolean, message: string): Promise<IStackFrame> {
113
const elements = await this.code.waitForElements(STACK_FRAME, true, elements => elements.some(e => func(toStackFrame(e))));
114
return elements.map(toStackFrame).filter(s => func(s))[0];
115
}
116
117
async waitForStackFrameLength(length: number): Promise<any> {
118
await this.code.waitForElements(STACK_FRAME, false, result => result.length === length);
119
}
120
121
async focusStackFrame(name: string, message: string): Promise<any> {
122
await this.code.waitAndClick(SPECIFIC_STACK_FRAME(name), 0, 0);
123
await this.editors.waitForTab(name);
124
}
125
126
async waitForReplCommand(text: string, accept: (result: string) => boolean): Promise<void> {
127
await this.commands.runCommand('Debug: Focus on Debug Console View');
128
const selector = !this.code.editContextEnabled ? REPL_FOCUSED_TEXTAREA : REPL_FOCUSED_NATIVE_EDIT_CONTEXT;
129
await this.code.waitForActiveElement(selector);
130
await this.code.waitForSetValue(selector, text);
131
132
// Wait for the keys to be picked up by the editor model such that repl evaluates what just got typed
133
await this.editor.waitForEditorContents('debug:replinput', s => s.indexOf(text) >= 0);
134
await this.code.dispatchKeybinding('enter', async () => {
135
await this.code.waitForElements(CONSOLE_EVALUATION_RESULT, false,
136
elements => !!elements.length && accept(elements[elements.length - 1].textContent));
137
});
138
}
139
140
// Different node versions give different number of variables. As a workaround be more relaxed when checking for variable count
141
async waitForVariableCount(count: number, alternativeCount: number): Promise<void> {
142
await this.code.waitForElements(VARIABLE, false, els => els.length === count || els.length === alternativeCount);
143
}
144
145
async waitForLink(): Promise<void> {
146
await this.code.waitForElement(CONSOLE_LINK);
147
}
148
149
private async waitForOutput(fn: (output: string[]) => boolean): Promise<string[]> {
150
const elements = await this.code.waitForElements(CONSOLE_OUTPUT, false, elements => fn(elements.map(e => e.textContent)));
151
return elements.map(e => e.textContent);
152
}
153
}
154
155