From bccffd21f8d2fea24be847c6906387529a8f0d12 Mon Sep 17 00:00:00 2001 From: Arjun Attam Date: Sun, 20 Oct 2024 17:50:02 +0530 Subject: [PATCH] feat: add device.pause() and device.waitForTimeout() methods (#183) * feat: add device.pause() method * wait for timeout * lint --- .changeset/green-turkeys-repeat.md | 5 ++++ src/device/index.ts | 42 +++++++++++++++++++----------- 2 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 .changeset/green-turkeys-repeat.md diff --git a/.changeset/green-turkeys-repeat.md b/.changeset/green-turkeys-repeat.md new file mode 100644 index 0000000..536c6a6 --- /dev/null +++ b/.changeset/green-turkeys-repeat.md @@ -0,0 +1,5 @@ +--- +"appwright": patch +--- + +feat: add device.pause() and device.waitForTimeout() methods diff --git a/src/device/index.ts b/src/device/index.ts index ac3d9be..da510b4 100644 --- a/src/device/index.ts +++ b/src/device/index.ts @@ -16,7 +16,7 @@ import { LLMModel } from "@empiricalrun/llm"; export class Device { constructor( - private webdriverClient: WebDriverClient, + private webDriverClient: WebDriverClient, private bundleId: string | undefined, private timeoutOpts: TimeoutOptions, private provider: string, @@ -32,7 +32,7 @@ export class Device { textToMatch?: string | RegExp; }): AppwrightLocator { return new Locator( - this.webdriverClient, + this.webDriverClient, this.timeoutOpts, selector, findStrategy, @@ -41,7 +41,7 @@ export class Device { } private vision(): AppwrightVision { - return new VisionProvider(this, this.webdriverClient); + return new VisionProvider(this, this.webDriverClient); } beta = { @@ -72,7 +72,7 @@ export class Device { // TODO: Add @boxedStep decorator here; disabled because it breaks persistentDevice // as test.step will throw async close() { - await this.webdriverClient.deleteSession(); + await this.webDriverClient.deleteSession(); } /** @@ -90,7 +90,7 @@ export class Device { @boxedStep async tap({ x, y }: { x: number; y: number }) { if (this.getPlatform() == Platform.ANDROID) { - await this.webdriverClient.executeScript("mobile: clickGesture", [ + await this.webDriverClient.executeScript("mobile: clickGesture", [ { x: x, y: y, @@ -99,7 +99,7 @@ export class Device { }, ]); } else { - await this.webdriverClient.executeScript("mobile: tap", [ + await this.webDriverClient.executeScript("mobile: tap", [ { x: x, y: y, @@ -225,7 +225,7 @@ export class Device { * @returns "android" or "ios" */ getPlatform(): Platform { - const isAndroid = this.webdriverClient.isAndroid; + const isAndroid = this.webDriverClient.isAndroid; return isAndroid ? Platform.ANDROID : Platform.IOS; } @@ -235,7 +235,7 @@ export class Device { } const keyName = this.getPlatform() == Platform.ANDROID ? "appId" : "bundleId"; - await this.webdriverClient.executeScript("mobile: terminateApp", [ + await this.webDriverClient.executeScript("mobile: terminateApp", [ { [keyName]: bundleId || this.bundleId, }, @@ -248,7 +248,7 @@ export class Device { } const keyName = this.getPlatform() == Platform.ANDROID ? "appId" : "bundleId"; - await this.webdriverClient.executeScript("mobile: activateApp", [ + await this.webDriverClient.executeScript("mobile: activateApp", [ { [keyName]: bundleId || this.bundleId, }, @@ -268,11 +268,11 @@ export class Device { */ async getClipboardText(): Promise { if (this.getPlatform() == Platform.ANDROID) { - return await this.webdriverClient.getClipboard(); + return await this.webDriverClient.getClipboard(); } else { if (this.provider == "emulator") { // iOS simulator supports clipboard sharing - return await this.webdriverClient.getClipboard(); + return await this.webDriverClient.getClipboard(); } else { if (!this.bundleId) { throw new Error( @@ -280,7 +280,7 @@ export class Device { ); } await this.activateApp("com.facebook.WebDriverAgentRunner.xctrunner"); - const clipboardDataBase64 = await this.webdriverClient.getClipboard(); + const clipboardDataBase64 = await this.webDriverClient.getClipboard(); await this.activateApp(this.bundleId); return clipboardDataBase64; } @@ -302,23 +302,35 @@ export class Device { async setMockCameraView(imagePath: string): Promise { if (this.provider == "browserstack") { const imageURL = await uploadImageToBS(imagePath); - await this.webdriverClient.executeScript( + await this.webDriverClient.executeScript( `browserstack_executor: {"action":"cameraImageInjection", "arguments": {"imageUrl" : "${imageURL}"}}`, [], ); } else if (this.provider == "lambdatest") { const imageURL = await uploadImageToLambdaTest(imagePath); - await this.webdriverClient.executeScript( + await this.webDriverClient.executeScript( `lambda-image-injection=${imageURL}`, [], ); } } + async pause() { + // eslint-disable-next-line no-constant-condition + while (true) { + await new Promise((resolve) => setTimeout(resolve, 20_000)); + await this.webDriverClient.takeScreenshot(); + } + } + + async waitForTimeout(timeout: number) { + await new Promise((resolve) => setTimeout(resolve, timeout)); + } + /** * Get a screenshot of the current screen as a base64 encoded string. */ async screenshot(): Promise { - return await this.webdriverClient.takeScreenshot(); + return await this.webDriverClient.takeScreenshot(); } }