Testing / Playwright Interview questions
What is the difference between page.click() and locator().click()?
page.click(selector) is a shorthand that finds the element and clicks it in one step, but it doesn't retain any reference afterward - it's essentially fire-and-forget on a raw selector string.
// Shorthand, no persisted reference await page.click('#submit'); // Locator-based, recommended const submit = page.locator('#submit'); await submit.click();
Creating a Locator first and calling .click() on it does the same underlying click, but the locator object can be reused, chained with filters, and passed into assertions like expect(submit).toBeEnabled(). Because locators re-query the DOM at the moment of the action, they're the pattern Playwright's own documentation now recommends over the older page.click() shorthand for anything beyond a quick one-off script.
More Related questions...