Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add waitForWebviewMs for mobile:getContexts #860

Merged
merged 11 commits into from
Dec 17, 2023
7 changes: 4 additions & 3 deletions lib/commands/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ const ContextMixin = {
this.curContext = name;
},

async mobileGetContexts() {
const opts = {
async mobileGetContexts(opts = {}) {
const _opts = {
androidDeviceSocket: this.opts.androidDeviceSocket,
ensureWebviewsHavePages: true,
webviewDevtoolsPort: this.opts.webviewDevtoolsPort,
enableWebviewDetailsCollection: true,
waitForWebviewMs: opts.waitForWebviewMs || 0,
};
return await WebviewHelpers.getWebViewsMapping(this.adb, opts);
return await WebviewHelpers.getWebViewsMapping(/** @type {ADB} */ (this.adb), _opts);
},

assignContexts(webviewsMapping) {
Expand Down
2 changes: 2 additions & 0 deletions lib/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface GetWebviewsOpts {
* @privateRemarks This is referenced but was not previously declared
*/
isChromeSession?: boolean;

waitForWebviewMs?: number | string;
}

export interface ProcessInfo {
Expand Down
28 changes: 22 additions & 6 deletions lib/helpers/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module
*/

import {util} from '@appium/support';
import {util, timing} from '@appium/support';
import {StringRecord} from '@appium/types';
import axios from 'axios';
import B from 'bluebird';
Expand All @@ -23,6 +23,7 @@ import type {
WebviewProps,
} from './types';
import type {ADB} from 'appium-adb';
import {sleep} from 'asyncbox';

const NATIVE_WIN = 'NATIVE_APP';
const WEBVIEW_WIN = 'WEBVIEW';
Expand Down Expand Up @@ -50,6 +51,7 @@ const DEVTOOLS_PORT_ALLOCATION_GUARD = util.getLockFileGuard(
path.resolve(os.tmpdir(), 'android_devtools_port_guard'),
{timeout: 7, tryRecovery: true},
);
const WEBVIEW_WAIT_INTERVAL_MS = 200;

interface WebviewHelpers {
/**
Expand Down Expand Up @@ -416,13 +418,27 @@ const WebviewHelpers: WebviewHelpers = {
ensureWebviewsHavePages = true,
webviewDevtoolsPort = null,
enableWebviewDetailsCollection = true,
waitForWebviewMs = 0,
}: GetWebviewsOpts = {},
): Promise<WebviewsMapping[]> {
logger.debug('Getting a list of available webviews');
const webviewsMapping = (await webviewsFromProcs(
adb,
androidDeviceSocket,
)) as WebviewsMapping[];
logger.debug(`Getting a list of available webviews`);

if (!_.isNumber(waitForWebviewMs)) {
waitForWebviewMs = parseInt(waitForWebviewMs, 10) || 0;
}

let webviewsMapping: WebviewsMapping[];
const timer = new timing.Timer().start();
do {
webviewsMapping = (await webviewsFromProcs(adb, androidDeviceSocket)) as WebviewsMapping[];

if (webviewsMapping.length > 0) {
break;
}

logger.debug(`No webviews found in ${timer.getDuration().asMilliSeconds.toFixed(0)}ms`);
await sleep(WEBVIEW_WAIT_INTERVAL_MS);
} while (timer.getDuration().asMilliSeconds < waitForWebviewMs);

await collectWebviewsDetails(adb, webviewsMapping, {
ensureWebviewsHavePages,
Expand Down
Loading