Skip to content

Commit 577d54b

Browse files
committed
Added Playwright test for bruno-testbench, few sanity tests and improvements
- Trace will capture snapshots now - Added ability to add init Electron user-data, preferences and other app settings. - Improved test Fixtures - Use tempdir for Electron user-data - Ability to reuse app instance for a given init user-data by placing them in a folder(`pageWithUserData` Fixture) - Ability to create tests with fresh user-data(`newPage` Fixture) - Improved logging - Improved the env vars to customize the Electron user-data-path
1 parent afaebf6 commit 577d54b

File tree

10 files changed

+286
-41
lines changed

10 files changed

+286
-41
lines changed

contributing.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,13 @@ npm run dev
9999
```
100100

101101
#### Customize Electron `userData` path
102-
If `ELECTRON_APP_NAME` env-variable is present and its development mode, then the `appName` and `userData` path is modified accordingly.
102+
If `ELECTRON_USER_DATA_PATH` env-variable is present and its development mode, then `userData` path is modified accordingly.
103103

104104
e.g.
105105
```sh
106-
ELECTRON_APP_NAME=bruno-dev npm run dev:electron
106+
ELECTRON_USER_DATA_PATH=$(realpath ~/Desktop/bruno-test) npm run dev:electron
107107
```
108-
109-
> This doesn't change the name of the window or the names in lot of other places, only the name used by Electron internally.
108+
This will create a `bruno-test` folder on your Desktop and use it as the `userData` path.
110109

111110
### Troubleshooting
112111

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test, expect } from '../../playwright';
2+
3+
test('Check if the logo on top left is visible', async ({ page }) => {
4+
await expect(page.getByRole('button', { name: 'bruno' })).toBeVisible();
5+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { test, expect } from '../../playwright';
2+
3+
test('Create new collection and add a simple HTTP request', async ({ page, createTmpDir }) => {
4+
await page.getByLabel('Create Collection').click();
5+
await page.getByLabel('Name').click();
6+
await page.getByLabel('Name').fill('test-collection');
7+
await page.getByLabel('Name').press('Tab');
8+
await page.getByLabel('Location').fill(await createTmpDir('test-collection'));
9+
await page.getByRole('button', { name: 'Create', exact: true }).click();
10+
await page.getByText('test-collection').click();
11+
await page.getByLabel('Safe ModeBETA').check();
12+
await page.getByRole('button', { name: 'Save' }).click();
13+
await page.locator('#create-new-tab').getByRole('img').click();
14+
await page.getByPlaceholder('Request Name').fill('r1');
15+
await page.getByPlaceholder('Request URL').click();
16+
await page.getByPlaceholder('Request URL').fill('http://localhost:8081');
17+
await page.getByRole('button', { name: 'Create' }).click();
18+
await page.locator('pre').filter({ hasText: 'http://localhost:' }).click();
19+
await page.locator('textarea').fill('/ping');
20+
await page.locator('#send-request').getByRole('img').nth(2).click();
21+
22+
await expect(page.getByRole('main')).toContainText('200 OK');
23+
24+
await page.getByRole('tab', { name: 'GET r1' }).locator('circle').click();
25+
await page.getByRole('button', { name: 'Save', exact: true }).click();
26+
await page.getByText('GETr1').click();
27+
await page.getByRole('button', { name: 'Clear response' }).click();
28+
await page.locator('body').press('ControlOrMeta+Enter');
29+
30+
await expect(page.getByRole('main')).toContainText('200 OK');
31+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"maximized": true,
3+
"lastOpenedCollections": ["{{projectRoot}}/packages/bruno-tests/collection"]
4+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { test, expect } from '../../playwright';
2+
3+
test.describe.parallel('Run Testbench Requests', () => {
4+
test('Run bruno-testbench in Developer Mode', async ({ pageWithUserData: page }) => {
5+
test.setTimeout(2 * 60 * 1000);
6+
7+
await page.getByText('bruno-testbench').click();
8+
await page.getByLabel('Developer Mode(use only if').check();
9+
await page.getByRole('button', { name: 'Save' }).click();
10+
await page.locator('.environment-selector').nth(1).click();
11+
await page.locator('.dropdown-item').getByText('Prod').click();
12+
await page.locator('.collection-actions').hover();
13+
await page.locator('.collection-actions .icon').click();
14+
await page.getByText('Run', { exact: true }).click();
15+
await page.getByRole('button', { name: 'Run Collection' }).click();
16+
await page.getByRole('button', { name: 'Run Again' }).waitFor({ timeout: 2 * 60 * 1000 });
17+
18+
const result = await page.getByText('Total Requests: ').innerText();
19+
const [totalRequests, passed, failed, skipped] = result
20+
.match(/Total Requests: (\d+), Passed: (\d+), Failed: (\d+), Skipped: (\d+)/)
21+
.slice(1);
22+
23+
await expect(parseInt(failed)).toBe(0);
24+
await expect(parseInt(passed)).toBe(parseInt(totalRequests) - parseInt(skipped) - 1);
25+
});
26+
27+
test.fixme('Run bruno-testbench in Safe Mode', async ({ pageWithUserData: page }) => {
28+
test.setTimeout(2 * 60 * 1000);
29+
30+
await page.getByText('bruno-testbench').click();
31+
await page.getByLabel('Safe ModeBETA').check();
32+
await page.getByRole('button', { name: 'Save' }).click();
33+
await page.locator('.environment-selector').nth(1).click();
34+
await page.locator('.dropdown-item').getByText('Prod').click();
35+
await page.locator('.collection-actions').hover();
36+
await page.locator('.collection-actions .icon').click();
37+
await page.getByText('Run', { exact: true }).click();
38+
await page.getByRole('button', { name: 'Run Collection' }).click();
39+
await page.getByRole('button', { name: 'Run Again' }).waitFor({ timeout: 2 * 60 * 1000 });
40+
41+
const result = await page.getByText('Total Requests: ').innerText();
42+
const [totalRequests, passed, failed, skipped] = result
43+
.match(/Total Requests: (\d+), Passed: (\d+), Failed: (\d+), Skipped: (\d+)/)
44+
.slice(1);
45+
46+
await expect(parseInt(failed)).toBe(0);
47+
await expect(parseInt(passed)).toBe(parseInt(totalRequests) - parseInt(skipped) - 1);
48+
});
49+
});

e2e-tests/test-app-start.spec.ts

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

packages/bruno-electron/src/index.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@ const { format } = require('url');
1414
const { BrowserWindow, app, session, Menu, ipcMain } = require('electron');
1515
const { setContentSecurityPolicy } = require('electron-util');
1616

17-
if (isDev && process.env.ELECTRON_APP_NAME) {
18-
const appName = process.env.ELECTRON_APP_NAME;
19-
const userDataPath = path.join(app.getPath("appData"), appName);
17+
if (isDev && process.env.ELECTRON_USER_DATA_PATH) {
18+
console.debug("`ELECTRON_USER_DATA_PATH` found, modifying `userData` path: \n"
19+
+ `\t${app.getPath("userData")} -> ${process.env.ELECTRON_USER_DATA_PATH}`);
2020

21-
console.log("`ELECTRON_APP_NAME` found, overriding `appName` and `userData` path: \n"
22-
+ `\t${app.getName()} -> ${appName}\n`
23-
+ `\t${app.getPath("userData")} -> ${userDataPath}`);
24-
25-
app.setName(appName);
26-
app.setPath("userData", userDataPath);
21+
app.setPath('userData', process.env.ELECTRON_USER_DATA_PATH);
2722
}
2823

2924
const menuTemplate = require('./app/menu-template');

playwright.config.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { defineConfig, devices } from '@playwright/test';
22

3-
const reporter: string[][string] = [['list'], ['html']];
3+
const reporter: any[] = [['list'], ['html']];
44

55
if (process.env.CI) {
6-
reporter.push(["github"]);
6+
reporter.push(['github']);
77
}
88

9-
109
export default defineConfig({
1110
testDir: './e2e-tests',
1211
fullyParallel: false,
1312
forbidOnly: !!process.env.CI,
1413
retries: process.env.CI ? 1 : 0,
1514
workers: process.env.CI ? undefined : 1,
1615
reporter,
16+
1717
use: {
18-
trace: 'on-first-retry'
18+
trace: process.env.CI ? 'on-first-retry' : 'on'
1919
},
2020

2121
projects: [
@@ -24,9 +24,16 @@ export default defineConfig({
2424
}
2525
],
2626

27-
webServer: {
28-
command: 'npm run dev:web',
29-
url: 'http://localhost:3000',
30-
reuseExistingServer: !process.env.CI
31-
}
27+
webServer: [
28+
{
29+
command: 'npm run dev:web',
30+
url: 'http://localhost:3000',
31+
reuseExistingServer: !process.env.CI
32+
},
33+
{
34+
command: 'npm start --workspace=packages/bruno-tests',
35+
url: 'http://localhost:8081/ping',
36+
reuseExistingServer: !process.env.CI
37+
}
38+
]
3239
});

playwright/electron.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ exports.startApp = async () => {
77
const app = await electron.launch({ args: [electronAppPath] });
88
const context = await app.context();
99

10-
app.process().stdout.on('data', (data) => console.log(data.toString()));
11-
app.process().stderr.on('data', (error) => console.error(error.toString()));
10+
app.process().stdout.on('data', (data) => {
11+
process.stdout.write(data.toString().replace(/^(?=.)/gm, '[Electron] |'));
12+
});
13+
app.process().stderr.on('data', (error) => {
14+
process.stderr.write(error.toString().replace(/^(?=.)/gm, '[Electron] |'));
15+
});
1216
return { app, context };
1317
};

playwright/index.ts

Lines changed: 168 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,179 @@
1-
import { test as baseTest, ElectronApplication, Page } from '@playwright/test';
1+
import { test as baseTest, BrowserContext, ElectronApplication, Page } from '@playwright/test';
2+
import * as path from 'path';
3+
import * as os from 'os';
4+
import * as fs from 'fs';
25

3-
const { startApp } = require('./electron.ts');
6+
const electronAppPath = path.join(__dirname, '../packages/bruno-electron');
47

5-
export const test = baseTest.extend<{ page: Page }, { electronApp: ElectronApplication }>({
6-
electronApp: [
8+
export const test = baseTest.extend<
9+
{
10+
context: BrowserContext;
11+
page: Page;
12+
newPage: Page;
13+
pageWithUserData: Page;
14+
},
15+
{
16+
createTmpDir: (tag?: string) => Promise<string>;
17+
launchElectronApp: (options?: { initUserDataPath?: string }) => Promise<ElectronApplication>;
18+
electronApp: ElectronApplication;
19+
reuseOrLaunchElectronApp: (options?: { initUserDataPath?: string }) => Promise<ElectronApplication>;
20+
}
21+
>({
22+
createTmpDir: [
723
async ({}, use) => {
8-
const { app: electronApp, context } = await startApp();
24+
const dirs: string[] = [];
25+
await use(async (tag?: string) => {
26+
const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), `pw-${tag || ''}-`));
27+
dirs.push(dir);
28+
return dir;
29+
});
30+
await Promise.all(
31+
dirs.map((dir) => fs.promises.rm(dir, { recursive: true, force: true, maxRetries: 10 }).catch((e) => e))
32+
);
33+
},
34+
{ scope: 'worker' }
35+
],
36+
37+
launchElectronApp: [
38+
async ({ playwright, createTmpDir }, use, workerInfo) => {
39+
const apps: ElectronApplication[] = [];
40+
await use(async ({ initUserDataPath } = {}) => {
41+
const userDataPath = await createTmpDir('electron-userdata');
42+
43+
if (initUserDataPath) {
44+
const replacements = {
45+
projectRoot: path.join(__dirname, '..')
46+
};
947

10-
await use(electronApp);
11-
await context.close();
12-
await electronApp.close();
48+
for (const file of await fs.promises.readdir(initUserDataPath)) {
49+
let content = await fs.promises.readFile(path.join(initUserDataPath, file), 'utf-8');
50+
content = content.replace(/{{(\w+)}}/g, (_, key) => {
51+
if (replacements[key]) {
52+
return replacements[key];
53+
} else {
54+
throw new Error(`\tNo replacement for {{${key}}} in ${path.join(initUserDataPath, file)}`);
55+
}
56+
});
57+
await fs.promises.writeFile(path.join(userDataPath, file), content, 'utf-8');
58+
}
59+
}
60+
61+
const app = await playwright._electron.launch({
62+
args: [electronAppPath],
63+
env: {
64+
...process.env,
65+
ELECTRON_USER_DATA_PATH: userDataPath,
66+
}
67+
});
68+
69+
const { workerIndex } = workerInfo;
70+
app.process().stdout.on('data', (data) => {
71+
process.stdout.write(data.toString().replace(/^(?=.)/gm, `[Electron #${workerIndex}] |`));
72+
});
73+
app.process().stderr.on('data', (error) => {
74+
process.stderr.write(error.toString().replace(/^(?=.)/gm, `[Electron #${workerIndex}] |`));
75+
});
76+
77+
apps.push(app);
78+
return app;
79+
});
80+
for (const app of apps) {
81+
await app.context().close();
82+
await app.close();
83+
}
1384
},
1485
{ scope: 'worker' }
1586
],
16-
page: async ({ electronApp }, use) => {
87+
88+
electronApp: [
89+
async ({ launchElectronApp }, use) => {
90+
const app = await launchElectronApp();
91+
await use(app);
92+
},
93+
{ scope: 'worker' }
94+
],
95+
96+
context: async ({ electronApp }, use, testInfo) => {
97+
const context = await electronApp.context();
98+
const tracingOptions = (testInfo as any)._tracing.traceOptions();
99+
if (tracingOptions) {
100+
try {
101+
await context.tracing.start({ screenshots: true, snapshots: true, sources: true });
102+
} catch (e) {}
103+
}
104+
await use(context);
105+
},
106+
107+
page: async ({ electronApp, context }, use, testInfo) => {
17108
const page = await electronApp.firstWindow();
18-
await use(page);
19-
await page.reload();
109+
const tracingOptions = (testInfo as any)._tracing.traceOptions();
110+
if (tracingOptions) {
111+
const tracePath = testInfo.outputPath(`trace-${testInfo.testId}.zip`);
112+
await context.tracing.startChunk();
113+
await use(page);
114+
await context.tracing.stopChunk({ path: tracePath });
115+
await testInfo.attach('trace', { path: tracePath });
116+
} else {
117+
await use(page);
118+
}
119+
},
120+
121+
newPage: async ({ launchElectronApp }, use, testInfo) => {
122+
const app = await launchElectronApp();
123+
const context = await app.context();
124+
const page = await app.firstWindow();
125+
const tracingOptions = (testInfo as any)._tracing.traceOptions();
126+
if (tracingOptions) {
127+
const tracePath = testInfo.outputPath(`trace-${testInfo.testId}.zip`);
128+
await context.tracing.start({ screenshots: true, snapshots: true, sources: true });
129+
await use(page);
130+
await context.tracing.stop({ path: tracePath });
131+
await testInfo.attach('trace', { path: tracePath });
132+
} else {
133+
await use(page);
134+
}
135+
},
136+
137+
reuseOrLaunchElectronApp: [
138+
async ({ launchElectronApp }, use, testInfo) => {
139+
const apps: Record<string, ElectronApplication> = {};
140+
await use(async ({ initUserDataPath } = {}) => {
141+
const key = initUserDataPath;
142+
if (key && apps[key]) {
143+
return apps[key];
144+
}
145+
const app = await launchElectronApp({ initUserDataPath });
146+
apps[key] = app;
147+
return app;
148+
});
149+
},
150+
{ scope: 'worker' }
151+
],
152+
153+
pageWithUserData: async ({ reuseOrLaunchElectronApp }, use, testInfo) => {
154+
const testDir = path.dirname(testInfo.file);
155+
const initUserDataPath = path.join(testDir, 'init-user-data');
156+
157+
const app = await reuseOrLaunchElectronApp(
158+
(await fs.promises.stat(initUserDataPath).catch(() => false)) ? { initUserDataPath } : {}
159+
);
160+
161+
const context = await app.context();
162+
const page = await app.firstWindow();
163+
const tracingOptions = (testInfo as any)._tracing.traceOptions();
164+
if (tracingOptions) {
165+
const tracePath = testInfo.outputPath(`trace-${testInfo.testId}.zip`);
166+
try {
167+
await context.tracing.start({ screenshots: true, snapshots: true, sources: true });
168+
} catch (e) {}
169+
await context.tracing.startChunk();
170+
await use(page);
171+
await context.tracing.stopChunk({ path: tracePath });
172+
await testInfo.attach('trace', { path: tracePath });
173+
} else {
174+
await use(page);
175+
}
20176
}
21177
});
22178

23-
export * from '@playwright/test'
179+
export * from '@playwright/test';

0 commit comments

Comments
 (0)