What happens if I forget await in Playwright?
Written by Jatin Thacker, Automation Architect · Updated
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.
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');
}); 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 failedexpect(locator).toHaveText('Welcome'), without await. What can happen?Practised in the full course: the drill A string is not a Promise.
See the chapterRelated questions
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