@@ -4,13 +4,18 @@ import {inspect} from 'node:util';
44
55import { RETRY_KEY } from '../../constants/internal' ;
66import { getFrameContext } from '../../context/frameContext' ;
7+ import {
8+ type AttributesOptions ,
9+ createTestLocator ,
10+ type LocatorFunction ,
11+ type Stringifiable ,
12+ } from '../../createLocator' ;
713import { getPlaywrightPage } from '../../useContext' ;
814
915import { getAttributeCssSelector } from './getAttributeCssSelector' ;
1016
1117import type { Locator as PlaywrightLocator } from '@playwright/test' ;
1218
13- import type { AttributesOptions } from '../../createLocator' ;
1419import type { SelectorPropertyRetryData } from '../../types/internal' ;
1520
1621const setRetryData = (
@@ -35,7 +40,7 @@ type Options = Readonly<{
3540 kind ?: Kind ;
3641 parentSelector ?: Selector ;
3742} > &
38- Omit < AttributesOptions , 'testIdSeparator' > ;
43+ AttributesOptions ;
3944
4045/**
4146 * Selector.
@@ -49,28 +54,39 @@ class Selector {
4954
5055 private readonly kind : 'css' | 'filter' | 'find' | 'nth' | 'parent' | 'withText' ;
5156
57+ private readonly locator : LocatorFunction < string > ;
58+
5259 private readonly parameterAttributePrefix : string ;
5360
5461 private readonly parentSelector : Selector | undefined ;
5562
5663 private readonly testIdAttribute : string ;
5764
65+ private readonly testIdSeparator : string ;
66+
5867 protected constructor ( {
5968 args,
6069 cssString,
6170 kind = 'css' ,
6271 parameterAttributePrefix,
6372 parentSelector,
6473 testIdAttribute,
74+ testIdSeparator,
6575 } : Options ) {
6676 this . args = args ;
6777 this . cssString = cssString ;
6878 this . description =
6979 kind === 'css' ? cssString : `${ parentSelector ! . description } .${ kind } (${ args ?. join ( ', ' ) } )` ;
7080 this . kind = kind ;
81+ this . locator = createTestLocator ( {
82+ attributesOptions : { parameterAttributePrefix, testIdAttribute, testIdSeparator} ,
83+ createLocatorByCssSelector : ( selector ) => selector ,
84+ supportWildcardsInCssSelectors : true ,
85+ } ) . locator ;
7186 this . parameterAttributePrefix = parameterAttributePrefix ;
7287 this . parentSelector = parentSelector ;
7388 this . testIdAttribute = testIdAttribute ;
89+ this . testIdSeparator = testIdSeparator ;
7490 }
7591
7692 get boundingClientRect ( ) : Promise < DOMRectReadOnly > {
@@ -182,14 +198,15 @@ class Selector {
182198 cssString,
183199 parameterAttributePrefix,
184200 testIdAttribute,
185- } : Pick < Options , 'cssString' | 'parameterAttributePrefix' | 'testIdAttribute' > ) : Selector {
186- return new Selector ( { cssString, parameterAttributePrefix, testIdAttribute} ) ;
201+ testIdSeparator,
202+ } : Pick < Options , keyof AttributesOptions | 'cssString' > ) : Selector {
203+ return new Selector ( { cssString, parameterAttributePrefix, testIdAttribute, testIdSeparator} ) ;
187204 }
188205
189206 createSelector ( cssString : string ) : Selector {
190- const { parameterAttributePrefix, testIdAttribute} = this ;
207+ const { parameterAttributePrefix, testIdAttribute, testIdSeparator } = this ;
191208
192- return new Selector ( { cssString, parameterAttributePrefix, testIdAttribute} ) ;
209+ return new Selector ( { cssString, parameterAttributePrefix, testIdAttribute, testIdSeparator } ) ;
193210 }
194211
195212 filter ( cssSelectorString : string ) : Selector {
@@ -200,8 +217,8 @@ class Selector {
200217 return this . filter ( getAttributeCssSelector ( this . getParameterAttribute ( parameter ) , value ) ) ;
201218 }
202219
203- filterByTestId ( testId : string ) : Selector {
204- return this . filter ( getAttributeCssSelector ( this . testIdAttribute , testId ) ) ;
220+ filterByTestId ( ... args : readonly [ Stringifiable , ... Stringifiable [ ] ] ) : Selector {
221+ return this . filter ( this . locator ( ... ( args as [ Stringifiable ] ) ) ) ;
205222 }
206223
207224 find ( cssSelectorString : string ) : Selector {
@@ -212,8 +229,8 @@ class Selector {
212229 return this . find ( getAttributeCssSelector ( this . getParameterAttribute ( parameter ) , value ) ) ;
213230 }
214231
215- findByTestId ( testId : string ) : Selector {
216- return this . find ( getAttributeCssSelector ( this . testIdAttribute , testId ) ) ;
232+ findByTestId ( ... args : readonly [ Stringifiable , ... Stringifiable [ ] ] ) : Selector {
233+ return this . find ( this . locator ( ... ( args as [ Stringifiable ] ) ) ) ;
217234 }
218235
219236 getAttribute ( attributeName : string ) : Promise < string | null > {
@@ -316,7 +333,7 @@ class Selector {
316333 }
317334
318335 private createChildSelector ( kind : Kind , args ?: Args ) : Selector {
319- const { cssString, parameterAttributePrefix, testIdAttribute} = this ;
336+ const { cssString, parameterAttributePrefix, testIdAttribute, testIdSeparator } = this ;
320337
321338 return new Selector ( {
322339 args,
@@ -325,6 +342,7 @@ class Selector {
325342 parameterAttributePrefix,
326343 parentSelector : this ,
327344 testIdAttribute,
345+ testIdSeparator,
328346 } ) ;
329347 }
330348
0 commit comments