Skip to content

Commit 94e15dd

Browse files
committed
feat: add support for queryAll* methods
1 parent 8f35e6d commit 94e15dd

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

lib/index.ts

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {readFileSync} from 'fs'
22
import * as path from 'path'
3-
import {ElementHandle, EvaluateFn, Page} from 'puppeteer'
3+
import {ElementHandle, EvaluateFn, JSHandle, Page} from 'puppeteer'
44
import {ITestUtils} from './typedefs'
55

66
const domLibraryAsString = readFileSync(
@@ -25,13 +25,36 @@ type DOMReturnType = ElementHandle | ElementHandle[] | null
2525

2626
type ContextFn = (...args: any[]) => ElementHandle
2727

28+
async function createElementHandleArray(handle: JSHandle): Promise<ElementHandle[]> {
29+
const lengthHandle = await handle.getProperty('length')
30+
const length = await lengthHandle.jsonValue()
31+
32+
const elements: ElementHandle[] = []
33+
for (let i = 0; i < length; i++) {
34+
const jsElement = await handle.getProperty(i.toString())
35+
const element = await createElementHandle(jsElement)
36+
if (element) elements.push(element)
37+
}
38+
39+
return elements
40+
}
41+
42+
async function createElementHandle(handle: JSHandle): Promise<ElementHandle | null> {
43+
const element = handle.asElement()
44+
if (element) return element
45+
await handle.dispose()
46+
return null // tslint:disable-line
47+
}
48+
49+
async function covertToElementHandle(handle: JSHandle, asArray: boolean): Promise<DOMReturnType> {
50+
return asArray ? createElementHandleArray(handle) : createElementHandle(handle)
51+
}
52+
2853
function createDelegateFor(
2954
fnName: keyof ITestUtils,
3055
contextFn?: ContextFn,
3156
): (...args: any[]) => Promise<DOMReturnType> {
3257
return async function(...args: any[]): Promise<DOMReturnType> {
33-
if (fnName.includes('All')) throw new Error('*All methods not yet supported')
34-
3558
// @ts-ignore
3659
const containerHandle: ElementHandle = contextFn ? contextFn(...args) : this
3760
// @ts-ignore
@@ -45,16 +68,13 @@ function createDelegateFor(
4568
const handle = await containerHandle
4669
.executionContext()
4770
.evaluateHandle(evaluateFn, containerHandle, fnName, ...argsToForward)
48-
const element = handle.asElement()
49-
if (element) return element
50-
await handle.dispose()
51-
return null // tslint:disable-line
71+
return covertToElementHandle(handle, fnName.includes('All'))
5272
}
5373
}
5474

55-
export async function getDocument(context?: Page): Promise<ElementHandle> {
75+
export async function getDocument(_page?: Page): Promise<ElementHandle> {
5676
// @ts-ignore
57-
const page: Page = context || this
77+
const page: Page = _page || this
5878
const documentHandle = await page.mainFrame().evaluateHandle('document')
5979
const document = documentHandle.asElement()
6080
if (!document) throw new Error('Could not find document')

test/extend.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ describe('lib/extend.ts', () => {
4040
expect(await page.evaluate(el => el.outerHTML, element)).toMatchSnapshot()
4141
})
4242

43-
it.skip('should handle the queryAll* methods', async () => {
43+
it('should handle the queryAll* methods', async () => {
4444
const elements = await document.queryAllByText(/Hello/)
45-
expect(elements).toHaveLength(2)
45+
expect(elements).toHaveLength(3)
4646

4747
const text = await Promise.all([
4848
page.evaluate(el => el.textContent, elements[0]),
4949
page.evaluate(el => el.textContent, elements[1]),
50+
page.evaluate(el => el.textContent, elements[2]),
5051
])
5152

52-
expect(text).toEqual(['Hello h1', 'Hello h2'])
53+
expect(text).toEqual(['Hello h1', 'Hello h2', 'Hello h3'])
5354
})
5455

5556
it('should scope results to element', async () => {

0 commit comments

Comments
 (0)