Skip to content
CodeKruPro

Your First Playwright Script

1/70

Your First Playwright Script

Welcome to Playwright! In this first assignment, you will:

  1. Launch a Chromium browser with chromium.launch()
  2. Create a new page with browser.newPage()
  3. Navigate to a URL with page.goto()
  4. Get the page title with page.title()
  5. Close the browser with browser.close()

In Playwright, everything is async/await. Every browser interaction returns a Promise that you await.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Print the page title

Expected output:

Title: Text Fields

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

  1. Navigate with page.goto() and the Text Fields address, with await in front.
  2. Get the title with page.title(). It returns a Promise, so await it too.
  3. Log one line with console.log: the label Title: and the title.
  4. Leave browser.close() at the end so the script exits.

Common mistakes

  • Forgetting await

    Without await, page.title() gives you a Promise, and console.log prints Promise { <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 a finally block when errors are possible.

  • Using await at the top level of a CommonJS file

    A require() script cannot use await outside a function, which is why the starter code wraps everything in an async arrow 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.

main.js
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();
})();