Skip to content

request-mocking.md "Delayed Response" example uses setTimeout, which run-code doesn't provide #471

Description

@ezrover

The "Delayed Response" example in skills/playwright-cli/references/request-mocking.md doesn't work, because run-code has no setTimeout:

playwright-cli run-code "async page => {
  await page.route('**/api/slow', async route => {
    await new Promise(r => setTimeout(r, 3000));
    route.fulfill({ body: JSON.stringify({ data: 'loaded' }) });
  });
}"

When the route fires, the handler throws ReferenceError: setTimeout is not defined, and the request never gets fulfilled (it hangs until the client gives up).

Repro (playwright-cli 0.1.21, Chrome 153, macOS):

playwright-cli -s=t open http://127.0.0.1:8767/page.html
playwright-cli -s=t run-code "async page => { await page.route('**/api/slow', async route => { await new Promise(r => setTimeout(r, 300)); route.fulfill({ body: 'loaded' }); }); }"
playwright-cli -s=t --raw eval "async () => { const c = new AbortController(); setTimeout(() => c.abort(), 5000); try { const r = await fetch('/api/slow', { signal: c.signal }); return r.status; } catch (e) { return e.name; } }"
# prints ReferenceError: setTimeout is not defined, then "AbortError"

run-code "async page => typeof setTimeout" returns "undefined". The same is true of URL, fetch, Buffer, crypto and AbortController.

Working version:

await page.route('**/api/slow', async route => {
  await page.waitForTimeout(3000);
  route.fulfill({ body: JSON.stringify({ data: 'loaded' }) });
});

It would also help if running-code.md said that run-code runs in a bare context without Node or timer globals.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions