diff --git a/packages/android/src/page/index.ts b/packages/android/src/page/index.ts index 6bc3d1f4e..ed1d7ecb1 100644 --- a/packages/android/src/page/index.ts +++ b/packages/android/src/page/index.ts @@ -3,14 +3,21 @@ import fs from 'node:fs'; import path from 'node:path'; import { type Point, type Size, getAIConfig } from '@midscene/core'; import type { PageType } from '@midscene/core'; -import { getTmpFile } from '@midscene/core/utils'; +import { getTmpFile, sleep } from '@midscene/core/utils'; import { MIDSCENE_ADB_PATH } from '@midscene/shared/env'; import type { ElementInfo } from '@midscene/shared/extractor'; import { isValidPNGImageBuffer, resizeImg } from '@midscene/shared/img'; import { getDebug } from '@midscene/shared/logger'; +import { repeat } from '@midscene/shared/utils'; import type { AndroidDevicePage } from '@midscene/web'; import { ADB } from 'appium-adb'; + const androidScreenshotPath = '/data/local/tmp/midscene_screenshot.png'; +// only for Android, because it's impossible to scroll to the bottom, so we need to set a default scroll times +const defaultScrollUntilTimes = 10; +const defaultFastScrollDuration = 100; +const defaultNormalScrollDuration = 1000; + export const debugPage = getDebug('android:device'); export class AndroidDevice implements AndroidDevicePage { @@ -397,7 +404,11 @@ ${Object.keys(size) await this.mouseDrag(start, end); return; } - await this.mouseWheel(0, 9999999, 100); + + await repeat(defaultScrollUntilTimes, () => + this.mouseWheel(0, 9999999, defaultFastScrollDuration), + ); + await sleep(1000); } async scrollUntilBottom(startPoint?: Point): Promise { @@ -408,7 +419,11 @@ ${Object.keys(size) await this.mouseDrag(start, end); return; } - await this.mouseWheel(0, -9999999, 100); + + await repeat(defaultScrollUntilTimes, () => + this.mouseWheel(0, -9999999, defaultFastScrollDuration), + ); + await sleep(1000); } async scrollUntilLeft(startPoint?: Point): Promise { @@ -418,7 +433,11 @@ ${Object.keys(size) await this.mouseDrag(start, end); return; } - await this.mouseWheel(9999999, 0, 100); + + await repeat(defaultScrollUntilTimes, () => + this.mouseWheel(9999999, 0, defaultFastScrollDuration), + ); + await sleep(1000); } async scrollUntilRight(startPoint?: Point): Promise { @@ -429,7 +448,11 @@ ${Object.keys(size) await this.mouseDrag(start, end); return; } - await this.mouseWheel(-9999999, 0, 100); + + await repeat(defaultScrollUntilTimes, () => + this.mouseWheel(-9999999, 0, defaultFastScrollDuration), + ); + await sleep(1000); } async scrollUp(distance?: number, startPoint?: Point): Promise { @@ -444,7 +467,7 @@ ${Object.keys(size) return; } - await this.mouseWheel(0, scrollDistance, 1000); + await this.mouseWheel(0, scrollDistance); } async scrollDown(distance?: number, startPoint?: Point): Promise { @@ -459,7 +482,7 @@ ${Object.keys(size) return; } - await this.mouseWheel(0, -scrollDistance, 1000); + await this.mouseWheel(0, -scrollDistance); } async scrollLeft(distance?: number, startPoint?: Point): Promise { @@ -474,7 +497,7 @@ ${Object.keys(size) return; } - await this.mouseWheel(scrollDistance, 0, 1000); + await this.mouseWheel(scrollDistance, 0); } async scrollRight(distance?: number, startPoint?: Point): Promise { @@ -489,7 +512,7 @@ ${Object.keys(size) return; } - await this.mouseWheel(-scrollDistance, 0, 1000); + await this.mouseWheel(-scrollDistance, 0); } private async ensureYadb() { @@ -595,7 +618,7 @@ ${Object.keys(size) private async mouseWheel( deltaX: number, deltaY: number, - duration = 1000, + duration = defaultNormalScrollDuration, ): Promise { const { width, height } = await this.size(); diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.ts index bfa2f2a5d..ca9b90fb3 100644 --- a/packages/shared/src/env.ts +++ b/packages/shared/src/env.ts @@ -192,7 +192,7 @@ export const getAIConfig = ( ); } - return getGlobalConfig()[configKey]?.trim(); + return getGlobalConfig()[configKey]?.trim?.(); }; export const getAIConfigInBoolean = ( diff --git a/packages/shared/src/utils.ts b/packages/shared/src/utils.ts index dd3a2dd0c..3b171e705 100644 --- a/packages/shared/src/utils.ts +++ b/packages/shared/src/utils.ts @@ -87,3 +87,12 @@ export function logMsg(...message: Parameters) { console.log(...message); } } + +export async function repeat( + times: number, + fn: (index: number) => Promise, +) { + for (let i = 0; i < times; i++) { + await fn(i); + } +}