Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/sanity/src/uiTest.ts
4772 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 assert from 'assert';
7
import fs from 'fs';
8
import { Page } from 'playwright';
9
import { TestContext } from './context';
10
11
/**
12
* UI Test helper class to perform common UI actions and verifications.
13
*/
14
export class UITest {
15
private _extensionsDir: string | undefined;
16
private _workspaceDir: string | undefined;
17
private _userDataDir: string | undefined;
18
19
constructor(private readonly context: TestContext) {
20
}
21
22
/**
23
* The directory where extensions are installed.
24
*/
25
public get extensionsDir(): string {
26
return this._extensionsDir ??= this.context.createTempDir();
27
}
28
29
/**
30
* The workspace directory used for testing.
31
*/
32
public get workspaceDir(): string {
33
return this._workspaceDir ??= this.context.createTempDir();
34
}
35
36
/**
37
* The user data directory used for testing.
38
*/
39
public get userDataDir(): string {
40
return this._userDataDir ??= this.context.createTempDir();
41
}
42
43
/**
44
* Run the UI test actions.
45
*/
46
public async run(page: Page) {
47
await this.dismissWorkspaceTrustDialog(page);
48
await this.createTextFile(page);
49
await this.installExtension(page);
50
}
51
52
/**
53
* Validate the results of the UI test actions.
54
*/
55
public validate() {
56
this.verifyTextFileCreated();
57
this.verifyExtensionInstalled();
58
}
59
60
/**
61
* Dismiss the workspace trust dialog.
62
*/
63
private async dismissWorkspaceTrustDialog(page: Page) {
64
this.context.log('Dismissing workspace trust dialog');
65
await page.getByText('Yes, I trust the authors').click();
66
await page.waitForTimeout(500);
67
}
68
69
/**
70
* Run a command from the command palette.
71
*/
72
private async runCommand(page: Page, command: string) {
73
this.context.log(`Running command: ${command}`);
74
await page.keyboard.press('F1');
75
await page.getByPlaceholder(/^Type the name of a command/).fill(`>${command}`);
76
await page.locator('span.monaco-highlighted-label', { hasText: new RegExp(`^${command}$`) }).click();
77
await page.waitForTimeout(500);
78
}
79
80
/**
81
* Create a new text file in the editor with some content and save it.
82
*/
83
private async createTextFile(page: Page) {
84
await this.runCommand(page, 'View: Show Explorer');
85
86
this.context.log('Clicking New File button');
87
await page.getByLabel('New File...').click();
88
89
this.context.log('Typing file name');
90
await page.getByRole('textbox', { name: /^Type file name/ }).fill('helloWorld.txt');
91
await page.keyboard.press('Enter');
92
93
this.context.log('Focusing the code editor');
94
await page.getByText(/Start typing/).focus();
95
96
this.context.log('Typing some content into the file');
97
await page.keyboard.type('Hello, World!');
98
99
await this.runCommand(page, 'File: Save');
100
await page.waitForTimeout(1000);
101
}
102
103
/**
104
* Verify that the text file was created with the expected content.
105
*/
106
private verifyTextFileCreated() {
107
this.context.log('Verifying file contents');
108
const filePath = `${this.workspaceDir}/helloWorld.txt`;
109
const fileContents = fs.readFileSync(filePath, 'utf-8');
110
assert.strictEqual(fileContents, 'Hello, World!');
111
}
112
113
/**
114
* Install GitHub Pull Requests extension from the Extensions view.
115
*/
116
private async installExtension(page: Page) {
117
await this.runCommand(page, 'View: Show Extensions');
118
119
this.context.log('Typing extension name to search for');
120
await page.getByText('Search Extensions in Marketplace').focus();
121
await page.keyboard.type('GitHub Pull Requests');
122
123
this.context.log('Clicking Install on the first extension in the list');
124
await page.locator('.extension-list-item').getByText(/^GitHub Pull Requests$/).waitFor();
125
await page.locator('.extension-action:not(.disabled)', { hasText: /Install/ }).first().click();
126
127
this.context.log('Waiting for extension to be installed');
128
await page.locator('.extension-action:not(.disabled)', { hasText: /Uninstall/ }).waitFor();
129
}
130
131
/**
132
* Verify that the GitHub Pull Requests extension is installed.
133
*/
134
private verifyExtensionInstalled() {
135
this.context.log('Verifying extension is installed');
136
const extensions = fs.readdirSync(this.extensionsDir);
137
const hasExtension = extensions.some(ext => ext.startsWith('github.vscode-pull-request-github'));
138
assert.strictEqual(hasExtension, true);
139
}
140
}
141
142