Lesson 17 — Functions, Scope & Access: Inside the Page Object · Practical QA
LESSON VIEW
F-04 · BASIC · PRACTICAL QA

Lesson 17 — Functions, Scope & Access: Inside the Page Object

Objective: Functions, scope, and access — the mechanics inside every page object. LAB-0 taught you what a class IS; this module teaches the moving parts: what a method receives and returns, what this points at, and why some members are private. You will open QuotePage.ts in LAB-0's lab and finally read every line.

⏱ ≈6 min full lesson · Converted from the F-04 module deck · English edition — Español · العربية · বাংলা in translation

⚡ Key Points

  • Functions package a rule — A function is a named, reusable computation: parameters come in, a return value goes out.
  • One rule, called everywhere — RUN IT, then extend it: add a guard so beds outside 1–5 returns null (the form's dropdown only offers 1–5 — the function…
  • Scope contains, this points — Scope is where a name is visible. A variable declared inside a function exists only inside it — standard inside priceFor…
  • Private parts, public actions — READ this page object closely — it is QuotePage in miniature.
  • Access is a promise-keeping device — Why private at all? Because a locator is an implementation detail: the day the form's markup changes, ONE line in the co…
  • The answer — It never compiles. The error appears while writing, not while running — the cheapest moment for any bug.

📖 Read

In ~6 minutes: functions, scope, and access — the mechanics inside every page object. LAB-0 taught you what a class IS; this module teaches the moving parts: what a method receives and returns, what this points at, and why some members are private. You will open QuotePage.ts in LAB-0's lab and finally read every line.

Functions package a rule: A function is a named, reusable computation: parameters come in, a return value goes out. priceFor(service, beds) from F-02 is one — the pricing rule exists ONCE, and every test calls it instead of re-deriving prices inline. A method is a function living on a class, called on an object: quote.choose(...) — same idea, with the object's facts (this) in reach.

One rule, called everywhere: RUN IT, then extend it: add a guard so beds outside 1–5 returns null (the form's dropdown only offers 1–5 — the function should refuse what the page refuses). Notice what the return type number | null is doing: it forces every CALLER to handle the no-price case. Types are how a function makes its contract un-ignorable. Code: function priceFor(service: string, beds: number): number | null { | const standard = 140 + (beds - 2) * 30; | if (service === 'standard') return standard; | if (service === 'deep') return standard * 1.5; | return null; | } | const expected = priceFor('deep', 2); | console.log('expect $' + expected);

Scope contains, this points: Scope is where a name is visible. A variable declared inside a function exists only inside it — standard inside priceFor cannot be read from the test, and that containment is good: tests shouldn't reach into the oracle's pockets. this, inside a method, points at the object the method was called on: this.page is THAT QuotePage's browser page. Same method, two page objects, two different this — that is how one class drives many pages.

Private parts, public actions: READ this page object closely — it is QuotePage in miniature. The locators are private: only the class's own methods may touch them. The actions are public: tests call choose() and submit(), never the raw elements. estimate is readonly + private: even the class never reassigns it after the constructor. Then the RUN-EDIT step: add a public method confirmationText() that returns the confirmation banner's text — using the private locator from inside, the only legal way. Code: export class QuotePage { | private readonly estimate: Locator; | private readonly submitButton: Locator; | constructor(private readonly page: Page) { | this.estimate = page.locator('#estimate-output'); | this.submitButton = page.getByRole('button', { name: 'Request booking' }); | } | async submit() { | await this.submitButton.click(); | } | async estimateText() { | return this.estimate.textContent(); | } | }

Access is a promise-keeping device: Why private at all? Because a locator is an implementation detail: the day the form's markup changes, ONE line in the constructor changes and every test survives. If tests touched locators directly, the same change would break forty files. public is the class's promise (actions and answers); private is its workshop (elements and helpers); readonly is a fact fixed at construction. Access modifiers are how a page object keeps its promises cheap to keep.

The answer: It never compiles. The error appears while writing, not while running — the cheapest moment for any bug. This is the same protection as const in F-01, one level up: the language itself guards the framework's shape. When LAB-4 talks about fixtures and state, this is the machinery underneath.

async/await: a reading skill: async/await, at reading level — you will write these from LAB-1 on, so learn to READ them now: async marks a method that does slow things (clicks, loads); await pauses THAT method until the slow thing finishes, so lines run in the order written. A missing await is the classic flake: the assertion runs before the click lands. You don't need more than this reading skill until the labs make you write it daily.

Complete the class: COMPLETE the class: make the phone locator private (it is currently public — any test can grab it) and keep the public action that uses it working.

The answer: The cost: change stops being local. One markup edit should cost one line; public-everything makes it cost the whole suite. You have finished this module — and the F-track — when: you can read QuotePage.ts line by line and explain every keyword in it: export, class, private, readonly, constructor, this, async, await, return.

Read QuotePage.ts aloud, keyword by keyword, without notes; then add one public action that uses a private locator, and predict one privacy refusal before compiling.