electron-playwright-cli 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -32,6 +32,35 @@ Optional launch options:
32
32
  - `env`: additional environment variables
33
33
  - `timeout`: launch timeout in milliseconds
34
34
 
35
+ ### App readiness
36
+
37
+ By default, the CLI waits for the `load` event and a paint frame before accepting commands. For apps with async rendering (React, etc.), the first screenshot may still capture a blank page. Use `readyCondition` to tell the CLI when your app is actually ready:
38
+
39
+ ```json
40
+ {
41
+ "browser": {
42
+ "launchOptions": {
43
+ "args": ["main.js"]
44
+ },
45
+ "readyCondition": {
46
+ "waitForSelector": "[data-testid='app-ready']",
47
+ "timeout": 10000
48
+ }
49
+ }
50
+ }
51
+ ```
52
+
53
+ Available options (all optional, can be combined):
54
+
55
+ | Option | Type | Description |
56
+ |---|---|---|
57
+ | `waitForSelector` | CSS selector | Waits for the element to be visible |
58
+ | `waitForFunction` | JS expression | Waits for the expression to return truthy (e.g. `"document.fonts.ready"`) |
59
+ | `waitForLoadState` | `"load"` \| `"domcontentloaded"` \| `"networkidle"` | Waits for the specified load state |
60
+ | `timeout` | number (ms) | Timeout for all conditions (default: 10000) |
61
+
62
+ A double `requestAnimationFrame` paint wait always runs after any conditions, ensuring pixels are rendered before the first command.
63
+
35
64
  ## Usage
36
65
 
37
66
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electron-playwright-cli",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Playwright CLI with Electron app support",
5
5
  "keywords": [
6
6
  "playwright",
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "license": "Apache-2.0",
28
28
  "scripts": {
29
- "check": "npm run fmt:check && npm run lint && npm run typecheck",
29
+ "check": "npm run fmt && npm run lint && npm run typecheck",
30
30
  "fmt": "oxfmt .",
31
31
  "fmt:check": "oxfmt --check .",
32
32
  "lint": "oxlint .",
@@ -33,13 +33,11 @@ class ElectronContextFactory {
33
33
  await electronApp.firstWindow();
34
34
  }
35
35
 
36
- // wait for the page to finish loading before handing it off —
36
+ // wait for the page to be ready before handing it off —
37
37
  // without this, screenshots taken immediately can capture a blank page
38
38
  const firstPage = browserContext.pages()[0];
39
39
  if (firstPage) {
40
- await firstPage
41
- .waitForLoadState("domcontentloaded", { timeout: 10000 })
42
- .catch(() => {});
40
+ await this._waitForReady(firstPage);
43
41
  }
44
42
 
45
43
  electronApp.process().on("exit", () => {
@@ -57,6 +55,57 @@ class ElectronContextFactory {
57
55
  throw e;
58
56
  }
59
57
  }
58
+
59
+ // waits for the app to be ready using either user-provided conditions
60
+ // from config.browser.readyCondition, or a sensible default.
61
+ //
62
+ // config example:
63
+ // "readyCondition": {
64
+ // "waitForLoadState": "load",
65
+ // "waitForSelector": "[data-testid='app-ready']",
66
+ // "waitForFunction": "document.fonts.ready",
67
+ // "timeout": 10000
68
+ // }
69
+ async _waitForReady(page) {
70
+ const ready = this.config.browser?.readyCondition ?? {};
71
+ const timeout = ready.timeout ?? 10000;
72
+
73
+ // if user provided custom conditions, use those
74
+ if (
75
+ ready.waitForLoadState ||
76
+ ready.waitForSelector ||
77
+ ready.waitForFunction
78
+ ) {
79
+ if (ready.waitForLoadState) {
80
+ await page
81
+ .waitForLoadState(ready.waitForLoadState, { timeout })
82
+ .catch(() => {});
83
+ }
84
+ if (ready.waitForSelector) {
85
+ await page
86
+ .waitForSelector(ready.waitForSelector, { state: "visible", timeout })
87
+ .catch(() => {});
88
+ }
89
+ if (ready.waitForFunction) {
90
+ await page
91
+ .waitForFunction(ready.waitForFunction, null, { timeout })
92
+ .catch(() => {});
93
+ }
94
+ } else {
95
+ // default: wait for load event (all resources loaded)
96
+ await page.waitForLoadState("load", { timeout }).catch(() => {});
97
+ }
98
+
99
+ // always wait for at least one paint frame to ensure pixels are rendered
100
+ await page
101
+ .evaluate(
102
+ () =>
103
+ new Promise((resolve) =>
104
+ requestAnimationFrame(() => requestAnimationFrame(resolve)),
105
+ ),
106
+ )
107
+ .catch(() => {});
108
+ }
60
109
  }
61
110
 
62
111
  module.exports = { ElectronContextFactory };
@@ -39,6 +39,33 @@ Optional launch options:
39
39
  - `env`: additional environment variables
40
40
  - `timeout`: launch timeout in milliseconds
41
41
 
42
+ ### App readiness
43
+
44
+ By default, the CLI waits for the `load` event and a paint frame before accepting commands. For apps with async rendering (React, etc.), use `readyCondition` to tell the CLI when your app is actually ready:
45
+
46
+ ```json
47
+ {
48
+ "browser": {
49
+ "launchOptions": {
50
+ "args": ["main.js"]
51
+ },
52
+ "readyCondition": {
53
+ "waitForSelector": "[data-testid='app-ready']",
54
+ "timeout": 10000
55
+ }
56
+ }
57
+ }
58
+ ```
59
+
60
+ Available options (all optional, can be combined):
61
+
62
+ | Option | Type | Description |
63
+ |---|---|---|
64
+ | `waitForSelector` | CSS selector | Waits for the element to be visible |
65
+ | `waitForFunction` | JS expression | Waits for the expression to return truthy (e.g. `"document.fonts.ready"`) |
66
+ | `waitForLoadState` | `"load"` \| `"domcontentloaded"` \| `"networkidle"` | Waits for the specified load state |
67
+ | `timeout` | number (ms) | Timeout for all conditions (default: 10000) |
68
+
42
69
  ## Commands
43
70
 
44
71
  ### Core