lecture 4 of 210 completed

Your first test

The anatomy of a test: goto, act, expect. And what makes it a real test.

What you'll learn

  • The parts of a Playwright test file
  • How to name a test so a failure explains itself
  • Why a test without a check proves nothing

You click a button to turn on a lamp. The button works, but the lamp stays off. A test that stops after the click misses this bug.

Reading example. Reading Desk is a fictional app. You can read this example here. The exercise below uses a different app.

import { test, expect } from '@playwright/test';

test('turning on the lamp changes its status', async ({ page }) => {
  await page.goto('/');
  const button = page.getByRole('button', { name: 'Turn on lamp' });
  await button.click();
  await expect(page.getByRole('status')).toHaveText('Lamp on');
});
  1. import brings in test to name the test and expect to check a result.
  2. The test name says what should happen. Playwright gives the test a browser tab called page.
  3. page.goto('/') opens the app. The word await tells the test to wait.
  4. getByRole finds an element by its purpose. Here it finds the button named “Turn on lamp”.
  5. click() presses that button.
  6. The status element tells the reader whether the lamp is on. The last line checks its text. If it still says “Lamp off”, the test fails.

Your exercise. In Nimbus, submit the login form with both fields empty. Check that Email shows an error. Check that Password shows an error too.

These lessons assume you can write a simple loop. If you cannot, start with the JavaScript guide to loops. Come back when you can read each value in a list.

You can try the shop exercise for free. The subtotal forgets the quantity.

Continue with the free Playwright lesson Choose data that exposes a bug.

Remember this. After an action, check what the user should see.

Writing tests in Playwright

now prove it

Reading is the setup. Solve this problem in the editor. We break the app on purpose to check your test would catch it.