Skip to content
This repository was archived by the owner on Aug 20, 2024. It is now read-only.

Commit af26cd0

Browse files
authored
Support locators (#121)
* Add support for locators * Fix locators * Fix up types
1 parent a2b33ef commit af26cd0

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/matchers/toBeChecked/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ describe("toBeChecked", () => {
4646
})
4747
})
4848

49+
describe("locator", () => {
50+
it("positive", async () => {
51+
await page.setContent('<input type="radio" checked>')
52+
await expect(page.locator("input")).toBeChecked()
53+
})
54+
55+
it("positive", async () => {
56+
await page.setContent('<input type="radio">')
57+
await expect(page.locator("input")).not.toBeChecked()
58+
})
59+
})
60+
4961
describe("with 'not' usage", () => {
5062
it("positive", async () => {
5163
await page.setContent('<input type="checkbox">')

src/matchers/utils.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import type { Page, ElementHandle, Frame } from "playwright-core"
1+
import type { Page, ElementHandle, Frame, Locator } from "playwright-core"
22
import { PageWaitForSelectorOptions } from "../../global"
33

4-
type Handle = Page | Frame | ElementHandle
4+
type Handle = Page | Frame | ElementHandle | Locator
55
export type ExpectInputType = Handle | Promise<Handle>
66

77
const isElementHandle = (value: Handle): value is ElementHandle => {
88
return value.constructor.name === "ElementHandle"
99
}
1010

11+
const isLocator = (value: Handle): value is Locator => {
12+
return value.constructor.name === "Locator"
13+
}
14+
1115
export const getFrame = async (value: ExpectInputType) => {
1216
const resolved = await value
13-
return isElementHandle(resolved) ? resolved.contentFrame() : resolved
17+
18+
return isElementHandle(resolved)
19+
? resolved.contentFrame()
20+
: (resolved as Page | Frame)
1421
}
1522

1623
const isObject = (value: unknown) =>
@@ -37,22 +44,25 @@ export const getElementHandle = async (
3744
const expectedValue = args.splice(-valueArgCount, valueArgCount) as string[]
3845

3946
// 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
4249

50+
if (isLocator(handle)) {
51+
handle = (await handle.elementHandle())!
52+
}
4353
// If the user provided a page or iframe, we need to locate the provided
4454
// selector or the `body` element if none was provided.
45-
if (!isElementHandle(elementHandle)) {
55+
else if (!isElementHandle(handle)) {
4656
const selector = args[1] ?? "body"
4757

4858
try {
49-
elementHandle = (await elementHandle.waitForSelector(selector, options))!
59+
handle = (await handle.waitForSelector(selector, options))!
5060
} catch (err) {
5161
throw new Error(`Timeout exceed for element ${quote(selector)}`)
5262
}
5363
}
5464

55-
return [elementHandle, expectedValue] as const
65+
return [handle, expectedValue] as const
5666
}
5767

5868
export const quote = (val: string | null) => (val === null ? "" : `'${val}'`)

0 commit comments

Comments
 (0)