1
- import type { Page , ElementHandle , Frame } from "playwright-core"
1
+ import type { Page , ElementHandle , Frame , Locator } from "playwright-core"
2
2
import { PageWaitForSelectorOptions } from "../../global"
3
3
4
- type Handle = Page | Frame | ElementHandle
4
+ type Handle = Page | Frame | ElementHandle | Locator
5
5
export type ExpectInputType = Handle | Promise < Handle >
6
6
7
7
const isElementHandle = ( value : Handle ) : value is ElementHandle => {
8
8
return value . constructor . name === "ElementHandle"
9
9
}
10
10
11
+ const isLocator = ( value : Handle ) : value is Locator => {
12
+ return value . constructor . name === "Locator"
13
+ }
14
+
11
15
export const getFrame = async ( value : ExpectInputType ) => {
12
16
const resolved = await value
13
- return isElementHandle ( resolved ) ? resolved . contentFrame ( ) : resolved
17
+
18
+ return isElementHandle ( resolved )
19
+ ? resolved . contentFrame ( )
20
+ : ( resolved as Page | Frame )
14
21
}
15
22
16
23
const isObject = ( value : unknown ) =>
@@ -37,22 +44,25 @@ export const getElementHandle = async (
37
44
const expectedValue = args . splice ( - valueArgCount , valueArgCount ) as string [ ]
38
45
39
46
// Finally, we can find the element handle
40
- const handle = await args [ 0 ]
41
- let elementHandle = ( await getFrame ( handle ) ) ?? handle
47
+ let handle = await args [ 0 ]
48
+ handle = ( await getFrame ( handle ) ) ?? handle
42
49
50
+ if ( isLocator ( handle ) ) {
51
+ handle = ( await handle . elementHandle ( ) ) !
52
+ }
43
53
// If the user provided a page or iframe, we need to locate the provided
44
54
// selector or the `body` element if none was provided.
45
- if ( ! isElementHandle ( elementHandle ) ) {
55
+ else if ( ! isElementHandle ( handle ) ) {
46
56
const selector = args [ 1 ] ?? "body"
47
57
48
58
try {
49
- elementHandle = ( await elementHandle . waitForSelector ( selector , options ) ) !
59
+ handle = ( await handle . waitForSelector ( selector , options ) ) !
50
60
} catch ( err ) {
51
61
throw new Error ( `Timeout exceed for element ${ quote ( selector ) } ` )
52
62
}
53
63
}
54
64
55
- return [ elementHandle , expectedValue ] as const
65
+ return [ handle , expectedValue ] as const
56
66
}
57
67
58
68
export const quote = ( val : string | null ) => ( val === null ? "" : `'${ val } '` )
0 commit comments