Skip to content

Feature request: no-floating-locators #395

@mikedidomizio

Description

@mikedidomizio

In Testing Library, querying (getBy*) will throw an error if no elements match. Preventing the continuation of a test.
In Playwright, locators although visually similar, behave differently, and will not fail written the same way as Testing Library. The test continues.

The differences may not be clear to some people, and can result in flakiness due to not understanding the differences between the two testing frameworks.

Some proper usages:

// variable assignment to be used later
const btn = page.getByRole('button', { name: 'Sign in' })
// a promise (user action) that is awaited
await page.getByRole('button', { name: 'Sign in' }).click()
// passing the result of the locator into an assertion
await expect(page.getByRole('button', { name: 'Sign in' })).toBeVisible()

Invalid usages:

page.getByRole('button', { name: 'Sign in' })

// continues when developer expected test to wait for 'Sign in' to be visible/attached/etc. similar to Testing Library.

My thinking is a rule (named similar to no-floating-promises) that requires the developer to use the locator in one of the following proper ways and can't "float". I can't think of a scenario where the invalid usage by itself makes sense (but let me know if I'm wrong).

  • I've looked through the existing rules of this plugin and I didn't see anything that covered this.
  • I've looked through Playwright options for something that might help and didn't find anything.
  • I've tried to think of other cases where the invalid usage is acceptable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions