Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Automate browser interactions for web testing, form filling, screenshots, and data extraction using the playwright-cli tool.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/request-mocking.md
1# Request Mocking23Intercept, mock, modify, and block network requests.45## CLI Route Commands67```bash8# Mock with custom status9playwright-cli route "**/*.jpg" --status=4041011# Mock with JSON body12playwright-cli route "**/api/users" --body='[{"id":1,"name":"Alice"}]' --content-type=application/json1314# Mock with custom headers15playwright-cli route "**/api/data" --body='{"ok":true}' --header="X-Custom: value"1617# Remove headers from requests18playwright-cli route "**/*" --remove-header=cookie,authorization1920# List active routes21playwright-cli route-list2223# Remove a route or all routes24playwright-cli unroute "**/*.jpg"25playwright-cli unroute26```2728## URL Patterns2930```31**/api/users - Exact path match32**/api/*/details - Wildcard in path33**/*.{png,jpg,jpeg} - Match file extensions34**/search?q=* - Match query parameters35```3637## Advanced Mocking with run-code3839For conditional responses, request body inspection, response modification, or delays:4041### Conditional Response Based on Request4243```bash44playwright-cli run-code "async page => {45await page.route('**/api/login', route => {46const body = route.request().postDataJSON();47if (body.username === 'admin') {48route.fulfill({ body: JSON.stringify({ token: 'mock-token' }) });49} else {50route.fulfill({ status: 401, body: JSON.stringify({ error: 'Invalid' }) });51}52});53}"54```5556### Modify Real Response5758```bash59playwright-cli run-code "async page => {60await page.route('**/api/user', async route => {61const response = await route.fetch();62const json = await response.json();63json.isPremium = true;64await route.fulfill({ response, json });65});66}"67```6869### Simulate Network Failures7071```bash72playwright-cli run-code "async page => {73await page.route('**/api/offline', route => route.abort('internetdisconnected'));74}"75# Options: connectionrefused, timedout, connectionreset, internetdisconnected76```7778### Delayed Response7980```bash81playwright-cli run-code "async page => {82await page.route('**/api/slow', async route => {83await new Promise(r => setTimeout(r, 3000));84route.fulfill({ body: JSON.stringify({ data: 'loaded' }) });85});86}"87```88