Skip to main content

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.

Quiz

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
C. A role plus accessible name follows what the user sees. Classes, positions and indexes change with every redesign.
Quiz

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. textContent does not exist
  • D. It only works in Chromium
Show answer
B. The value is read once. await expect(locator).toHaveText('2') retries until the page shows the expected text or the timeout expires.
Quiz

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
B. The locator matches every card’s button. Chaining from the right card states which product the test is about and keeps working when the product order changes. .first() (A) silently depends on the sort order.
Quiz

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
C. The web-first assertion waits exactly as long as needed, up to its timeout. A fixed sleep is either too short on a slow day or wastes time on every run.
Quiz

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.only was committed by accident
  • C. To skip tests in CI
  • D. To allow only one worker in CI
Show answer
B. A forgotten test.only would quietly shrink the suite to one test while CI stays green. With forbidOnly, the run fails instead.
Quiz

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
B. Nothing in the tests depends on one browser’s rendering or timing. Retries (D) are off locally, so they played no part in that run.
Quiz

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. hasText matches anywhere in the card, and the headphones’ description says “wireless”; filter on the heading with filter({ has: page.getByRole("heading", { name: "Wireless Mouse" }) })
  • C. The page shows the same product twice; reload the page
  • D. hasText is case-sensitive; write “wireless”
Show answer
B. 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.
Quiz

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. getByLabel is case-sensitive
  • C. The label is not connected to the <select> (no for, not wrapped, no aria-label), so the field has no accessible name
  • D. Dropdowns cannot be found by label
Show answer
C. A label only counts when it is connected to its field. Use the test id for now, and report the missing label: a screen reader announces the dropdown without a name too.