Playwright + JS · Lesson 11 of 35
Module 11: JavaScript for Test Automation
Try it yourself
Implement buildAuthHeader:
Bearer ${user.token} }.The editor has a worked example. Read it, tweak it, then press Mark as complete.
Never hardcode raw timeouts or base URLs inside test files:
// config/constants.js
export const TIMEOUTS = Object.freeze({
SHORT: 5000,
MEDIUM: 15000,
LONG: 30000,
});
export const ENVIRONMENTS = {
staging: {
baseUrl: "https://staging.targetqa.com",
apiUrl: "https://api.staging.targetqa.com",
defaultTimeout: TIMEOUTS.MEDIUM,
},
production: {
baseUrl: "https://targetqa.com",
apiUrl: "https://api.targetqa.com",
defaultTimeout: TIMEOUTS.LONG,
},
};export class StringFormatter {
static extractOrderId(text) {
const match = text.match(/Order #(\d+)/);
if (!match) throw new Error(`Order ID not found in text: ${text}`);
return match[1];
}
}Welcome to Phase 2! Here we connect core JavaScript directly to automation. In this lesson, we design structured test data objects, centralized configuration files, constants, and utility classes so your tests are clean, maintainable, and DRY. Let's get started!
Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
Object.freeze() prevents accidental modifications to constant objects (like modifying timeouts) at runtime.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.