Mocking browser APIs: clock, geolocation, clipboard and more
Replace parts of the browser with stage props, such as the time, the place, the clipboard or the battery, before the page loads, so a test can control what the page sees.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: stage props, swapped before the curtain. In a play, a real knife would be dangerous. The actors use a prop that looks right. The stage manager swaps the prop before the scene, not during it.
Tom tests Daily List, a to-do list app. A reminder should appear at nine in the morning. He cannot wait until morning. A share button copies a link, and a real clipboard is awkward to read. Another button asks the browser where the user is.
What it is: Playwright is a free program, made by Microsoft, that drives real web browsers from test code. This topic is about standing in for features the browser gives the page. A (application programming interface mock) is a prop for one of those features, such as the battery report. A is a stand-in for the browser's time that the test can set and move. An init script is a small script Playwright runs in the page before the page's own scripts. Do not mix these props with the real clock, the real clipboard or the real place.
Why it matters: without a fake clock, Tom either waits until nine or he skips the reminder. Without a clipboard prop, he cannot see the link the page copied. The props let him prove the reminder and the share button in one short run.
A prop is not the real thing. A fake clock does not show what a wrong phone clock does.
Kept simple: Playwright already has settings for place, language and . Use a script only for the rest.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named here are invented.)
PLAIN WORDS · 3 in this section
- clipboard
- a board with a clip for holding papers
- playwright
- a free Microsoft tool that drives a browser for tests
- init
- short for initialise: set up for the first time
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Controlling what the page sees from the browser, such as the time, the place or the clipboard, so a test can be fast and repeatable.
- Pick the simplest tool
- Swap before the load
- Run the scene
- Keep a real check
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The real knife | The real browser feature |
| The prop knife | The mock |
| Swapping before the curtain goes up | Adding the init script before the page loads |
| The stage manager's tools | Built-in options and the clock tool |
WHERE THIS PICTURE STOPS
- A prop cannot cut. A mock cannot tell you how a real phone behaves.
- Props are swapped before the scene. A mock added after the page loads is too late.
- A prop that looks wrong spoils the play. A mock with the wrong shape breaks the page.
HOOK
Props in before the curtain, real tools still checked.
REMEMBER IT THIS WAY
A contrast: two neighbours told apart by one test.
Swap the prop before the curtain goes up. Add the before the page loads, never after.
| In the trick | What it stands for |
|---|---|
| prop | the mock |
| curtain | the page loads |
A mock cannot tell you how a real phone behaves.
TELL A FRIEND
Swap real tools for stage props before the curtain goes up, and still test with the real tools sometimes.
OFTEN MIXED UP WITH Controlling time: the clock. The clock lesson goes deep on time. This lesson shows the general way to swap browser features.
Sources & standards — 4 sources
- Market practice
Mock browser APIs (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
BrowserContext (Playwright documentation) ↗ — Microsoft (Playwright) · addInitScript runs after the document is created and before its scripts; the order of several init scripts is not defined; permissions include clipboard-read and clipboard-write and differ between browsers; a clock property exists on page and context in recent versions (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number; read at the time of writing. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Test user-visible behaviour, keep tests isolated, and prefer web-first assertions (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).
- Market practice
Clock (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
PRACTISE AND KEEP
Sources for this topic — 4 sources
- Market practice
Mock browser APIs (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
BrowserContext (Playwright documentation) ↗ — Microsoft (Playwright) · addInitScript runs after the document is created and before its scripts; the order of several init scripts is not defined; permissions include clipboard-read and clipboard-write and differ between browsers; a clock property exists on page and context in recent versions (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number; read at the time of writing. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Test user-visible behaviour, keep tests isolated, and prefer web-first assertions (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).
- Market practice
Clock (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.