fix(types): allow specifying element type in locator.evaluate callback (#11048)

This commit is contained in:
Yury Semikhatsky 2021-12-21 13:17:45 -08:00 committed by GitHub
parent f5304e3bda
commit 8f9eaf7cba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 8 deletions

View file

@ -8508,7 +8508,7 @@ export interface Locator {
* @param arg Optional argument to pass to `pageFunction`.
* @param options
*/
evaluate<R, Arg>(pageFunction: PageFunctionOn<SVGElement | HTMLElement, Arg, R>, arg: Arg, options?: {
evaluate<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: {
timeout?: number;
}): Promise<R>;
/**
@ -8530,7 +8530,7 @@ export interface Locator {
* @param arg Optional argument to pass to `pageFunction`.
* @param options
*/
evaluate<R>(pageFunction: PageFunctionOn<SVGElement | HTMLElement, void, R>, options?: {
evaluate<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, void, R>, options?: {
timeout?: number;
}): Promise<R>;
/**
@ -8551,7 +8551,7 @@ export interface Locator {
* @param pageFunction Function to be evaluated in the page context.
* @param arg Optional argument to pass to `pageFunction`.
*/
evaluateAll<R, Arg>(pageFunction: PageFunctionOn<(SVGElement | HTMLElement)[], Arg, R>, arg: Arg): Promise<R>;
evaluateAll<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;
/**
* The method finds all elements matching the specified locator and passes an array of matched elements as a first argument
* to `pageFunction`. Returns the result of `pageFunction` invocation.
@ -8570,7 +8570,7 @@ export interface Locator {
* @param pageFunction Function to be evaluated in the page context.
* @param arg Optional argument to pass to `pageFunction`.
*/
evaluateAll<R>(pageFunction: PageFunctionOn<(SVGElement | HTMLElement)[], void, R>): Promise<R>;
evaluateAll<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], void, R>): Promise<R>;
/**
* Resolves given locator to the first matching DOM element. If no elements matching the query are visible, waits for them
* up to a given timeout. If multiple elements match the selector, throws.

View file

@ -142,14 +142,14 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {
}
export interface Locator {
evaluate<R, Arg>(pageFunction: PageFunctionOn<SVGElement | HTMLElement, Arg, R>, arg: Arg, options?: {
evaluate<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: {
timeout?: number;
}): Promise<R>;
evaluate<R>(pageFunction: PageFunctionOn<SVGElement | HTMLElement, void, R>, options?: {
evaluate<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, void, R>, options?: {
timeout?: number;
}): Promise<R>;
evaluateAll<R, Arg>(pageFunction: PageFunctionOn<(SVGElement | HTMLElement)[], Arg, R>, arg: Arg): Promise<R>;
evaluateAll<R>(pageFunction: PageFunctionOn<(SVGElement | HTMLElement)[], void, R>): Promise<R>;
evaluateAll<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;
evaluateAll<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], void, R>): Promise<R>;
elementHandle(options?: {
timeout?: number;
}): Promise<null|ElementHandle<SVGElement | HTMLElement>>;

View file

@ -387,6 +387,36 @@ playwright.chromium.launch().then(async browser => {
await browser.close();
})();
// test locator.evaluate
(async () => {
const browser = await playwright.firefox.launch();
const page = await browser.newPage();
const locator = page.locator('.foo');
{
const result = await locator.evaluate((sel: HTMLSelectElement) => sel.options[sel.selectedIndex].textContent)
const assertion: AssertType<string, typeof result> = true;
}
{
const result = await locator.evaluate((media: HTMLMediaElement, dummy) => media.duration, 10);
const assertion: AssertType<number, typeof result> = true;
}
{
await locator.evaluate((input: HTMLInputElement) => {})
}
{
const list = await locator.evaluateAll((i: HTMLInputElement[]) => i.length);
const assertion: AssertType<number, typeof list> = true;
}
{
const list = await locator.evaluateAll((i: HTMLInputElement[], dummy) => i.length, 10);
const assertion: AssertType<number, typeof list> = true;
}
{
await locator.evaluateAll((sel: HTMLSelectElement[]) => {})
}
await browser.close();
})();
// waitForEvent
(async () => {
const browser = await playwright.webkit.launch();