About this lesson
Launching a browser and opening a page in Playwright JavaScript
Learn Playwright JSBeginnerGetting Started
How it works
A Playwright script works with three objects. chromium.launch() starts the browser, browser.newPage() opens a tab inside a fresh browser context, and the page object is what you drive. Playwright downloads its own Chromium, Firefox and WebKit builds, so there is no separate driver binary to install or keep in step with the browser version.
Almost every call talks to the browser over a connection and returns a Promise, which is why the script runs inside an async function and puts await in front of each call. The browser launches headless by default; pass { headless: false } to watch it.
About the assignment
The starter launches Chromium and opens a blank page. You point that page at Text Fields and log its title, which makes the smallest complete Playwright script: launch, navigate, read, close.
What you will do
- Navigate with
page.goto()and the Text Fields address, withawaitin front. - Get the title with
page.title(). It returns a Promise, so await it too. - Log one line with
console.log: the labelTitle:and the title. - Leave
browser.close()at the end so the script exits.
Common mistakes
Forgetting await
Without
await,page.title()gives you a Promise, andconsole.logprintsPromise { <pending> }instead of the title.Leaving the browser open
If
browser.close()never runs, Node keeps the process alive and the script appears to hang. Close it in afinallyblock when errors are possible.Using await at the top level of a CommonJS file
A
require()script cannot useawaitoutside a function, which is why the starter code wraps everything in anasyncarrow function that calls itself.
In a real test suite
Inside the @playwright/test runner you seldom launch a browser yourself. Each test() receives a ready page fixture with its own isolated context, and the runner closes everything when the test ends.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
// TODO: Navigate to https://testkru.com/Elements/TextFields
// TODO: Print the page title in format: "Title: <title>"
await browser.close();
})();