Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/automation/src/notebook.ts
5222 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 { Code } from './code';
7
import { QuickAccess } from './quickaccess';
8
import { QuickInput } from './quickinput';
9
10
const anyRowSelector = `.notebook-editor .monaco-list-row`;
11
const activeRowSelector = `${anyRowSelector}.focused`;
12
const activeMarkdownRowSelector = `${activeRowSelector}.markdown-cell-row`;
13
14
export class Notebook {
15
16
constructor(
17
private readonly quickAccess: QuickAccess,
18
private readonly quickInput: QuickInput,
19
private readonly code: Code) {
20
}
21
22
async openNotebook() {
23
await this.code.whenWorkbenchRestored();
24
await this.quickAccess.openFileQuickAccessAndWait('notebook.ipynb', 1);
25
await this.quickInput.selectQuickInputElement(0);
26
27
await this.code.waitForElement(anyRowSelector);
28
await this.focusFirstCell();
29
await this.code.waitForElement(activeRowSelector);
30
}
31
32
async focusNextCell() {
33
await this.code.dispatchKeybinding('down', async () => {
34
// TODO: Add an accept callback to verify the keybinding was successful
35
});
36
}
37
38
async focusFirstCell() {
39
await this.quickAccess.runCommand('notebook.focusTop');
40
}
41
42
async editCell() {
43
await this.code.dispatchKeybinding('enter', async () => {
44
// TODO: Add an accept callback to verify the keybinding was successful
45
});
46
}
47
48
async stopEditingCell() {
49
await this.quickAccess.runCommand('notebook.cell.quitEdit');
50
}
51
52
async waitForTypeInEditor(text: string): Promise<any> {
53
const editor = `${activeRowSelector} .monaco-editor`;
54
55
await this.code.waitForElement(editor);
56
57
const editContext = `${editor} ${!this.code.editContextEnabled ? 'textarea' : '.native-edit-context'}`;
58
await this.code.waitForActiveElement(editContext);
59
60
await this.code.waitForTypeInEditor(editContext, text);
61
62
await this._waitForActiveCellEditorContents(c => c.indexOf(text) > -1);
63
}
64
65
async waitForActiveCellEditorContents(contents: string): Promise<any> {
66
return this._waitForActiveCellEditorContents(str => str === contents);
67
}
68
69
private async _waitForActiveCellEditorContents(accept: (contents: string) => boolean): Promise<any> {
70
const selector = `${activeRowSelector} .monaco-editor .view-lines`;
71
return this.code.waitForTextContent(selector, undefined, c => accept(c.replace(/\u00a0/g, ' ')));
72
}
73
74
async waitForMarkdownContents(markdownSelector: string, text: string): Promise<void> {
75
const selector = `${activeMarkdownRowSelector} ${markdownSelector}`;
76
await this.code.waitForTextContent(selector, text);
77
}
78
79
async insertNotebookCell(kind: 'markdown' | 'code'): Promise<void> {
80
if (kind === 'markdown') {
81
await this.quickAccess.runCommand('notebook.cell.insertMarkdownCellBelow');
82
} else {
83
await this.quickAccess.runCommand('notebook.cell.insertCodeCellBelow');
84
}
85
}
86
87
async deleteActiveCell(): Promise<void> {
88
await this.quickAccess.runCommand('notebook.cell.delete');
89
}
90
91
async focusInCellOutput(): Promise<void> {
92
await this.quickAccess.runCommand('notebook.cell.focusInOutput');
93
await this.code.waitForActiveElement('iframe.webview.ready');
94
}
95
96
async focusOutCellOutput(): Promise<void> {
97
await this.quickAccess.runCommand('notebook.cell.focusOutOutput');
98
}
99
100
async executeActiveCell(): Promise<void> {
101
await this.quickAccess.runCommand('notebook.cell.execute');
102
}
103
104
async executeCellAction(selector: string): Promise<void> {
105
await this.code.waitAndClick(selector);
106
}
107
}
108
109