Playwright 互換 API を WebDriver BiDi 上に実装した MoonBit ライブラリです。
関数名は MoonBit 流儀の snake_case で提供します。
moon add mizchi/playwrightmizchi/playwright: ブラウザ操作 API(chromium,firefox,webkitなど)mizchi/playwright/test:expectとテスト DSL(retry,timeout,to_pass,expect_poll,step,skip,fail,fixme,shardなど)
import "mizchi/playwright" as @pw
import "mizchi/playwright/test" as @pwt
let chromium = @pw.chromium()
let _ = @pwt.expect(chromium.name()).to_equal("chromium")
async test "dsl helpers" {
@pwt.configure_test(
retry_times=3,
timeout_ms=3_000,
expect_timeout_ms=800,
expect_interval_ms=10,
shard_index=1,
shard_total=2,
)
let _ = @pwt.step("open", fn() -> Int { 1 })
// shard は設定値を省略可能
@pwt.shard(item_index=0, item_total=2, fn() {})
// retry / timeout は設定値をデフォルトとして使える
let _ = @pwt.retry(async fn() -> Int { 1 })
let _ = @pwt.timeout(async fn() -> Int { 1 })
// 一定時間リトライしながら assertion
@pwt.to_pass(async fn() {
@pwt.expect("ok").to_equal("ok")
})
// expect.poll 相当
@pwt.expect_poll(async fn() -> String { "done-1" }).to_match("done-\\d+")
}@playwright/test 側で globalSetup を使うと、ブラウザサーバーを 1 回だけ起動して wsEndpoint を複数テストで共有できます。
// test/integration/global.setup.js
const { chromium } = require("@playwright/test")
const fs = require("node:fs/promises")
const path = require("node:path")
module.exports = async () => {
const server = await chromium.launchServer({ headless: true })
const runtimeDir = path.join(__dirname, ".runtime")
await fs.mkdir(runtimeDir, { recursive: true })
await fs.writeFile(
path.join(runtimeDir, "state.json"),
JSON.stringify({ ws_endpoint: server.wsEndpoint() }),
"utf-8",
)
return async () => {
await server.close()
}
}// test/integration/config_bridge.spec.js (抜粋)
const state = JSON.parse(fs.readFileSync(".runtime/state.json", "utf-8"))
const wsEndpoint = state.ws_endpoint
spawnSync("moon", [
"run",
"src/playwright/test_utils/integration_bridge",
"--target",
"js",
/* retry/timeout/shard など */,
wsEndpoint,
])このリポジトリでは上記方式を test/integration/ で実装済みです。