Skip to contentPlayForge

What happens if I forget await in Playwright?

Written by Jatin Thacker, Automation Architect · Updated

If you forget await on a Playwright assertion or action, the test function completes immediately without waiting for the operation to settle. Playwright closes the browser context during teardown, causing the pending operation to fail with a session closed error.

This answer is one piece of Chapter 6: Async and Await.

  • Error Handling & Debugging
  • Callbacks & Promises
  • Async/Await Mastery
  • Practice and interview questions: Asynchronous JavaScript & Modern ES6+

Every locator action and web-first assertion in Playwright returns a Promise. When you omit await before an asynchronous call, JavaScript does not pause execution; it proceeds immediately to subsequent statements while the operation remains pending in the event loop.

When await is omitted on an assertion such as expect(locator).toHaveText(...), the test function reaches the end of its body and returns immediately. Playwright begins closing the browser page and context as part of test teardown. The in-flight assertion is abruptly interrupted, rejecting with a protocol session closed error that fails the test run.

Linters like eslint-plugin-playwright provide the missing-playwright-await rule to detect unawaited Playwright promises before code is executed. Always ensure every locator action and assertion is properly awaited.

missing-await.spec.js(https://practice.learnplayforge.com)
import { test, expect } from '@playwright/test'; test('omitting await on an assertion fails during session teardown', async ({ page }) => { await page.goto('/catalog.html?mock=static&seed=42&autologin=1'); // Deliberately omit await: the test function returns while assertion is pending expect(page.locator('#grid-heading')).toHaveText('WRONG NONEXISTENT TEXT'); });
Output
  1) missing-await.spec.js:3:1 › omitting await on an assertion fails during session teardown

    Error: expect(locator).toHaveText(expected) failed

    Locator:  locator('#grid-heading')
    Expected: "WRONG NONEXISTENT TEXT"
    Received: ""

    Call log:
      - Expect "toHaveText" locator('#grid-heading') with timeout 5000ms
      - waiting for locator('#grid-heading')
      - Protocol error (Runtime.evaluate): Internal server error, session closed.

  1 failed
Test yourself
A test ends with expect(locator).toHaveText('Welcome'), without await. What can happen?

Practised in the full course: the drill A string is not a Promise.

See the chapter

AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.

163 lessons · 226 drills · start free

← All questions

Training a team? Talk to us.