Skip to content

hasText option is available in getByRole(..., { hasText }) but no-op #10295

@hi-ogawa

Description

@hi-ogawa

Describe the bug

The following code fails

test("simple", async () => {
  document.body.innerHTML = `
<button>A</button>
<button>B</button>
`
  await page.getByRole('button', { hasText: 'A' }).click()
})

with the error:

 FAIL   chromium  basic.test.ts > simple
Error: locator.click: Error: strict mode violation: getByRole('button') resolved to 2 elements:
    1) <button>A</button> aka getByRole('button', { name: 'A' })
    2) <button>B</button> aka getByRole('button', { name: 'B' })

Call log:
  - waiting for locator('[data-vitest="true"]').contentFrame().getByRole('button')

 ❯ basic.test.ts:24:51
     25| <button>B</button>
     26| `
     27|   await page.getByRole('button', { hasText: 'A' }).click()
       |      ^

Apparently it's intended to use name option like getByRole(..., { name: "a" }) instead, but I was misled as it's available in type.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-dbiwucly?file=vitest-example%2FHelloWorld.test.ts

System Info

System:
    OS: Linux 7.0 Arch Linux
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-12650H
    Memory: 18.11 GB / 31.05 GB
    Container: Yes
    Shell: 5.3.9 - /usr/bin/bash
  Binaries:
    Node: 24.15.0 - /home/hiroshi/.vite-plus/js_runtime/node/24.15.0/bin/node
    Yarn: 1.22.22 - /home/hiroshi/.vite-plus/bin/yarn
    npm: 11.12.1 - /home/hiroshi/.vite-plus/js_runtime/node/24.15.0/bin/npm
    pnpm: 10.33.2 - /home/hiroshi/.vite-plus/bin/pnpm
    bun: 1.3.9 - /home/hiroshi/.bun/bin/bun
    Deno: 2.6.6 - /home/hiroshi/.local/bin/deno
  Browsers:
    Firefox: 150.0.1
    Firefox Developer Edition: 150.0.1
  npmPackages:
    @vitest/browser-playwright: ^4.1.5 => 4.1.5 
    @vitest/ui: latest => 4.1.5 
    vite: latest => 8.0.11 
    vitest: latest => 4.1.5

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions