async, await and promises
Why This Matters
Ask experienced Playwright users for the most common cause of a flaky test written by a beginner, and many will say the same thing: a missing await. The test does not crash. It just sometimes runs the next step before the previous one has finished, and passes or fails depending on how fast the machine, the network and the site happen to be that day.
A promise is a receipt
Everything a browser does takes time: loading a page, clicking a button, waiting for a response. Playwright’s methods therefore do not return the result straight away. They return a promise: a receipt that says “the result will be here later”.
const loading = page.goto("/shopping"); // a promise: the page is still loading
await loading; // wait until it is done
await means: stop here until the promise is settled, then continue. You can only use await inside a function marked async, which is why every browser test starts like this:
test("adding two products updates the cart button", async ({ page }) => {
await page.goto("/shopping");
// ...
});
The rule: await every action and every assertion
In Playwright almost everything returns a promise:
await page.goto("/shopping"); // navigation
await page.getByRole("button", { name: "Add to Cart" }).first().click(); // action
await expect(page.getByTestId("cart-button")).toContainText("1 items"); // assertion
The one thing you do not await is creating a locator. page.getByRole(...) only describes how to find an element; nothing happens until you act on it or assert on it.
What a missing await does
test("floating", async ({ page }) => {
page.goto("/shopping"); // no await
await expect(page.getByTestId("cart-button")).toBeVisible();
});
The test starts the navigation and immediately moves on. Here the assertion happens to wait long enough (lesson 9 explains why), so the test often passes. Change the next line to something that does not wait, or let the test end straight after the click, and the result depends on timing. Worse, if the promise fails after the test has ended, the error can show up in a different test.
Let a tool find it for you
You will forget an await one day; everybody does. The example repository therefore runs ESLint with the rule @typescript-eslint/no-floating-promises. On the test above it reports:
8:3 error Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator @typescript-eslint/no-floating-promises
✖ 1 problem (1 error, 0 warnings)
This is the configuration, in eslint.config.mjs:
rules: {
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/await-thenable": "error",
},
Run it with npm run lint. The repository’s CI workflow runs it on every push, so a missing await never reaches main.
Exercise
- In
tests/module-2/shop.spec.ts, remove theawaitin front of one of theclick()calls in adding two products updates the cart button. - Run
npm run lintand read the error. - Run the test twenty times:
npx playwright test shop.spec.ts --project=chromium --repeat-each=20. Note what happens, then put theawaitback.
Show suggested answer
no-floating-promises on the line you changed, before any browser starts. The repeated run may well stay green: the next assertion, toContainText("2 items"), keeps retrying until the cart is updated, which hides the missing await most of the time. That is exactly what makes this bug dangerous. It passes today and fails on a slower CI machine next month, so do not rely on the run; rely on the linter.