fix(types): allow specifying element type in locator.evaluate callback (#11048)
This commit is contained in:
parent
f5304e3bda
commit
8f9eaf7cba
8
packages/playwright-core/types/types.d.ts
vendored
8
packages/playwright-core/types/types.d.ts
vendored
|
|
@ -8508,7 +8508,7 @@ export interface Locator {
|
||||||
* @param arg Optional argument to pass to `pageFunction`.
|
* @param arg Optional argument to pass to `pageFunction`.
|
||||||
* @param options
|
* @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;
|
timeout?: number;
|
||||||
}): Promise<R>;
|
}): Promise<R>;
|
||||||
/**
|
/**
|
||||||
|
|
@ -8530,7 +8530,7 @@ export interface Locator {
|
||||||
* @param arg Optional argument to pass to `pageFunction`.
|
* @param arg Optional argument to pass to `pageFunction`.
|
||||||
* @param options
|
* @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;
|
timeout?: number;
|
||||||
}): Promise<R>;
|
}): Promise<R>;
|
||||||
/**
|
/**
|
||||||
|
|
@ -8551,7 +8551,7 @@ export interface Locator {
|
||||||
* @param pageFunction Function to be evaluated in the page context.
|
* @param pageFunction Function to be evaluated in the page context.
|
||||||
* @param arg Optional argument to pass to `pageFunction`.
|
* @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
|
* 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.
|
* 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 pageFunction Function to be evaluated in the page context.
|
||||||
* @param arg Optional argument to pass to `pageFunction`.
|
* @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
|
* 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.
|
* up to a given timeout. If multiple elements match the selector, throws.
|
||||||
|
|
|
||||||
8
utils/generate_types/overrides.d.ts
vendored
8
utils/generate_types/overrides.d.ts
vendored
|
|
@ -142,14 +142,14 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Locator {
|
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;
|
timeout?: number;
|
||||||
}): Promise<R>;
|
}): 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;
|
timeout?: number;
|
||||||
}): Promise<R>;
|
}): Promise<R>;
|
||||||
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>;
|
||||||
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>;
|
||||||
elementHandle(options?: {
|
elementHandle(options?: {
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
}): Promise<null|ElementHandle<SVGElement | HTMLElement>>;
|
}): Promise<null|ElementHandle<SVGElement | HTMLElement>>;
|
||||||
|
|
|
||||||
|
|
@ -387,6 +387,36 @@ playwright.chromium.launch().then(async browser => {
|
||||||
await browser.close();
|
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
|
// waitForEvent
|
||||||
(async () => {
|
(async () => {
|
||||||
const browser = await playwright.webkit.launch();
|
const browser = await playwright.webkit.launch();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue