TOPIC 39/6018 MINNOT STARTED

Running JavaScript in the page: evaluate, handles and init scripts

Run a small piece of code inside the page to read or set something, know what a handle is, and keep clicks and checks on locators.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: whispering instructions to an actor on stage. A play is running. You cannot walk on stage, but you can whisper a note: tell me what is on the table. The actor looks and tells you. You never touch the table yourself.

Tom tests Daily List, a to-do list app. Normally he talks to the page with clicks and checks. Sometimes he needs a fact that no click shows, such as how far the task list has scrolled.

The picture has limits. An actor can refuse a note. A page runs the code it is given. Code run this way can also skip the waiting that clicks do, so it is easy to misuse.

What it is: Playwright is a free library from Microsoft that drives a real browser from code. This topic is one slice of that library: running a little JavaScript inside the page. JavaScript is the language pages run. An is page.evaluate. It sends a function to the page and brings a plain result back. A is a pointer to a live object that stays in the page, from page.evaluateHandle. An init script is code that runs before the page's own scripts, from page.addInitScript. The nearest neighbour is a locator click. Use the locator for actions and for checks. Use evaluate when the fact is not on the page as text.

Why it matters: Some states never appear as text. A scroll position, a saved theme, or a flag the app reads at start cannot be filled in with getByLabel. Without these calls, Tom cannot set dark mode before the first paint, and he cannot read a counter the page keeps in memory. With them, he can, and he can also hide a bug by forcing a flag the user never gets. He keeps the scripts small and says so in the test name.

(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)

PLAIN WORDS · 3 in this section
playwright
a free Microsoft tool that drives a browser for tests
init
short for initialise: set up for the first time
locator
a pointer telling you where to find something

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 Running a small piece of code inside the page to read or set something, without touching it from outside.

  1. Send
  2. Run in page
  3. Answer
  4. Check
Whisper, listen, keep clicks for locators

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
Whispering a note to the actorThe evaluate call
The actor's answerThe value brought back
A note read before the play startsThe init script
A line the actor can call out to youThe exposed function

WHERE THIS PICTURE STOPS

  • An actor may refuse a note, but a page runs any code it receives.
  • An actor sees the stage, but the page code cannot see the variables of the test.
  • A whisper skips the usual cues, and an evaluate call skips the usual waiting.

HOOK

Whisper to read, never to push.

REMEMBER IT THIS WAY

A rule of thumb: if you see this, think that.

Use evaluate when a click cannot show the fact, such as scroll position. Read, do not push, and add init scripts before the page loads.

What each part of the trick stands for
In the trickWhat it stands for
evaluatehands the page a small piece of JavaScript
init scripta note read before the play starts

An evaluate call skips the usual waiting, so the page may not be ready.

TELL A FRIEND

Evaluate whispers a note to the page and brings the answer back, and the is a note read before the play starts.

OFTEN MIXED UP WITH Browser developer tools. The earlier lesson shows how to look inside a page by hand with browser tools. This lesson sends small pieces of code into the page from a test.

Sources & standards — 5 sources
  1. Market practice

    Handles (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.

    Says a JSHandle points to any object in the page and an ElementHandle points to a page element; ElementHandle is discouraged except in rare cases, and locators are advised for actions and assertions; handles are released when the page navigates and a handle keeps its object alive until disposed. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-11-06

    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.

  2. Market practice

    Page (Playwright documentation) ↗ — Microsoft (Playwright) · page.evaluate runs a function in the page and gives back a value, evaluateHandle gives back a handle, addInitScript runs before the page scripts, and exposeFunction adds a function to window that survives navigations (search excerpt, page not opened)

    Covers page.routeFromHAR() with its update and notFound options, the popup event, page.waitForLoadState(), page.close() with runBeforeUnload, and page.bringToFront(). Vendor documentation for one tool. Practitioner or vendor guidance, not a standard. · Checked 2026-10-18

    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.

  3. Market practice

    Locator class reference (Playwright documentation) ↗ — Microsoft (Playwright) · A locator has its own evaluate and evaluateAll methods (search excerpt, page not opened)

    Reference for the Locator class: filter with hasText, hasNotText, has and hasNot (text matching is case-insensitive substring for a string), and, or, locator, nth, first, last, count and all. Says the inner locator of has is matched starting from the outer one. Says hasNot and hasNotText arrived in version 1.33 and and() in version 1.34. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-10-18

    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.

  4. Market practice

    Auto-waiting (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.

    Says Playwright runs actionability checks (visible, stable, enabled, receives events) before an action and fails with a timeout error if they do not pass in time. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-11-06

    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.

  5. 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.

    Describes replacing a browser API before the page loads. Vendor documentation, not a standard. · Checked 2026-11-06

    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 — 5 sources
  1. Market practice

    Handles (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.

    Says a JSHandle points to any object in the page and an ElementHandle points to a page element; ElementHandle is discouraged except in rare cases, and locators are advised for actions and assertions; handles are released when the page navigates and a handle keeps its object alive until disposed. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-11-06

    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.

  2. Market practice

    Page (Playwright documentation) ↗ — Microsoft (Playwright) · page.evaluate runs a function in the page and gives back a value, evaluateHandle gives back a handle, addInitScript runs before the page scripts, and exposeFunction adds a function to window that survives navigations (search excerpt, page not opened)

    Covers page.routeFromHAR() with its update and notFound options, the popup event, page.waitForLoadState(), page.close() with runBeforeUnload, and page.bringToFront(). Vendor documentation for one tool. Practitioner or vendor guidance, not a standard. · Checked 2026-10-18

    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.

  3. Market practice

    Locator class reference (Playwright documentation) ↗ — Microsoft (Playwright) · A locator has its own evaluate and evaluateAll methods (search excerpt, page not opened)

    Reference for the Locator class: filter with hasText, hasNotText, has and hasNot (text matching is case-insensitive substring for a string), and, or, locator, nth, first, last, count and all. Says the inner locator of has is matched starting from the outer one. Says hasNot and hasNotText arrived in version 1.33 and and() in version 1.34. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-10-18

    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.

  4. Market practice

    Auto-waiting (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.

    Says Playwright runs actionability checks (visible, stable, enabled, receives events) before an action and fails with a timeout error if they do not pass in time. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-11-06

    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.

  5. 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.

    Describes replacing a browser API before the page loads. Vendor documentation, not a standard. · Checked 2026-11-06

    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.