playwright-record-and-mock
v0.2.1
Published
## a playwright tool for record api and replay
Readme
Api recorder and mock tool for playwright
a playwright tool for record api and replay
Why this tool born?
Why not use playwright native recordHar option?
The recordHar function of playwright use exact match of url,body,headers content for mock data.
This tool will record api data by request order, and replay them with the same order.
Precondition
⚠️ Must install @playwright/test.
Install
yarn add -D @playwright/test playwright-record-and-mockUsage steps
After install it, the pram cli is avalable, it is the shortcase of playwright-record-and-mock.
Create
playwrightts format config, content like below.import type { PlaywrightTestConfig } from '@playwright/test' const config: PlaywrightTestConfig = { projects: [ { name: 'Chrome Stable', testDir: 'e2e', use: { headless: false, browserName: 'chromium', channel: 'chrome', viewport: { width: 1920, height: 1080 }, }, }, ], } export default configrun
pramcommand for configure.pram initThe command will change
playwright.config.tscontent.Then the
playwright.config.tsshould like below.import type { PlaywrightTestConfig } from '@playwright/test' import type { PramConfig } from 'playwright-record-and-mock' const config: PlaywrightTestConfig & PramConfig = { projects: [ { name: 'Chrome Stable', testDir: 'e2e', use: { headless: false, browserName: 'chromium', channel: 'chrome', viewport: { width: 1920, height: 1080 }, }, }, ], pram: { outDir: 'e2e', site: 'https://your.host/', urlFilter: /\/api\//, }, } export default configOr else you can copy the
prampart intoplaywright.config.tsby yourself.run record by
pramcommand.Record your test case in the browser by your own.
yarn pram mytest1After record some browser acitons, close the browser and it will create dir
e2e/mytest1, which contains the test case files.The test case file will looks like below.
import { test, expect } from '@playwright/test' import { mock } from "playwright-record-and-mock" test('test', async ({ page }) => { mock(page, __dirname) ... })For now,
pramfinishes its work, the next is all the work ofplaywright.run
playwright test.
yarn playwright testApi support.
json
text
TODO
- custom plugable rule for record and mock.
