|

Day 18 of 100: Classes — The Blueprint for Page Objects

Day 18 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: JavaScript Fundamentals. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).

🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.


Classes structure Page Objects, helpers, and data factories. The syntax that organizes frameworks.

Contents

Class Anatomy

class ApiHelper {
  constructor(baseUrl) {
    this.baseUrl = baseUrl;
  }

  buildUrl(path) {
    return this.baseUrl + path;
  }

  static fromEnv() {                 // factory without an instance
    return new ApiHelper(process.env.API_URL || 'http://localhost');
  }
}

const api = new ApiHelper('https://api.test.com');
api.buildUrl('/users');     // https://api.test.com/users
ApiHelper.fromEnv();        // static call

🚀 Level Up Your Playwright

From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.

A Real Page Object Shape

class LoginPage {
  constructor(page) {
    this.page = page;
    // locators defined once, used everywhere
    // this.emailInput = page.getByLabel('Email');
  }

  async goto() { /* await this.page.goto('/login') */ }

  async login(email, password) {
    // await this.emailInput.fill(email); ...
    console.log(`login as ${email}`);
  }
}

Getters

class Config {
  get isCI() { return !!process.env.CI; }
}
new Config().isCI;  // property-style access, computed live

Tomorrow (Day 19): Inheritance and composition — and why Playwright prefers composition.

🎓 Master Playwright End to End

Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.