Quiz: Your first Playwright tests
Why This Matters
A quick check that this module’s ideas hold up before you move on. Answer each question yourself before you open the answer.
1. Which locator is most robust when the design changes?
- A.
page.locator('.btn.btn-primary.mt-2') - B.
page.locator('//div[3]/button') - C.
page.getByRole('button', { name: 'Add to Cart' }) - D.
page.locator('button').nth(4)
Show answer
2. Why is expect(await locator.textContent()).toBe('2') a trap?
- A. It is slower than a web-first assertion
- B. It reads the text once and does not retry, so it fails if the page is not updated yet
- C.
textContentdoes not exist - D. It only works in Chromium
Show answer
await expect(locator).toHaveText('2') retries until the page shows the expected text or the timeout expires.3. You click page.getByRole("button", { name: "Add to Cart" }) on the shop page and get a strict mode violation. What is the best fix?
- A. Add
.first()so it always clicks the first product - B. Narrow it to the product you mean: find the card, then the button inside it
- C. Switch to an XPath locator
- D. Turn off strict mode in the config
Show answer
.first() (A) silently depends on the sort order.4. The login page answers after about 0.8 seconds. How should the test wait for the welcome message?
- A.
await page.waitForTimeout(1000) - B.
await page.waitForTimeout(5000), to be safe - C.
await expect(page.getByTestId("welcome-message")).toHaveText(...) - D. Retry the whole test until it passes
Show answer
5. Why does the example config set forbidOnly: !!process.env.CI?
- A. To run tests faster in CI
- B. To fail the CI run when a
test.onlywas committed by accident - C. To skip tests in CI
- D. To allow only one worker in CI
Show answer
test.only would quietly shrink the suite to one test while CI stays green. With forbidOnly, the run fails instead.6. All 12 browser tests pass in Chromium, Firefox and WebKit without changes. What made that likely?
- A. Playwright translates tests per browser
- B. Locators based on roles and test ids, and assertions that wait instead of fixed sleeps
- C. Running with one worker
- D. Retries in the config
Show answer
7. After sorting by price, cards.filter({ hasText: "Wireless" }) finds two product cards. Why, and what is the fix?
- A. A bug in Playwright; use
.first() - B.
hasTextmatches anywhere in the card, and the headphones’ description says “wireless”; filter on the heading withfilter({ has: page.getByRole("heading", { name: "Wireless Mouse" }) }) - C. The page shows the same product twice; reload the page
- D.
hasTextis case-sensitive; write “wireless”
Show answer
hasText is a case-insensitive substring anywhere inside the element. Filtering on the card’s heading says what you mean: the card whose title is Wireless Mouse. .first() would hide the problem and depend on the sort order.8. page.getByLabel("Category") finds nothing, although the word “Category” is right above the dropdown. What is going on?
- A. The dropdown is still loading
- B.
getByLabelis case-sensitive - C. The label is not connected to the
<select>(nofor, not wrapped, noaria-label), so the field has no accessible name - D. Dropdowns cannot be found by label