ohos-playwright 0.2.2 → 0.2.3

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.
Files changed (2) hide show
  1. package/README.md +41 -125
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,166 +1,82 @@
1
1
  # ohos-playwright
2
2
 
3
- Playwright OpenHarmony / ArkWeb 上跑 e2e,同一份 `playwright.config.ts` spec Windows / Linux / macOS 上仍按原版 Playwright 跑,**不需要写两份**。
3
+ Playwright adapter for **HarmonyOS / OpenHarmony ArkWeb** (Chromium 132-based) — runs your existing Playwright e2e suite against the device's system browser over CDP, no bundled browser binaries needed. Spec files don't change.
4
4
 
5
- ## 它解决什么问题
5
+ The MCP server counterpart lives at [`ohos-playwright-mcp`](https://github.com/social4hyq/ohos-playwright-mcp).
6
6
 
7
- OpenHarmony 上没有官方的 Playwright:没有适配的浏览器二进制,Playwright 的平台检测在 `openharmony` 上直接崩。
7
+ ## Why this exists
8
8
 
9
- ohos-playwright **不让 Playwright 自己启动浏览器**,而是通过 `hdc` 把设备上正在运行的系统浏览器的 CDP 调试通道转发回本机,让 Playwright "接管"它。spec 一行不动,config 只多一层包装。
9
+ Playwright's bundled browsers don't ship for the `openharmony` platform, and ArkWeb's sandbox blocks the `AF_UNIX` sockets Playwright uses internally — running stock Playwright on a HarmonyOS PC just crashes. This adapter skips the binary download, uses `hdc` to attach to the browser that's already on the device, and drives it through the Chrome DevTools Protocol over a TCP forward. If the browser isn't running it's launched automatically; when the test run ends the `hdc` forward is cleaned up but the browser stays open.
10
10
 
11
- ## 快速开始
11
+ On Windows / macOS / Linux `withOpenHarmony` is a no-op and the config passes through to stock Playwright.
12
12
 
13
- ### 1. 安装
13
+ ## Install
14
14
 
15
- ```sh
15
+ ```bash
16
16
  pnpm add -D ohos-playwright @playwright/test
17
17
  ```
18
18
 
19
- > `@playwright/test` peer dependency,版本 `>=1.59.0`。Node 24。
19
+ Node ≥ 24. `hdc` must be on `PATH` and an OpenHarmony / HarmonyOS device reachable.
20
20
 
21
- ### 2. 包一层 config
21
+ ## Wire it into playwright.config
22
22
 
23
23
  ```ts
24
- // playwright.config.ts
25
- import { defineConfig, devices } from '@playwright/test'
24
+ import { defineConfig } from '@playwright/test'
26
25
  import { withOpenHarmony } from 'ohos-playwright/config'
27
26
 
28
- export default defineConfig(withOpenHarmony({
29
- testDir: './e2e',
30
- use: { baseURL: 'http://localhost:5173' },
31
- projects: [
32
- { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
33
- // firefox / webkit 在 OpenHarmony 上会被自动裁掉
34
- ],
35
- }))
27
+ export default defineConfig(withOpenHarmony({ /* your config */ }))
36
28
  ```
37
29
 
38
- `withOpenHarmony` 在非 OH 主机上原样返回 config;在 OH 上注入 setup/teardown、锁 workers=1、过滤非 chromium project。
39
-
40
- ### 3. npm script
41
-
42
30
  ```json
43
31
  { "scripts": { "test:e2e": "ohos-playwright test" } }
44
32
  ```
45
33
 
46
- `ohos-playwright` 在非 OH 上等价于 `playwright`,所有参数透传。
47
-
48
- ### 4. spec 不用动
49
-
50
- ```ts
51
- import { test, expect } from '@playwright/test'
52
-
53
- test('something', async ({ page }) => {
54
- await page.goto('/')
55
- await expect(page).toHaveTitle(/.../)
56
- })
57
- ```
34
+ ## Limitations
58
35
 
59
- ### 5. 运行
36
+ - **Chromium only.** firefox and webkit aren't available on HarmonyOS.
37
+ - **One context, one page.** `newContext()` / `newPage()` aren't supported. Isolate tests with `localStorage.clear()` + `page.reload()`.
38
+ - **`process.platform` reads `'linux'`** during the run — we patch it because Playwright's hostPlatform detection only branches on linux/darwin/win32 and falls through to `<unknown>` on openharmony. For real platform checks use `process.env.OHOS_PW_HOST`.
60
39
 
61
- ```sh
62
- pnpm test:e2e
63
- ```
40
+ ## Environment variables
64
41
 
65
- 首次跑会自动找设备、拉起浏览器、转发 CDP 端口。如果本机就是 OpenHarmony 设备,会通过 `param get persist.hdc.port` 自动发现端口并连接,无需手动配置。
66
-
67
- ## 工作原理
68
-
69
- **非 OpenHarmony 主机**:什么都不做,等价于 stock Playwright。
70
-
71
- **OpenHarmony 主机**:
72
-
73
- - **复用而非启动**:Playwright 不再自己起浏览器,接管设备上正在跑的系统浏览器,通过 `hdc` 把 CDP 通道转发到本机。
74
- - **Spec 无感**:底层透明替换 `@playwright/test` 导入,走 CDP 复用设备上现有 page。
75
- - **配置自动收紧**:`workers` 锁 1、firefox/webkit 裁掉、globalSetup/Teardown 自动注入。
76
- - **智能设备发现**(v0.2.0):
77
- 1. 已有连接 → 直接复用
78
- 2. 本机就是 OH 设备 → 读 `persist.hdc.port` 秒连
79
- 3. LAN 广播 `hdc discover` → 找独立设备
80
- 4. 找不到 → TTY 提示手动输入 / CI 直接报错
42
+ | Variable | Default |
43
+ |---|---|
44
+ | `OHOS_PW_BUNDLE` | `com.huawei.hmos.browser` |
45
+ | `OHOS_PW_LAUNCH_URL` | `http://localhost:5173` |
46
+ | `OHOS_PW_HDC` | `/data/service/hnp/bin/hdc` |
47
+ | `OHOS_PW_AUTO_CONNECT` | auto (set `0` to skip device auto-connect) |
48
+ | `OHOS_PW_INFO_PATH` | `<tmpdir>/ohos-playwright-cdp.json` |
81
49
 
82
- ## 设备连接
50
+ ## Compatibility
83
51
 
84
- ### 本机即设备(一台 OH 机器跑全部)
52
+ | | Required |
53
+ |---|---|
54
+ | Node | ≥ 24 |
55
+ | Playwright | ≥ 1.59 |
56
+ | Verified browser | `com.huawei.hmos.browser` (Chromium 132) |
85
57
 
86
- 无需额外配置,`ensureDeviceConnected()` 会自动通过 `param get persist.hdc.port` 连上本机 hdc 守护进程。
58
+ ## Troubleshooting
87
59
 
88
- ### 固定无线调试端口(一劳永逸)
60
+ **`Cannot find module 'ohos-playwright/config'`** — not installed, or `pnpm install` not run.
89
61
 
90
- 无线调试每次开关端口都会变。设固定端口后不再需要看屏幕:
62
+ **`defineConfig(...)` has no type hints** — set `moduleResolution` to `bundler` or `nodenext` in tsconfig.
91
63
 
92
- ```sh
93
- # 先连上(USB 或看屏幕临时端口)
94
- hdc tconn 127.0.0.1:<临时端口>
64
+ **`未发现设备` / no device found** — on the device, enable Developer Options → Wireless Debugging, make sure it's on the same Wi-Fi as the host, and allow inbound UDP:8710 (used by `hdc discover` broadcast). In CI, run `hdc tconn <ip:port>` manually before tests.
95
65
 
96
- # 设固定端口(会重启设备)
97
- hdc tmode port 5555
66
+ **`Failed to launch` the browser** — bundle not installed or wrong bundle name. List installed browsers with:
98
67
 
99
- # 之后永远用固定端口
100
- hdc tconn 127.0.0.1:5555
101
68
  ```
102
-
103
- 设好后 `param get persist.hdc.port` 也返回固定值,ohos-playwright 自动识别。
104
-
105
- ### 跳过自动连接
106
-
107
- ```sh
108
- OHOS_PW_AUTO_CONNECT=0 pnpm test:e2e
69
+ hdc shell "bm dump -a" | grep -iE "browser|webview|chrom|arkweb"
109
70
  ```
110
71
 
111
- ## 环境变量
112
-
113
- | 变量 | 默认值 | 说明 |
114
- |---|---|---|
115
- | `OHOS_PW_HDC` | `/data/service/hnp/bin/hdc` | hdc 二进制路径 |
116
- | `OHOS_PW_BUNDLE` | `com.huawei.hmos.browser` | 接管的目标浏览器 bundle |
117
- | `OHOS_PW_LAUNCH_URL` | `http://localhost:5173` | 浏览器首次启动时的导航 URL |
118
- | `OHOS_PW_INFO_PATH` | `os.tmpdir()/ohos-playwright-cdp.json` | CDP info 文件位置 |
119
- | `OHOS_PW_AUTO_CONNECT` | (未设) | 设为 `0` 跳过自动 discover/tconn 流程 |
120
- | `OHOS_PW_HOST` | 自动 | 内部标志位,**不要手动设** |
121
-
122
- ## 约束
123
-
124
- 设备和 ArkWeb 本身的硬约束:
125
-
126
- - **`workers: 1`**:设备上只有一个浏览器实例。
127
- - **不能并发 project**:firefox/webkit 在 ArkWeb 上跑不了,自动裁掉。
128
- - **不能 `newContext` / `newPage`**:全进程复用一个 context、一个 page。用例隔离用 `localStorage.clear()` + `page.reload()`。
129
- - **测试结束不关浏览器**:浏览器进程归 OS 管。
130
- - **`process.platform` 被改成 `'linux'`**:判断真实主机用 `process.env.OHOS_PW_HOST`。
131
- - **设备需要开发者模式 + 无线调试或 USB 调试**。
132
-
133
- ## 排错
134
-
135
- | 报错 | 处理 |
136
- |---|---|
137
- | `Cannot find module 'ohos-playwright/config'` | 没装或没 install |
138
- | `defineConfig({...})` 没有类型提示 | `tsconfig` 的 `moduleResolution` 改成 `bundler` 或 `nodenext` |
139
- | `未发现设备...` | 设备上开「开发者选项 → 无线调试」;本机防火墙放行 UDP:8710;CI 预先 `hdc tconn` |
140
- | `Failed to launch com.huawei.hmos.browser` | 设备上没装该浏览器,或设 `OHOS_PW_BUNDLE` |
141
- | `DevTools socket not found for pid` | 浏览器没暴露 CDP,或等几秒重试 |
142
- | `CDP probe failed` | `hdc fport ls` 查现 ruler,`hdc fport rm` 清残留 |
143
- | `await page.goto('/foo')` 不拼 baseURL | 检查 `use.baseURL` 是否非空 |
144
-
145
- ## TypeScript
146
-
147
- 项目源码使用 TypeScript(`.mts`),Node 24 原生支持类型剥离,无需编译步骤。
148
-
149
- 支持类型检查:
150
-
151
- ```sh
152
- npm run typecheck # tsc --noEmit
153
- ```
72
+ Then set `OHOS_PW_BUNDLE` (e.g. `OHOS_PW_BUNDLE=com.quark.ohosbrowser`).
154
73
 
155
- `withOpenHarmony` `ohos-playwright/config` 导入时有完整的 `PlaywrightTestConfig` 类型提示(直接从源码推断,无需 `.d.ts` 文件)。
74
+ **`DevTools socket not found`** browser doesn't expose CDP, or hasn't finished loading. Usually a retry after a few seconds works; persistent failure means this browser doesn't support CDP.
156
75
 
157
- ## 兼容性
76
+ **`CDP probe failed`** — leftover `hdc` forward rule from a prior crashed run. `hdc fport ls` to inspect, `hdc fport rm tcp:<port> localabstract:<socket>` to clear.
158
77
 
159
- - **Playwright**:`>=1.59.0`
160
- - **Node**:`>=24`(OpenHarmony 上只支持 Node 24+)
161
- - **OpenHarmony**:`hdc` 可用 + 系统浏览器暴露 CDP。已验证 `com.huawei.hmos.browser`(华为浏览器,Chromium 132)
162
- - **其他主机**:Windows / Linux / macOS 上自动降级为 stock Playwright,无副作用
78
+ **`page.goto('/foo')` doesn't prepend baseURL**Playwright's standard behavior is `/foo` → `http://localhost:5173/foo`. If it's not working, check `use.baseURL` in `playwright.config.ts`.
163
79
 
164
- ## 协议
80
+ ## License
165
81
 
166
- MIT
82
+ MIT © 2026 social4hyq
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ohos-playwright",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Playwright adapter for OpenHarmony / ArkWeb via hdc + CDP",
5
5
  "license": "MIT",
6
6
  "author": "social4hyq",