Skip to content

Commit a09aa99

Browse files
sergiolmseamodio
authored andcommitted
add: Playwright sample tests
put electron launch code in a setup file
1 parent 59fb900 commit a09aa99

File tree

5 files changed

+117
-40
lines changed

5 files changed

+117
-40
lines changed

tests/playwright/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { downloadAndUnzipVSCode } from '@vscode/test-electron/out/download';
1+
import { downloadAndUnzipVSCode } from '@vscode/test-electron';
22

33
export default async () => {
44
await downloadAndUnzipVSCode('insiders');

tests/playwright/specs/baseTest.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { test as base, type Page, _electron } from '@playwright/test';
2+
import { downloadAndUnzipVSCode } from '@vscode/test-electron/out/download';
3+
export { expect } from '@playwright/test';
4+
import path from 'path';
5+
import os from 'os';
6+
import fs from 'fs';
7+
import { spawnSync } from 'child_process';
8+
9+
export type TestOptions = {
10+
vscodeVersion: string;
11+
};
12+
13+
type TestFixtures = TestOptions & {
14+
page: Page;
15+
createTestProject: () => Promise<string>;
16+
createTmpDir: () => Promise<string>;
17+
};
18+
19+
let testProjectPath: string;
20+
export const test = base.extend<TestFixtures>({
21+
vscodeVersion: ['insiders', { option: true }],
22+
page: async ({ vscodeVersion, createTestProject, createTmpDir }, use) => {
23+
const defaultCachePath = await createTmpDir();
24+
const vscodePath = await downloadAndUnzipVSCode(vscodeVersion);
25+
testProjectPath = await createTestProject();
26+
27+
const electronApp = await _electron.launch({
28+
executablePath: vscodePath,
29+
// Got it from https://github.com/microsoft/vscode-test/blob/0ec222ef170e102244569064a12898fb203e5bb7/lib/runTest.ts#L126-L160
30+
args: [
31+
'--no-sandbox', // https://github.com/microsoft/vscode/issues/84238
32+
'--disable-gpu-sandbox', // https://github.com/microsoft/vscode-test/issues/221
33+
'--disable-updates', // https://github.com/microsoft/vscode-test/issues/120
34+
'--skip-welcome',
35+
'--skip-release-notes',
36+
'--disable-workspace-trust',
37+
`--extensionDevelopmentPath=${path.join(__dirname, '../../../')}`,
38+
`--extensions-dir=${path.join(defaultCachePath, 'extensions')}`,
39+
`--user-data-dir=${path.join(defaultCachePath, 'user-data')}`,
40+
testProjectPath,
41+
],
42+
});
43+
44+
const page = await electronApp.firstWindow();
45+
await page.context().tracing.start({
46+
screenshots: true,
47+
snapshots: true,
48+
title: test.info().title,
49+
});
50+
51+
await use(page);
52+
53+
const tracePath = test.info().outputPath('trace.zip');
54+
await page.context().tracing.stop({ path: tracePath });
55+
test.info().attachments.push({ name: 'trace', path: tracePath, contentType: 'application/zip' });
56+
await electronApp.close();
57+
58+
const logPath = path.join(defaultCachePath, 'user-data');
59+
if (fs.existsSync(logPath)) {
60+
const logOutputPath = test.info().outputPath('vscode-logs');
61+
await fs.promises.cp(logPath, logOutputPath, { recursive: true });
62+
}
63+
},
64+
createTestProject: async ({ createTmpDir }, use) => {
65+
await use(async () => {
66+
// We want to be outside of the project directory to avoid already installed dependencies.
67+
const projectPath = await createTmpDir();
68+
if (fs.existsSync(projectPath)) await fs.promises.rm(projectPath, { recursive: true });
69+
console.log(`Creating project in ${projectPath}`);
70+
await fs.promises.mkdir(projectPath);
71+
spawnSync(`yarn init playwright@latest --yes -- --quiet --browser=chromium --gha --install-deps`, {
72+
cwd: projectPath,
73+
stdio: 'inherit',
74+
shell: true,
75+
});
76+
spawnSync(`git init .`, {
77+
cwd: projectPath,
78+
});
79+
return projectPath;
80+
});
81+
},
82+
createTmpDir: async ({}, use) => {
83+
const tempDirs: string[] = [];
84+
await use(async () => {
85+
const tempDir = await fs.promises.realpath(await fs.promises.mkdtemp(path.join(os.tmpdir(), 'gltest-')));
86+
tempDirs.push(tempDir);
87+
return tempDir;
88+
});
89+
for (const tempDir of tempDirs) await fs.promises.rm(tempDir, { recursive: true });
90+
},
91+
});

tests/playwright/specs/basic.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { test, expect } from './baseTest';
2+
3+
test.describe('Test GitLens installation', () => {
4+
test('should display GitLens Welcome page after installation', async ({ page }) => {
5+
const title = await page.textContent('.tab a');
6+
expect(title).toBe('Welcome to GitLens');
7+
});
8+
9+
test('should contain GitLens & GitLens Inspect icons in activity bar', async ({ page }) => {
10+
await page.getByRole('tab', { name: 'GitLens Inspect' }).waitFor();
11+
const gitlensIcons = await page.getByRole('tab', { name: 'GitLens' });
12+
expect(gitlensIcons).toHaveCount(2);
13+
14+
expect(await page.title()).toContain('[Extension Development Host] gltest-');
15+
});
16+
});

tests/playwright/specs/basic2.test.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6095,6 +6095,15 @@ [email protected], playwright@^1.45.0:
60956095
optionalDependencies:
60966096
fsevents "2.3.2"
60976097

6098+
playwright@^1.45.1:
6099+
version "1.45.1"
6100+
resolved "https://registry.npmjs.org/playwright/-/playwright-1.45.1.tgz#aaa6b0d6db14796b599d80c6679e63444e942534"
6101+
integrity sha512-Hjrgae4kpSQBr98nhCj3IScxVeVUixqj+5oyif8TdIn2opTCPEzqAqNMeK42i3cWDCVu9MI+ZsGWw+gVR4ISBg==
6102+
dependencies:
6103+
playwright-core "1.45.1"
6104+
optionalDependencies:
6105+
fsevents "2.3.2"
6106+
60986107
plimit-lit@^1.2.6:
60996108
version "1.6.1"
61006109
resolved "https://registry.yarnpkg.com/plimit-lit/-/plimit-lit-1.6.1.tgz#a34594671b31ee8e93c72d505dfb6852eb72374a"

0 commit comments

Comments
 (0)