Skip to content

Commit 9b4df4b

Browse files
author
Arjun
authored
fix(browser): disallow filter-only options in getBy* locators (fix #10295) (#10933)
1 parent bac009d commit 9b4df4b

5 files changed

Lines changed: 20 additions & 7 deletions

File tree

docs/api/browser/locators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ page.getByRole('button')
515515
## filter
516516

517517
```ts
518-
function filter(options: LocatorOptions): Locator
518+
function filter(options: LocatorFilterOptions): Locator
519519
```
520520

521521
This methods narrows down the locator according to the options, such as filtering by text. It can be chained to apply multiple filters.

packages/browser/context.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ export interface LocatorOptions {
447447
* regular expression. Note that exact match still trims whitespace.
448448
*/
449449
exact?: boolean
450+
}
451+
452+
export interface LocatorFilterOptions {
450453
hasText?: string | RegExp
451454
hasNotText?: string | RegExp
452455
has?: Locator
@@ -766,7 +769,7 @@ export interface Locator extends LocatorSelectors {
766769
* Narrows existing locator according to the options.
767770
* @see {@link https://vitest.dev/api/browser/locators#filter}
768771
*/
769-
filter(options: LocatorOptions): Locator
772+
filter(options: LocatorFilterOptions): Locator
770773
/**
771774
* This method returns an element matching the locator.
772775
* Unlike [`.element()`](https://vitest.dev/api/browser/locators#element),

packages/browser/src/client/tester/locators.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ParsedSelector } from 'ivya'
22
import type {
33
LocatorByRoleOptions,
4+
LocatorFilterOptions,
45
LocatorOptions,
56
LocatorScreenshotOptions,
67
MarkOptions,
@@ -273,7 +274,7 @@ export abstract class Locator {
273274
return this.locator(getByTitleSelector(title, options))
274275
}
275276

276-
public filter(filter: LocatorOptions): Locator {
277+
public filter(filter: LocatorFilterOptions): Locator {
277278
const selectors = []
278279

279280
if (filter?.hasText) {

packages/ui/client/components/artifacts/visual-regression/VisualRegressionSlider.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ describe('VisualRegressionSlider', () => {
7676

7777
const container = page.getByLabelText(containerLabel)
7878
const input = page.getByLabelText(inputLabel)
79-
const status = page.getByRole(
80-
'status',
81-
{ hasText: 'Showing 50% reference, 50% actual' },
82-
)
79+
const status = page.getByRole('status')
80+
.filter({ hasText: 'Showing 50% reference, 50% actual' })
8381

8482
// container with accessible label should exist and contain input and status
8583
await expect.element(container).toBeInTheDocument()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { test } from 'vitest'
2+
import { page } from 'vitest/browser'
3+
4+
test('filter options are not accepted by getBy* methods', () => {
5+
// @ts-expect-error -- `hasText` is only supported by `.filter()`
6+
page.getByRole('button', { hasText: 'A' })
7+
// @ts-expect-error -- `hasText` is only supported by `.filter()`
8+
page.getByText('hello', { hasText: 'A' })
9+
10+
page.getByRole('button').filter({ hasText: 'A' })
11+
})

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo