From 62a4d82b7b999b66aede8a0f15dc6aebbe9dbbdf Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 5 Aug 2021 17:13:46 +0200 Subject: [PATCH] chore: cleanup locators implementation (#7990) --- src/client/locator.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/client/locator.ts b/src/client/locator.ts index 07af9237b3..5d81708c87 100644 --- a/src/client/locator.ts +++ b/src/client/locator.ts @@ -32,23 +32,21 @@ export class Locator implements api.Locator { this._selector = selector; } - private async _withElement(task: (handle: ElementHandle, options?: O) => Promise, options?: O): Promise { - if (!options) - options = {} as O; - const timeout = this._frame.page()._timeoutSettings.timeout(options!); + private async _withElement(task: (handle: ElementHandle, timeout?: number) => Promise, timeout?: number): Promise { + timeout = this._frame.page()._timeoutSettings.timeout({ timeout }); const deadline = timeout ? monotonicTime() + timeout : 0; const handle = await this.elementHandle({ timeout }); if (!handle) throw new Error(`Could not resolve ${this._selector} to DOM Element`); try { - return await task(handle, { ...options!, timeout: deadline ? deadline - monotonicTime() : 0 }); + return await task(handle, deadline ? deadline - monotonicTime() : 0); } finally { handle.dispose(); } } async boundingBox(options?: TimeoutOptions): Promise { - return this._withElement(h => h.boundingBox(), { strict: true, ...options }); + return this._withElement(h => h.boundingBox(), options?.timeout); } async check(options: channels.ElementHandleCheckOptions = {}) { @@ -68,7 +66,7 @@ export class Locator implements api.Locator { } async evaluate(pageFunction: structs.PageFunctionOn, arg?: Arg, options?: TimeoutOptions): Promise { - return this._withElement(h => h.evaluate(pageFunction, arg), { strict: true, ...options }); + return this._withElement(h => h.evaluate(pageFunction, arg), options?.timeout); } async evaluateAll(pageFunction: structs.PageFunctionOn, arg?: Arg): Promise { @@ -76,7 +74,7 @@ export class Locator implements api.Locator { } async evaluateHandle(pageFunction: structs.PageFunctionOn, arg?: Arg, options?: TimeoutOptions): Promise> { - return this._withElement(h => h.evaluateHandle(pageFunction, arg), { strict: true, ...options }); + return this._withElement(h => h.evaluateHandle(pageFunction, arg), options?.timeout); } async fill(value: string, options: channels.ElementHandleFillOptions = {}): Promise { @@ -164,11 +162,11 @@ export class Locator implements api.Locator { } async screenshot(options: channels.ElementHandleScreenshotOptions & { path?: string } = {}): Promise { - return this._withElement((h, o) => h.screenshot(o), { strict: true, ...options }); + return this._withElement((h, timeout) => h.screenshot({ ...options, timeout }), options.timeout); } async scrollIntoViewIfNeeded(options: channels.ElementHandleScrollIntoViewIfNeededOptions = {}) { - return this._withElement((h, o) => h.scrollIntoViewIfNeeded(o), { strict: true, ...options }); + return this._withElement((h, timeout) => h.scrollIntoViewIfNeeded({ ...options, timeout }), options.timeout); } async selectOption(values: string | api.ElementHandle | SelectOption | string[] | api.ElementHandle[] | SelectOption[] | null, options: SelectOptionOptions = {}): Promise { @@ -176,7 +174,7 @@ export class Locator implements api.Locator { } async selectText(options: channels.ElementHandleSelectTextOptions = {}): Promise { - return this._withElement((h, o) => h.selectText(o), { strict: true, ...options }); + return this._withElement((h, timeout) => h.selectText({ ...options, timeout }), options.timeout); } async setInputFiles(files: string | FilePayload | string[] | FilePayload[], options: channels.ElementHandleSetInputFilesOptions = {}) {