Playwright + TS · Lesson 2 of 36
Module 2: Functions
Your task
Complete the createTestUser function:
username and role from input."User: ${username} | Role: ${role}".Write it in the editor, then press Check answer (Ctrl + Enter).
In this lesson
In Playwright test scripts, you will write arrow functions everywhere:
// Arrow function:
const buildUrl = (path: string): string => {
return `https://targetqa.com${path}`;
};${value})Use backticks (`) instead of string concatenation (+`) to interpolate dynamic test data:
const userId = 104;
const locator = `//div[@data-user-id='${userId}']`;Playwright passes test fixtures using object destructuring:
// Destructuring object properties:
const user = { username: "admin", role: "lead", timeout: 5000 };
const { username, role } = user;
// Array destructuring:
const [firstItem, secondItem] = ["Chromium", "Firefox"];...) & Rest (...rest) Operators// Spread: copies or merges objects/arrays
const baseConfig = { headless: true, timeout: 30000 };
const testConfig = { ...baseConfig, browser: "firefox" };
// Rest: collects remaining arguments into an array
function logSteps(testName: string, ...steps: string[]) {
console.log(`Running ${testName} with ${steps.length} steps`);
}Functions are the building blocks of clean test automation. In this lesson, we will explore modern JavaScript functions, including arrow functions, callbacks, template literals, and destructuring. You will see how these features make your test code concise, reusable, and easy to read. Let's dive in!
Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
That is object destructuring! Playwright's test runner passes an object containing all fixtures ({ page, context, browser, request }), and destructuring extracts only the ones you need.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___