@witchcraft/nuxt-electron 0.4.5 → 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.json +1 -1
- package/dist/runtime/electron/test/index.d.ts +0 -1
- package/dist/runtime/electron/test/index.js +0 -1
- package/dist/runtime/electron/test/launchElectron.d.ts +40 -6
- package/dist/runtime/electron/test/launchElectron.js +64 -33
- package/dist/runtime/electron/test/localhostUrl.d.ts +6 -1
- package/dist/runtime/electron/test/localhostUrl.js +4 -2
- package/dist/runtime/electron/test/waitUntilAtPath.d.ts +4 -1
- package/dist/runtime/electron/test/waitUntilAtPath.js +8 -2
- package/package.json +1 -1
- package/src/runtime/electron/test/index.ts +0 -1
- package/src/runtime/electron/test/launchElectron.ts +106 -66
- package/src/runtime/electron/test/localhostUrl.ts +10 -6
- package/src/runtime/electron/test/waitUntilAtPath.ts +11 -5
- package/dist/runtime/electron/test/launchApp.d.ts +0 -27
- package/dist/runtime/electron/test/launchApp.js +0 -59
- package/src/runtime/electron/test/launchApp.ts +0 -94
package/dist/module.json
CHANGED
|
@@ -1,20 +1,54 @@
|
|
|
1
1
|
import { type ElectronApplication, type Page } from "@playwright/test";
|
|
2
2
|
import type { Cookie } from "electron";
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Creates a Playwright test object with an Electron app and its window wired in. The app launches on first use and closes when the test ends.
|
|
5
|
+
*
|
|
6
|
+
* Without arguments the page is the window the app opens at launch. Pass another prefix to waitForWindow after an action that opens a different window.
|
|
7
|
+
*/
|
|
8
|
+
export declare function launchElectron({ appDir, userDataDir, serverUrl, useDevMode, bundlePrefix, env, extraArgs, windowTimeout }: {
|
|
9
|
+
/** Points the app at serverUrl with VITE_DEV_SERVER_URL instead of at its bundlePrefix. Only works when the app leaves the windowUrl override open, so a baked production flag must be false. */
|
|
10
|
+
useDevMode?: boolean | string;
|
|
11
|
+
/** Directory the app is launched from, becomes the electron process cwd. A module playground passes its playground dir, an app passes its repo root. */
|
|
4
12
|
appDir: string;
|
|
13
|
+
/** Profile directory, it will be wiped before every launch. Point it somewhere gitignored. Must be within appDir for safety. */
|
|
5
14
|
userDataDir: string;
|
|
15
|
+
/** Server the app talks to when proxying. Is used to launch electron with OVERRIDE_PUBLIC_SERVER_URL and VITE_DEV_SERVER_URL as needed. */
|
|
6
16
|
serverUrl: string;
|
|
7
|
-
|
|
8
|
-
|
|
17
|
+
/** How long to wait for the matching window before throwing. */
|
|
18
|
+
windowTimeout?: number;
|
|
19
|
+
/** The app's bundle url prefix, e.g. "app://bundle". Only needed in bundled mode, dev uses serverUrl. */
|
|
20
|
+
bundlePrefix?: string;
|
|
21
|
+
/** Extra env to run the app with. */
|
|
9
22
|
env?: Record<string, string>;
|
|
23
|
+
/** Extra args for the electron process. */
|
|
10
24
|
extraArgs?: string[];
|
|
11
|
-
windowTimeout?: number;
|
|
12
25
|
}): import("@playwright/test").TestType<import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions & {
|
|
13
26
|
app: ElectronApplication;
|
|
14
27
|
page: Page;
|
|
15
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Waits for a window whose url starts with the prefix and returns it.
|
|
30
|
+
*
|
|
31
|
+
* Without arguments it waits for the launch prefix, which is the app bundle normally and the server url in dev. Pass another prefix after an action that opens a different window, such as switching user. Already open windows are checked first, then new arrivals, and navigation is awaited since the url is not set yet when the window event fires. Throws listing the urls it saw on timeout.
|
|
32
|
+
*/
|
|
33
|
+
waitForWindow: (prefix?: string, timeout?: number) => Promise<Page>;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the next window the app opens, for example after switching user.
|
|
36
|
+
*
|
|
37
|
+
* Unlike waitForWindow it does not match by url, it takes whatever non-first window arrives next. An event that fired before the caller asks is not missed, and it waits until only one window is listed since the app closes the old window asynchronously.
|
|
38
|
+
*/
|
|
16
39
|
getPageFromWindow: () => Promise<Page>;
|
|
17
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Cancels the given window's session requests (http/https only by default), so proxied calls fail instead of being answered to simulate the server being unavailable. You can also pass a custom list of urls (which can contain globs, see win.webContents.session.webRequest.onBeforeRequest).
|
|
42
|
+
*/
|
|
43
|
+
blockNetwork: (page: Page, urls?: string[]) => Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Lets all the given window's requests through again, after blockNetwork.
|
|
46
|
+
*/
|
|
18
47
|
unblockNetwork: (page: Page) => Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Reads the cookies of the given window's session, all of them or the ones named.
|
|
50
|
+
*
|
|
51
|
+
* The window is matched by its position among the app's windows, which share creation order on both sides.
|
|
52
|
+
*/
|
|
19
53
|
getWindowCookies: (page: Page, name?: string) => Promise<Cookie[]>;
|
|
20
54
|
}, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions>;
|
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
import { expect, test as base } from "@playwright/test";
|
|
2
|
-
import {
|
|
1
|
+
import { _electron as electron, expect, test as base } from "@playwright/test";
|
|
2
|
+
import { rm } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
3
4
|
export function launchElectron({
|
|
4
5
|
appDir,
|
|
5
6
|
userDataDir,
|
|
6
7
|
serverUrl,
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
useDevMode = false,
|
|
9
|
+
bundlePrefix,
|
|
9
10
|
env = {},
|
|
10
11
|
extraArgs = [],
|
|
11
12
|
windowTimeout = 3e4
|
|
12
13
|
}) {
|
|
14
|
+
if (!useDevMode && !bundlePrefix) {
|
|
15
|
+
throw new Error("launchElectron: pass bundlePrefix, the app's bundle url prefix, or run in dev with useDevMode.");
|
|
16
|
+
}
|
|
17
|
+
const launchPrefix = useDevMode ? serverUrl : bundlePrefix;
|
|
13
18
|
return base.extend({
|
|
14
19
|
browser: async ({ browser }, use) => {
|
|
15
20
|
const marked = browser;
|
|
@@ -28,30 +33,49 @@ export function launchElectron({
|
|
|
28
33
|
},
|
|
29
34
|
// eslint-disable-next-line no-empty-pattern
|
|
30
35
|
app: async ({}, use) => {
|
|
31
|
-
const
|
|
36
|
+
const relative = path.relative(path.resolve(appDir), path.resolve(userDataDir));
|
|
37
|
+
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
38
|
+
throw new Error(`userDataDir must be inside appDir, got ${userDataDir}.`);
|
|
39
|
+
}
|
|
40
|
+
await rm(userDataDir, { recursive: true, force: true });
|
|
41
|
+
const app = await electron.launch({
|
|
42
|
+
// launched as an app directory so `app.getAppPath()` is the app root, which is where
|
|
43
|
+
// @witchcraft/nuxt-logger resolves pino's workers from
|
|
44
|
+
args: [".", "--no-sandbox", "--ignore-certificate-errors", `--user-data-dir=${userDataDir}`, ...extraArgs],
|
|
45
|
+
cwd: appDir,
|
|
46
|
+
env: {
|
|
47
|
+
...process.env,
|
|
48
|
+
...env,
|
|
49
|
+
DISABLE_AUTO_UPDATES: "true",
|
|
50
|
+
OVERRIDE_PUBLIC_SERVER_URL: serverUrl,
|
|
51
|
+
...useDevMode ? { VITE_DEV_SERVER_URL: serverUrl } : {}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
32
54
|
await use(app);
|
|
33
|
-
await close();
|
|
55
|
+
await app.close();
|
|
34
56
|
},
|
|
35
57
|
page: async ({ waitForWindow }, use) => {
|
|
36
58
|
await use(await waitForWindow());
|
|
37
59
|
},
|
|
38
60
|
waitForWindow: async ({ app }, use) => {
|
|
39
|
-
|
|
61
|
+
const isAppWindow = (candidate) => !candidate.url().startsWith("devtools://");
|
|
62
|
+
await use(async (prefix = launchPrefix, timeout = windowTimeout) => {
|
|
40
63
|
const deadline = Date.now() + timeout;
|
|
41
64
|
const watched = /* @__PURE__ */ new Map();
|
|
42
|
-
const watch = (candidate) => {
|
|
43
|
-
let pending = watched.get(candidate);
|
|
44
|
-
if (!pending) {
|
|
45
|
-
pending = candidate.waitForURL((url) => url.toString().startsWith(prefix), { timeout: Math.max(deadline - Date.now(), 1) }).then(() => candidate).catch(() => void 0);
|
|
46
|
-
watched.set(candidate, pending);
|
|
47
|
-
}
|
|
48
|
-
return pending;
|
|
49
|
-
};
|
|
50
65
|
let page;
|
|
51
66
|
while (!page && Date.now() < deadline) {
|
|
52
67
|
const next = await Promise.race([
|
|
53
|
-
|
|
54
|
-
app.
|
|
68
|
+
// watches windows already open until their url matches, the url is not set yet when a window appears
|
|
69
|
+
...app.windows().filter(isAppWindow).map((candidate) => {
|
|
70
|
+
let pending = watched.get(candidate);
|
|
71
|
+
if (!pending) {
|
|
72
|
+
pending = candidate.waitForURL((url) => url.toString().startsWith(prefix), { timeout: Math.max(deadline - Date.now(), 1) }).then(() => candidate).catch(() => void 0);
|
|
73
|
+
watched.set(candidate, pending);
|
|
74
|
+
}
|
|
75
|
+
return pending;
|
|
76
|
+
}),
|
|
77
|
+
// notices each newly opening window, matched or not
|
|
78
|
+
app.waitForEvent("window", { timeout: Math.max(deadline - Date.now(), 1) }).then((window2) => isAppWindow(window2) ? window2 : void 0).catch(() => void 0)
|
|
55
79
|
]);
|
|
56
80
|
if (next && next.url().startsWith(prefix)) page = next;
|
|
57
81
|
}
|
|
@@ -64,12 +88,14 @@ export function launchElectron({
|
|
|
64
88
|
},
|
|
65
89
|
getPageFromWindow: async ({ app }, use) => {
|
|
66
90
|
const isAppWindow = (candidate) => !candidate.url().startsWith("devtools://");
|
|
91
|
+
const deadline = Date.now() + windowTimeout;
|
|
67
92
|
let first = app.windows().find(isAppWindow);
|
|
68
|
-
while (!first) {
|
|
69
|
-
const candidate = await app.waitForEvent("window");
|
|
70
|
-
if (isAppWindow(candidate)) first = candidate;
|
|
93
|
+
while (!first && Date.now() < deadline) {
|
|
94
|
+
const candidate = await app.waitForEvent("window", { timeout: Math.max(deadline - Date.now(), 1) }).catch(() => void 0);
|
|
95
|
+
if (candidate && isAppWindow(candidate)) first = candidate;
|
|
71
96
|
}
|
|
72
|
-
|
|
97
|
+
if (!first) throw new Error("getPageFromWindow: no app window opened.");
|
|
98
|
+
const pending = app.windows().filter((candidate) => candidate !== first && isAppWindow(candidate));
|
|
73
99
|
const waiting = [];
|
|
74
100
|
app.on("window", (window2) => {
|
|
75
101
|
if (window2 === first || !isAppWindow(window2)) return;
|
|
@@ -79,28 +105,33 @@ export function launchElectron({
|
|
|
79
105
|
await use(async () => {
|
|
80
106
|
if (pending.length === 0) await new Promise((resolve) => waiting.push(resolve));
|
|
81
107
|
const next = pending.shift();
|
|
82
|
-
await next.waitForLoadState();
|
|
83
|
-
await expect.poll(() => app.evaluate((
|
|
108
|
+
await next.waitForLoadState("load", { timeout: windowTimeout });
|
|
109
|
+
await expect.poll(() => app.evaluate((electron2) => electron2.BrowserWindow.getAllWindows().length), { timeout: windowTimeout }).toBe(1);
|
|
84
110
|
return next;
|
|
85
111
|
});
|
|
86
112
|
},
|
|
87
113
|
blockNetwork: async ({ app }, use) => {
|
|
88
|
-
await use(async (page) => {
|
|
89
|
-
const index = app.windows().filter((candidate) =>
|
|
114
|
+
await use(async (page, urls = ["http://*/*", "https://*/*"]) => {
|
|
115
|
+
const index = app.windows().filter((candidate) => {
|
|
116
|
+
return !candidate.url().startsWith("devtools://");
|
|
117
|
+
}).indexOf(page);
|
|
90
118
|
if (index === -1) throw new Error("blockNetwork: the page does not belong to this app.");
|
|
91
|
-
await app.evaluate((
|
|
92
|
-
const win =
|
|
119
|
+
await app.evaluate((electron2, { windowIndex, patterns }) => {
|
|
120
|
+
const win = electron2.BrowserWindow.getAllWindows()[windowIndex];
|
|
93
121
|
if (!win) throw new Error("blockNetwork: no window at that index.");
|
|
94
|
-
win.webContents.session.webRequest.onBeforeRequest(
|
|
95
|
-
|
|
122
|
+
win.webContents.session.webRequest.onBeforeRequest(
|
|
123
|
+
{ urls: patterns },
|
|
124
|
+
(_details, callback) => callback({ cancel: true })
|
|
125
|
+
);
|
|
126
|
+
}, { windowIndex: index, patterns: urls });
|
|
96
127
|
});
|
|
97
128
|
},
|
|
98
129
|
unblockNetwork: async ({ app }, use) => {
|
|
99
130
|
await use(async (page) => {
|
|
100
131
|
const index = app.windows().filter((candidate) => !candidate.url().startsWith("devtools://")).indexOf(page);
|
|
101
132
|
if (index === -1) throw new Error("unblockNetwork: the page does not belong to this app.");
|
|
102
|
-
await app.evaluate((
|
|
103
|
-
const win =
|
|
133
|
+
await app.evaluate((electron2, windowIndex) => {
|
|
134
|
+
const win = electron2.BrowserWindow.getAllWindows()[windowIndex];
|
|
104
135
|
if (!win) throw new Error("unblockNetwork: no window at that index.");
|
|
105
136
|
win.webContents.session.webRequest.onBeforeRequest(null);
|
|
106
137
|
}, index);
|
|
@@ -110,8 +141,8 @@ export function launchElectron({
|
|
|
110
141
|
await use(async (page, name) => {
|
|
111
142
|
const index = app.windows().filter((candidate) => !candidate.url().startsWith("devtools://")).indexOf(page);
|
|
112
143
|
if (index === -1) throw new Error("getWindowCookies: the page does not belong to this app.");
|
|
113
|
-
return app.evaluate((
|
|
114
|
-
const win =
|
|
144
|
+
return app.evaluate((electron2, { windowIndex, cookieName }) => {
|
|
145
|
+
const win = electron2.BrowserWindow.getAllWindows()[windowIndex];
|
|
115
146
|
if (!win) throw new Error("getWindowCookies: no window at that index.");
|
|
116
147
|
return win.webContents.session.cookies.get(cookieName ? { name: cookieName } : {});
|
|
117
148
|
}, { windowIndex: index, cookieName: name });
|
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The url the local dev server is reached on. https needs both LOCALHOST_KEY_PEM and LOCALHOST_PEM set, otherwise http.
|
|
3
|
+
*
|
|
4
|
+
* Uses NUXT_PORT by default, override with an port from an env or a number. If the port is undefined, it will throw.
|
|
5
|
+
*/
|
|
6
|
+
export declare function localhostUrl(port?: string | number): string;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export function localhostUrl(port
|
|
1
|
+
export function localhostUrl(port) {
|
|
2
|
+
const resolved = port ?? process.env.NUXT_PORT;
|
|
3
|
+
if (resolved === void 0) throw new Error("localhostUrl: pass a port or set NUXT_PORT.");
|
|
2
4
|
const certs = process.env.LOCALHOST_KEY_PEM && process.env.LOCALHOST_PEM;
|
|
3
|
-
return `${certs ? "https" : "http"}://localhost:${
|
|
5
|
+
return `${certs ? "https" : "http"}://localhost:${resolved}`;
|
|
4
6
|
}
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import type { Page } from "@playwright/test";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Waits until the page is on the route path, whatever the scheme and host are. The same route is app://bundle/authed in the bundle and http://localhost/authed in dev, so matching the pathname is what keeps one assertion working in both.
|
|
4
|
+
*/
|
|
5
|
+
export declare function waitUntilAtPath(page: Page, routePath: string, timeout?: number): Promise<void>;
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
export async function waitUntilAtPath(page,
|
|
2
|
-
await page.waitForURL((url) =>
|
|
1
|
+
export async function waitUntilAtPath(page, routePath, timeout) {
|
|
2
|
+
await page.waitForURL((url) => {
|
|
3
|
+
try {
|
|
4
|
+
return new URL(url).pathname === routePath;
|
|
5
|
+
} catch {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
}, { timeout });
|
|
3
9
|
}
|
package/package.json
CHANGED
|
@@ -1,66 +1,74 @@
|
|
|
1
|
-
import { type ElectronApplication, expect, type Page, test as base } from "@playwright/test"
|
|
2
|
-
// playwright's window list includes devtools, getAllWindows does not, so devtools is filtered out of every window list the fixture reads
|
|
1
|
+
import { _electron as electron, type ElectronApplication, expect, type Page, test as base } from "@playwright/test"
|
|
3
2
|
import type { Cookie } from "electron"
|
|
3
|
+
import { rm } from "node:fs/promises"
|
|
4
|
+
import path from "node:path"
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
// playwright's window list includes devtools, getAllWindows does not, so devtools is filtered out of every window list the fixture reads
|
|
6
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Creates a Playwright test object with an Electron app and its window wired in. The app launches on first use and closes when the test ends.
|
|
10
|
+
*
|
|
11
|
+
* Without arguments the page is the window the app opens at launch. Pass another prefix to waitForWindow after an action that opens a different window.
|
|
12
|
+
*/
|
|
7
13
|
export function launchElectron({
|
|
8
14
|
appDir,
|
|
9
15
|
userDataDir,
|
|
10
16
|
serverUrl,
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
useDevMode = false,
|
|
18
|
+
bundlePrefix,
|
|
13
19
|
env = {},
|
|
14
20
|
extraArgs = [],
|
|
15
21
|
windowTimeout = 30_000
|
|
16
22
|
}: {
|
|
17
|
-
|
|
23
|
+
/** Points the app at serverUrl with VITE_DEV_SERVER_URL instead of at its bundlePrefix. Only works when the app leaves the windowUrl override open, so a baked production flag must be false. */
|
|
24
|
+
useDevMode?: boolean | string
|
|
25
|
+
/** Directory the app is launched from, becomes the electron process cwd. A module playground passes its playground dir, an app passes its repo root. */
|
|
18
26
|
appDir: string
|
|
19
|
-
|
|
27
|
+
/** Profile directory, it will be wiped before every launch. Point it somewhere gitignored. Must be within appDir for safety. */
|
|
20
28
|
userDataDir: string
|
|
21
|
-
|
|
29
|
+
/** Server the app talks to when proxying. Is used to launch electron with OVERRIDE_PUBLIC_SERVER_URL and VITE_DEV_SERVER_URL as needed. */
|
|
22
30
|
serverUrl: string
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
/** How long to wait for the matching window before throwing. */
|
|
32
|
+
windowTimeout?: number
|
|
33
|
+
/** The app's bundle url prefix, e.g. "app://bundle". Only needed in bundled mode, dev uses serverUrl. */
|
|
34
|
+
bundlePrefix?: string
|
|
35
|
+
/** Extra env to run the app with. */
|
|
28
36
|
env?: Record<string, string>
|
|
29
|
-
|
|
37
|
+
/** Extra args for the electron process. */
|
|
30
38
|
extraArgs?: string[]
|
|
31
|
-
/* How long to wait for the matching window before throwing. */
|
|
32
|
-
windowTimeout?: number
|
|
33
39
|
}) {
|
|
40
|
+
if (!useDevMode && !bundlePrefix) {
|
|
41
|
+
throw new Error("launchElectron: pass bundlePrefix, the app's bundle url prefix, or run in dev with useDevMode.")
|
|
42
|
+
}
|
|
43
|
+
const launchPrefix = useDevMode ? serverUrl : bundlePrefix as string
|
|
34
44
|
return base.extend<{
|
|
35
45
|
app: ElectronApplication
|
|
36
46
|
page: Page
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
waitForWindow: (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Waits for a window whose url starts with the prefix and returns it.
|
|
49
|
+
*
|
|
50
|
+
* Without arguments it waits for the launch prefix, which is the app bundle normally and the server url in dev. Pass another prefix after an action that opens a different window, such as switching user. Already open windows are checked first, then new arrivals, and navigation is awaited since the url is not set yet when the window event fires. Throws listing the urls it saw on timeout.
|
|
51
|
+
*/
|
|
52
|
+
waitForWindow: (prefix?: string, timeout?: number) => Promise<Page>
|
|
53
|
+
/**
|
|
54
|
+
* Returns the next window the app opens, for example after switching user.
|
|
55
|
+
*
|
|
56
|
+
* Unlike waitForWindow it does not match by url, it takes whatever non-first window arrives next. An event that fired before the caller asks is not missed, and it waits until only one window is listed since the app closes the old window asynchronously.
|
|
57
|
+
*/
|
|
48
58
|
getPageFromWindow: () => Promise<Page>
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
Lets the given window's http requests through again, after blockNetwork.
|
|
57
|
-
*/
|
|
59
|
+
/**
|
|
60
|
+
* Cancels the given window's session requests (http/https only by default), so proxied calls fail instead of being answered to simulate the server being unavailable. You can also pass a custom list of urls (which can contain globs, see win.webContents.session.webRequest.onBeforeRequest).
|
|
61
|
+
*/
|
|
62
|
+
blockNetwork: (page: Page, urls?: string[]) => Promise<void>
|
|
63
|
+
/**
|
|
64
|
+
* Lets all the given window's requests through again, after blockNetwork.
|
|
65
|
+
*/
|
|
58
66
|
unblockNetwork: (page: Page) => Promise<void>
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Reads the cookies of the given window's session, all of them or the ones named.
|
|
69
|
+
*
|
|
70
|
+
* The window is matched by its position among the app's windows, which share creation order on both sides.
|
|
71
|
+
*/
|
|
64
72
|
getWindowCookies: (page: Page, name?: string) => Promise<Cookie[]>
|
|
65
73
|
}>({
|
|
66
74
|
browser: async ({ browser }, use) => {
|
|
@@ -79,32 +87,53 @@ export function launchElectron({
|
|
|
79
87
|
},
|
|
80
88
|
// eslint-disable-next-line no-empty-pattern
|
|
81
89
|
app: async ({}, use) => {
|
|
82
|
-
const
|
|
90
|
+
const relative = path.relative(path.resolve(appDir), path.resolve(userDataDir))
|
|
91
|
+
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
92
|
+
throw new Error(`userDataDir must be inside appDir, got ${userDataDir}.`)
|
|
93
|
+
}
|
|
94
|
+
await rm(userDataDir, { recursive: true, force: true })
|
|
95
|
+
|
|
96
|
+
const app = await electron.launch({
|
|
97
|
+
// launched as an app directory so `app.getAppPath()` is the app root, which is where
|
|
98
|
+
// @witchcraft/nuxt-logger resolves pino's workers from
|
|
99
|
+
args: [".", "--no-sandbox", "--ignore-certificate-errors", `--user-data-dir=${userDataDir}`, ...extraArgs],
|
|
100
|
+
cwd: appDir,
|
|
101
|
+
env: {
|
|
102
|
+
...process.env,
|
|
103
|
+
...env,
|
|
104
|
+
DISABLE_AUTO_UPDATES: "true",
|
|
105
|
+
OVERRIDE_PUBLIC_SERVER_URL: serverUrl,
|
|
106
|
+
...(useDevMode ? { VITE_DEV_SERVER_URL: serverUrl } : {})
|
|
107
|
+
} as Record<string, string>
|
|
108
|
+
})
|
|
83
109
|
await use(app)
|
|
84
|
-
await close()
|
|
110
|
+
await app.close()
|
|
85
111
|
},
|
|
86
112
|
page: async ({ waitForWindow }, use) => {
|
|
87
113
|
await use(await waitForWindow())
|
|
88
114
|
},
|
|
115
|
+
|
|
89
116
|
waitForWindow: async ({ app }, use) => {
|
|
90
|
-
|
|
91
|
-
await use(async (prefix =
|
|
117
|
+
const isAppWindow = (candidate: Page) => !candidate.url().startsWith("devtools://")
|
|
118
|
+
await use(async (prefix = launchPrefix, timeout = windowTimeout): Promise<Page> => {
|
|
92
119
|
const deadline = Date.now() + timeout
|
|
93
120
|
const watched = new Map<Page, Promise<Page | undefined>>()
|
|
94
|
-
const watch = (candidate: Page) => {
|
|
95
|
-
let pending = watched.get(candidate)
|
|
96
|
-
if (!pending) {
|
|
97
|
-
pending = candidate.waitForURL(url => url.toString().startsWith(prefix), { timeout: Math.max(deadline - Date.now(), 1) }).then(() => candidate).catch(() => undefined)
|
|
98
|
-
watched.set(candidate, pending)
|
|
99
|
-
}
|
|
100
|
-
return pending
|
|
101
|
-
}
|
|
102
121
|
let page: Page | undefined
|
|
103
122
|
while (!page && Date.now() < deadline) {
|
|
104
123
|
const next = await Promise.race([
|
|
105
|
-
|
|
106
|
-
app.
|
|
124
|
+
// watches windows already open until their url matches, the url is not set yet when a window appears
|
|
125
|
+
...app.windows().filter(isAppWindow).map((candidate: Page): Promise<Page | undefined> => {
|
|
126
|
+
let pending: Promise<Page | undefined> | undefined = watched.get(candidate)
|
|
127
|
+
if (!pending) {
|
|
128
|
+
pending = candidate.waitForURL(url => url.toString().startsWith(prefix), { timeout: Math.max(deadline - Date.now(), 1) }).then(() => candidate).catch(() => undefined)
|
|
129
|
+
watched.set(candidate, pending)
|
|
130
|
+
}
|
|
131
|
+
return pending
|
|
132
|
+
}),
|
|
133
|
+
// notices each newly opening window, matched or not
|
|
134
|
+
app.waitForEvent("window", { timeout: Math.max(deadline - Date.now(), 1) }).then(window => isAppWindow(window) ? window : undefined).catch(() => undefined)
|
|
107
135
|
])
|
|
136
|
+
// keeps only winners whose url matches
|
|
108
137
|
if (next && next.url().startsWith(prefix)) page = next
|
|
109
138
|
}
|
|
110
139
|
if (!page) {
|
|
@@ -114,15 +143,18 @@ export function launchElectron({
|
|
|
114
143
|
return page
|
|
115
144
|
})
|
|
116
145
|
},
|
|
146
|
+
|
|
117
147
|
getPageFromWindow: async ({ app }, use) => {
|
|
118
148
|
const isAppWindow = (candidate: Page) => !candidate.url().startsWith("devtools://")
|
|
119
149
|
// the first window is not a reopen, and its event has already fired by the time this runs
|
|
150
|
+
const deadline = Date.now() + windowTimeout
|
|
120
151
|
let first = app.windows().find(isAppWindow)
|
|
121
|
-
while (!first) {
|
|
122
|
-
const candidate = await app.waitForEvent("window")
|
|
123
|
-
if (isAppWindow(candidate)) first = candidate
|
|
152
|
+
while (!first && Date.now() < deadline) {
|
|
153
|
+
const candidate = await app.waitForEvent("window", { timeout: Math.max(deadline - Date.now(), 1) }).catch(() => undefined)
|
|
154
|
+
if (candidate && isAppWindow(candidate)) first = candidate
|
|
124
155
|
}
|
|
125
|
-
|
|
156
|
+
if (!first) throw new Error("getPageFromWindow: no app window opened.")
|
|
157
|
+
const pending: Page[] = app.windows().filter(candidate => candidate !== first && isAppWindow(candidate))
|
|
126
158
|
const waiting: (() => void)[] = []
|
|
127
159
|
app.on("window", window => {
|
|
128
160
|
if (window === first || !isAppWindow(window)) return
|
|
@@ -133,21 +165,29 @@ export function launchElectron({
|
|
|
133
165
|
// the event can have fired before the caller asks, so the queue is the only source, never waitForEvent
|
|
134
166
|
if (pending.length === 0) await new Promise<void>(resolve => waiting.push(resolve))
|
|
135
167
|
const next = pending.shift()!
|
|
136
|
-
await next.waitForLoadState()
|
|
168
|
+
await next.waitForLoadState("load", { timeout: windowTimeout })
|
|
137
169
|
// the app closes the old window asynchronously, so it can still be listed while the new one loads
|
|
138
|
-
await expect.poll(() => app.evaluate(electron => electron.BrowserWindow.getAllWindows().length)).toBe(1)
|
|
170
|
+
await expect.poll(() => app.evaluate(electron => electron.BrowserWindow.getAllWindows().length), { timeout: windowTimeout }).toBe(1)
|
|
139
171
|
return next
|
|
140
172
|
})
|
|
141
173
|
},
|
|
142
174
|
blockNetwork: async ({ app }, use) => {
|
|
143
|
-
await use(async (page: Page) => {
|
|
144
|
-
const index = app.windows()
|
|
175
|
+
await use(async (page: Page, urls: string[] = ["http://*/*", "https://*/*"]) => {
|
|
176
|
+
const index = app.windows()
|
|
177
|
+
.filter(candidate => {
|
|
178
|
+
return !candidate.url().startsWith("devtools://")
|
|
179
|
+
})
|
|
180
|
+
.indexOf(page)
|
|
181
|
+
|
|
145
182
|
if (index === -1) throw new Error("blockNetwork: the page does not belong to this app.")
|
|
146
|
-
await app.evaluate((electron, windowIndex) => {
|
|
183
|
+
await app.evaluate((electron, { windowIndex, patterns }) => {
|
|
147
184
|
const win = electron.BrowserWindow.getAllWindows()[windowIndex]
|
|
148
185
|
if (!win) throw new Error("blockNetwork: no window at that index.")
|
|
149
|
-
win.webContents.session.webRequest.onBeforeRequest(
|
|
150
|
-
|
|
186
|
+
win.webContents.session.webRequest.onBeforeRequest(
|
|
187
|
+
{ urls: patterns },
|
|
188
|
+
(_details, callback) => callback({ cancel: true })
|
|
189
|
+
)
|
|
190
|
+
}, { windowIndex: index, patterns: urls })
|
|
151
191
|
})
|
|
152
192
|
},
|
|
153
193
|
unblockNetwork: async ({ app }, use) => {
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
The url the local dev server is reached on. https when the devenv shell exported the local certificate.
|
|
1
|
+
import type { Page } from "@playwright/test"
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/**
|
|
4
|
+
* The url the local dev server is reached on. https needs both LOCALHOST_KEY_PEM and LOCALHOST_PEM set, otherwise http.
|
|
5
|
+
*
|
|
6
|
+
* Uses NUXT_PORT by default, override with an port from an env or a number. If the port is undefined, it will throw.
|
|
7
|
+
*/
|
|
8
|
+
export function localhostUrl(port?: string | number): string {
|
|
9
|
+
const resolved = port ?? process.env.NUXT_PORT
|
|
10
|
+
if (resolved === undefined) throw new Error("localhostUrl: pass a port or set NUXT_PORT.")
|
|
7
11
|
const certs = process.env.LOCALHOST_KEY_PEM && process.env.LOCALHOST_PEM
|
|
8
|
-
return `${certs ? "https" : "http"}://localhost:${
|
|
12
|
+
return `${certs ? "https" : "http"}://localhost:${resolved}`
|
|
9
13
|
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { Page } from "@playwright/test"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
export async function waitUntilAtPath(page: Page,
|
|
7
|
-
await page.waitForURL(url =>
|
|
3
|
+
/**
|
|
4
|
+
* Waits until the page is on the route path, whatever the scheme and host are. The same route is app://bundle/authed in the bundle and http://localhost/authed in dev, so matching the pathname is what keeps one assertion working in both.
|
|
5
|
+
*/
|
|
6
|
+
export async function waitUntilAtPath(page: Page, routePath: string, timeout?: number): Promise<void> {
|
|
7
|
+
await page.waitForURL(url => {
|
|
8
|
+
try {
|
|
9
|
+
return new URL(url).pathname === routePath
|
|
10
|
+
} catch {
|
|
11
|
+
return false
|
|
12
|
+
}
|
|
13
|
+
}, { timeout })
|
|
8
14
|
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { type ElectronApplication, type Page } from "@playwright/test";
|
|
2
|
-
/**
|
|
3
|
-
* Launches the built app for a test, starting from a clean profile every time.
|
|
4
|
-
*
|
|
5
|
-
* ```ts
|
|
6
|
-
* const { app, page, close } = await launchApp({
|
|
7
|
-
* appDir: playgroundDir,
|
|
8
|
-
* userDataDir: path.join(playgroundDir, ".user-data-dir-test"),
|
|
9
|
-
* serverUrl: `http://localhost:${process.env.NUXT_PORT}`,
|
|
10
|
-
* windowUrlPrefix: "app://bundle"
|
|
11
|
-
* })
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
export declare function launchApp({ appDir, userDataDir, serverUrl, windowUrlPrefix, useDevServer, env, extraArgs, windowTimeout }: {
|
|
15
|
-
appDir: string;
|
|
16
|
-
userDataDir: string;
|
|
17
|
-
serverUrl: string;
|
|
18
|
-
windowUrlPrefix: string;
|
|
19
|
-
useDevServer?: boolean;
|
|
20
|
-
env?: Record<string, string>;
|
|
21
|
-
extraArgs?: string[];
|
|
22
|
-
windowTimeout?: number;
|
|
23
|
-
}): Promise<{
|
|
24
|
-
app: ElectronApplication;
|
|
25
|
-
page: Page;
|
|
26
|
-
close: () => Promise<void>;
|
|
27
|
-
}>;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { _electron as electron } from "@playwright/test";
|
|
2
|
-
import { rm } from "node:fs/promises";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
export async function launchApp({
|
|
5
|
-
appDir,
|
|
6
|
-
userDataDir,
|
|
7
|
-
serverUrl,
|
|
8
|
-
windowUrlPrefix,
|
|
9
|
-
useDevServer = false,
|
|
10
|
-
env = {},
|
|
11
|
-
extraArgs = [],
|
|
12
|
-
windowTimeout = 3e4
|
|
13
|
-
}) {
|
|
14
|
-
const relative = path.relative(path.resolve(appDir), path.resolve(userDataDir));
|
|
15
|
-
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
16
|
-
throw new Error(`userDataDir must be inside appDir, got ${userDataDir}.`);
|
|
17
|
-
}
|
|
18
|
-
await rm(userDataDir, { recursive: true, force: true });
|
|
19
|
-
const app = await electron.launch({
|
|
20
|
-
// launched as an app directory so `app.getAppPath()` is the app root, which is where
|
|
21
|
-
// @witchcraft/nuxt-logger resolves pino's workers from
|
|
22
|
-
args: [".", "--no-sandbox", "--ignore-certificate-errors", `--user-data-dir=${userDataDir}`, ...extraArgs],
|
|
23
|
-
cwd: appDir,
|
|
24
|
-
env: {
|
|
25
|
-
...process.env,
|
|
26
|
-
...env,
|
|
27
|
-
DISABLE_AUTO_UPDATES: "true",
|
|
28
|
-
OVERRIDE_PUBLIC_SERVER_URL: serverUrl,
|
|
29
|
-
...useDevServer ? { VITE_DEV_SERVER_URL: serverUrl } : {}
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
const deadline = Date.now() + windowTimeout;
|
|
33
|
-
const watched = /* @__PURE__ */ new Map();
|
|
34
|
-
const watch = (candidate) => {
|
|
35
|
-
let pending = watched.get(candidate);
|
|
36
|
-
if (!pending) {
|
|
37
|
-
pending = candidate.waitForURL((url) => url.toString().startsWith(windowUrlPrefix), { timeout: Math.max(deadline - Date.now(), 1) }).then(() => candidate).catch(() => void 0);
|
|
38
|
-
watched.set(candidate, pending);
|
|
39
|
-
}
|
|
40
|
-
return pending;
|
|
41
|
-
};
|
|
42
|
-
let page;
|
|
43
|
-
while (!page && Date.now() < deadline) {
|
|
44
|
-
const next = await Promise.race([
|
|
45
|
-
...app.windows().map(watch),
|
|
46
|
-
app.waitForEvent("window", { timeout: Math.max(deadline - Date.now(), 1) }).catch(() => void 0)
|
|
47
|
-
]);
|
|
48
|
-
if (next && next.url().startsWith(windowUrlPrefix)) page = next;
|
|
49
|
-
}
|
|
50
|
-
if (!page) {
|
|
51
|
-
const seen = app.windows().map((candidate) => candidate.url());
|
|
52
|
-
throw new Error(`No window with url starting with ${windowUrlPrefix}, saw: ${seen.join(", ") || "no windows"}.`);
|
|
53
|
-
}
|
|
54
|
-
await page.waitForLoadState();
|
|
55
|
-
const close = async () => {
|
|
56
|
-
await app.close();
|
|
57
|
-
};
|
|
58
|
-
return { app, page, close };
|
|
59
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { _electron as electron, type ElectronApplication, type Page } from "@playwright/test"
|
|
2
|
-
import { rm } from "node:fs/promises"
|
|
3
|
-
import path from "node:path"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Launches the built app for a test, starting from a clean profile every time.
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* const { app, page, close } = await launchApp({
|
|
11
|
-
* appDir: playgroundDir,
|
|
12
|
-
* userDataDir: path.join(playgroundDir, ".user-data-dir-test"),
|
|
13
|
-
* serverUrl: `http://localhost:${process.env.NUXT_PORT}`,
|
|
14
|
-
* windowUrlPrefix: "app://bundle"
|
|
15
|
-
* })
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export async function launchApp(
|
|
19
|
-
{
|
|
20
|
-
appDir,
|
|
21
|
-
userDataDir,
|
|
22
|
-
serverUrl,
|
|
23
|
-
windowUrlPrefix,
|
|
24
|
-
useDevServer = false,
|
|
25
|
-
env = {},
|
|
26
|
-
extraArgs = [],
|
|
27
|
-
windowTimeout = 30_000
|
|
28
|
-
}: {
|
|
29
|
-
/* Directory the app is launched from, becomes the electron process cwd. A module playground passes its playground dir, an app passes its repo root. */
|
|
30
|
-
appDir: string
|
|
31
|
-
/* Profile directory, wiped before every launch. Point it somewhere gitignored. */
|
|
32
|
-
userDataDir: string
|
|
33
|
-
/* Server the app should talk to. */
|
|
34
|
-
serverUrl: string
|
|
35
|
-
/* Only a window whose url starts with this is returned. The bundle prefix normally, serverUrl in dev. */
|
|
36
|
-
windowUrlPrefix: string
|
|
37
|
-
/* Points the app at serverUrl with VITE_DEV_SERVER_URL instead of at its bundle. Only works when the app leaves the windowUrl override open, so a baked production flag must be false. */
|
|
38
|
-
useDevServer?: boolean
|
|
39
|
-
/* Extra env for the app process, merged last so it wins. */
|
|
40
|
-
env?: Record<string, string>
|
|
41
|
-
/* Extra args for the electron process, appended after the fixed args. */
|
|
42
|
-
extraArgs?: string[]
|
|
43
|
-
/* How long to wait for the matching window before throwing. */
|
|
44
|
-
windowTimeout?: number
|
|
45
|
-
}
|
|
46
|
-
): Promise<{ app: ElectronApplication, page: Page, close: () => Promise<void> }> {
|
|
47
|
-
const relative = path.relative(path.resolve(appDir), path.resolve(userDataDir))
|
|
48
|
-
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
49
|
-
throw new Error(`userDataDir must be inside appDir, got ${userDataDir}.`)
|
|
50
|
-
}
|
|
51
|
-
await rm(userDataDir, { recursive: true, force: true })
|
|
52
|
-
|
|
53
|
-
const app = await electron.launch({
|
|
54
|
-
// launched as an app directory so `app.getAppPath()` is the app root, which is where
|
|
55
|
-
// @witchcraft/nuxt-logger resolves pino's workers from
|
|
56
|
-
args: [".", "--no-sandbox", "--ignore-certificate-errors", `--user-data-dir=${userDataDir}`, ...extraArgs],
|
|
57
|
-
cwd: appDir,
|
|
58
|
-
env: {
|
|
59
|
-
...process.env,
|
|
60
|
-
...env,
|
|
61
|
-
DISABLE_AUTO_UPDATES: "true",
|
|
62
|
-
OVERRIDE_PUBLIC_SERVER_URL: serverUrl,
|
|
63
|
-
...(useDevServer ? { VITE_DEV_SERVER_URL: serverUrl } : {})
|
|
64
|
-
} as Record<string, string>
|
|
65
|
-
})
|
|
66
|
-
// the url is not set yet when a window appears, so watch every window for its url and race that against new windows arriving
|
|
67
|
-
const deadline = Date.now() + windowTimeout
|
|
68
|
-
const watched = new Map<Page, Promise<Page | undefined>>()
|
|
69
|
-
const watch = (candidate: Page) => {
|
|
70
|
-
let pending = watched.get(candidate)
|
|
71
|
-
if (!pending) {
|
|
72
|
-
pending = candidate.waitForURL(url => url.toString().startsWith(windowUrlPrefix), { timeout: Math.max(deadline - Date.now(), 1) }).then(() => candidate).catch(() => undefined)
|
|
73
|
-
watched.set(candidate, pending)
|
|
74
|
-
}
|
|
75
|
-
return pending
|
|
76
|
-
}
|
|
77
|
-
let page: Page | undefined
|
|
78
|
-
while (!page && Date.now() < deadline) {
|
|
79
|
-
const next = await Promise.race([
|
|
80
|
-
...app.windows().map(watch),
|
|
81
|
-
app.waitForEvent("window", { timeout: Math.max(deadline - Date.now(), 1) }).catch(() => undefined)
|
|
82
|
-
])
|
|
83
|
-
if (next && next.url().startsWith(windowUrlPrefix)) page = next
|
|
84
|
-
}
|
|
85
|
-
if (!page) {
|
|
86
|
-
const seen = app.windows().map(candidate => candidate.url())
|
|
87
|
-
throw new Error(`No window with url starting with ${windowUrlPrefix}, saw: ${seen.join(", ") || "no windows"}.`)
|
|
88
|
-
}
|
|
89
|
-
await page.waitForLoadState()
|
|
90
|
-
const close = async () => {
|
|
91
|
-
await app.close()
|
|
92
|
-
}
|
|
93
|
-
return { app, page, close }
|
|
94
|
-
}
|