Skip to content

Commit 4a26460

Browse files
committed
test: add e2e test with playwright
1 parent a35106f commit 4a26460

File tree

5 files changed

+131
-3
lines changed

5 files changed

+131
-3
lines changed

.github/workflows/playwright.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: E2E Tests
2+
on:
3+
push:
4+
branches: [ master, dev-zx ]
5+
pull_request:
6+
branches: [ master ]
7+
jobs:
8+
test:
9+
timeout-minutes: 10
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: 20
16+
- name: Install dependencies
17+
run: npm i
18+
- name: Install Playwright Browser
19+
run: npx playwright install --with-deps chromium
20+
- name: Run Playwright tests
21+
run: npm run test:e2e

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ coverage
55
file.txt
66
*.gz
77
*.sw*
8-
*.un~
8+
*.un~
9+
10+
# Playwright
11+
/test-results/
12+
/playwright-report/
13+
/blob-report/
14+
/playwright/.cache/

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
"lodash": "4"
2424
},
2525
"devDependencies": {
26+
"@playwright/test": "^1.52.0",
27+
"@types/node": "^22.15.17",
2628
"electron-json-storage-alt": "18",
2729
"eslint": "7",
28-
"eslint-config-egg": "^5.1.1",
29-
"eslint-plugin-mocha": "^4.11.0",
30+
"eslint-config-egg": "12",
31+
"eslint-plugin-mocha": "~10.0.0",
3032
"git-contributor": "*",
3133
"husky": "*",
3234
"mocha": "*",
@@ -35,6 +37,7 @@
3537
"scripts": {
3638
"dev": "electron ./start.js",
3739
"test": "nyc --reporter=lcov --reporter=text mocha",
40+
"test:e2e": "npx playwright test",
3841
"lint": "eslint . --fix",
3942
"contributor": "git-contributor"
4043
},

playwright.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { defineConfig, devices } from '@playwright/test';
2+
3+
const isCI = !!process.env.CI;
4+
5+
export default defineConfig({
6+
testDir: './test/e2e',
7+
fullyParallel: false,
8+
forbidOnly: isCI,
9+
retries: isCI ? 2 : 0,
10+
workers: 1,
11+
reporter: 'null',
12+
use: {
13+
trace: 'on-first-retry',
14+
},
15+
projects: [
16+
{
17+
name: 'chromium',
18+
use: { ...devices['Desktop Chrome'] },
19+
},
20+
],
21+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// @ts-check
2+
import { test, expect, _electron as electron } from '@playwright/test';
3+
import { last as _last } from 'lodash';
4+
import * as path from 'path';
5+
6+
const wait = (ms = 2000) => new Promise((resolve) => setTimeout(resolve, ms));
7+
8+
/**
9+
* Gets the last element of array.
10+
* @template T
11+
* @param {T[] | null | undefined} arr
12+
* @returns {T}
13+
*/
14+
const last = (arr) => {
15+
const lastElement = _last(arr);
16+
expect(lastElement).not.toBeUndefined();
17+
// @ts-ignore
18+
return lastElement;
19+
};
20+
21+
test.describe('e2e', () => {
22+
/** @type { import('@playwright/test').ElectronApplication } */
23+
let electronApp;
24+
25+
test.beforeEach(async () => {
26+
electronApp = await electron.launch({ args: ['start.js'], cwd: path.join(__dirname, '../..') });
27+
});
28+
29+
test('loadingView option is working', async () => {
30+
const window = await electronApp.firstWindow();
31+
32+
expect(await window.title()).toBe('Loading');
33+
expect(window.url()).toMatch(/renderer\/loading\.html$/);
34+
});
35+
36+
test('new window can be correctly created', async () => {
37+
await wait();
38+
const window = await electronApp.firstWindow();
39+
await window.click('button#new');
40+
await wait();
41+
const windows = electronApp.windows();
42+
43+
expect(windows.length).toBeGreaterThan(1);
44+
expect(last(windows).url()).toMatch(/renderer\/window\.html$/);
45+
});
46+
47+
test('new online window can be correctly created', async () => {
48+
await wait();
49+
const window = await electronApp.firstWindow();
50+
await window.click('button#new-online');
51+
await wait();
52+
const windows = electronApp.windows();
53+
54+
expect(windows.length).toBeGreaterThan(1);
55+
expect(last(windows).url()).toMatch(/https(.+)?github\.com/);
56+
});
57+
58+
test('window can be correctly closed', async () => {
59+
await wait();
60+
const window = await electronApp.firstWindow();
61+
await window.click('button#new');
62+
await wait();
63+
const newWindow = last(electronApp.windows());
64+
await newWindow.click('button#close');
65+
await wait();
66+
const windows = electronApp.windows();
67+
68+
expect(windows).toHaveLength(1);
69+
expect(await windows[0].title()).toBe('main');
70+
});
71+
72+
test.afterEach(async () => {
73+
await electronApp.close();
74+
// @ts-ignore
75+
electronApp = null;
76+
});
77+
});

0 commit comments

Comments
 (0)