forked from irinazheltisheva/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.ts
More file actions
152 lines (124 loc) · 4.12 KB
/
Copy pathapplication.ts
File metadata and controls
152 lines (124 loc) · 4.12 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import * as path from 'path';
import { Workbench } from './workbench';
import { Code, spawn, SpawnOptions } from './code';
import { Logger } from './logger';
export const enum Quality {
Dev,
Insiders,
Stable
}
export interface ApplicationOptions extends SpawnOptions {
quality: Quality;
workspacePath: string;
waitTime: number;
screenshotsPath: string | null;
}
export class Application {
private _code: Code | undefined;
private _workbench: Workbench | undefined;
constructor(private options: ApplicationOptions) {
this._workspacePathOrFolder = options.workspacePath;
}
get quality(): Quality {
return this.options.quality;
}
get code(): Code {
return this._code!;
}
get workbench(): Workbench {
return this._workbench!;
}
get logger(): Logger {
return this.options.logger;
}
get remote(): boolean {
return !!this.options.remote;
}
private _workspacePathOrFolder: string;
get workspacePathOrFolder(): string {
return this._workspacePathOrFolder;
}
get extensionsPath(): string {
return this.options.extensionsPath;
}
get userDataPath(): string {
return this.options.userDataDir;
}
async start(expectWalkthroughPart = true): Promise<any> {
await this._start();
await this.code.waitForElement('.explorer-folders-view');
if (expectWalkthroughPart) {
await this.code.waitForActiveElement(`.editor-instance[data-editor-id="workbench.editor.walkThroughPart"] > div > div[tabIndex="0"]`);
}
}
async restart(options: { workspaceOrFolder?: string, extraArgs?: string[] }): Promise<any> {
await this.stop();
await new Promise(c => setTimeout(c, 1000));
await this._start(options.workspaceOrFolder, options.extraArgs);
}
private async _start(workspaceOrFolder = this.workspacePathOrFolder, extraArgs: string[] = []): Promise<any> {
this._workspacePathOrFolder = workspaceOrFolder;
await this.startApplication(extraArgs);
await this.checkWindowReady();
}
async reload(): Promise<any> {
this.code.reload()
.catch(err => null); // ignore the connection drop errors
// needs to be enough to propagate the 'Reload Window' command
await new Promise(c => setTimeout(c, 1500));
await this.checkWindowReady();
}
async stop(): Promise<any> {
if (this._code) {
await this._code.exit();
this._code.dispose();
this._code = undefined;
}
}
async captureScreenshot(name: string): Promise<void> {
if (this.options.screenshotsPath) {
const raw = await this.code.capturePage();
const buffer = Buffer.from(raw, 'base64');
const screenshotPath = path.join(this.options.screenshotsPath, `${name}.png`);
if (this.options.log) {
this.logger.log('*** Screenshot recorded:', screenshotPath);
}
fs.writeFileSync(screenshotPath, buffer);
}
}
private async startApplication(extraArgs: string[] = []): Promise<any> {
this._code = await spawn({
codePath: this.options.codePath,
workspacePath: this.workspacePathOrFolder,
userDataDir: this.options.userDataDir,
extensionsPath: this.options.extensionsPath,
logger: this.options.logger,
verbose: this.options.verbose,
log: this.options.log,
extraArgs,
remote: this.options.remote,
web: this.options.web,
browser: this.options.browser
});
this._workbench = new Workbench(this._code, this.userDataPath);
}
private async checkWindowReady(): Promise<any> {
if (!this.code) {
console.error('No code instance found');
return;
}
await this.code.waitForWindowIds(ids => ids.length > 0);
await this.code.waitForElement('.monaco-workbench');
if (this.remote) {
await this.code.waitForTextContent('.monaco-workbench .statusbar-item[id="status.host"]', ' TestResolver');
}
// wait a bit, since focus might be stolen off widgets
// as soon as they open (e.g. quick access)
await new Promise(c => setTimeout(c, 1000));
}
}