Skip to content

Commit 616df7d

Browse files
authored
fix(adb): minor fixes (#4678)
1 parent 495085c commit 616df7d

File tree

8 files changed

+39
-19
lines changed

8 files changed

+39
-19
lines changed

android-types-internal.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ export interface AndroidDevice<BrowserContextOptions, BrowserContext, Page> exte
2121

2222
setDefaultTimeout(timeout: number): void;
2323
on(event: 'webview', handler: (webView: AndroidWebView<Page>) => void): this;
24-
waitForEvent(event: string, predicate?: (data: any) => boolean): Promise<any>;
24+
waitForEvent(event: string, optionsOrPredicate?: (data: any) => boolean | { timeout?: number, predicate?: (data: any) => boolean }): Promise<any>;
2525

2626
serial(): string;
2727
model(): string;
2828
webViews(): AndroidWebView<Page>[];
29+
webView(selector: { pkg: string }): Promise<AndroidWebView<Page>>;
2930
shell(command: string): Promise<string>;
3031
launchBrowser(options?: BrowserContextOptions & { packageName?: string }): Promise<BrowserContext>;
3132
close(): Promise<void>;

packages/playwright-android/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ const { android } = require('playwright-android');
2121
await device.shell('am force-stop org.chromium.webview_shell');
2222
await device.shell('am start org.chromium.webview_shell/.WebViewBrowserActivity');
2323

24-
await device.fill({ res: 'org.chromium.webview_shell:id/url_field' }, 'github.com/microsoft/playwright');
25-
26-
let [webview] = device.webViews();
27-
if (!webview)
28-
webview = await device.waitForEvent('webview');
29-
24+
const webview = await device.webView({ pkg: 'org.chromium.webview_shell' });
3025
const page = await webview.page();
26+
27+
await device.fill({ res: 'org.chromium.webview_shell:id/url_field' }, 'github.com/microsoft/playwright');
3128
await Promise.all([
3229
page.waitForNavigation(),
3330
device.press({ res: 'org.chromium.webview_shell:id/url_field' }, 'Enter')

src/client/android.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class Android extends ChannelOwner<channels.AndroidChannel, channels.Andr
5353
}
5454
}
5555

56-
export class AndroidDevice extends ChannelOwner<channels.AndroidDeviceChannel, channels.AndroidDeviceInitializer> {
56+
export class AndroidDevice extends ChannelOwner<channels.AndroidDeviceChannel, channels.AndroidDeviceInitializer> implements apiInternal.AndroidDevice<types.BrowserContextOptions, BrowserContext, Page> {
5757
readonly _timeoutSettings: TimeoutSettings;
5858
private _webViews = new Map<number, AndroidWebView>();
5959

@@ -101,6 +101,16 @@ export class AndroidDevice extends ChannelOwner<channels.AndroidDeviceChannel, c
101101
return [...this._webViews.values()];
102102
}
103103

104+
async webView(selector: { pkg: string }, options?: types.TimeoutOptions): Promise<AndroidWebView> {
105+
const webView = [...this._webViews.values()].find(v => v.pkg() === selector.pkg);
106+
if (webView)
107+
return webView;
108+
return this.waitForEvent('webview', {
109+
...options,
110+
predicate: (view: AndroidWebView) => view.pkg() === selector.pkg
111+
});
112+
}
113+
104114
async wait(selector: apiInternal.AndroidSelector, options?: { state?: 'gone' } & types.TimeoutOptions) {
105115
await this._wrapApiCall('androidDevice.wait', async () => {
106116
await this._channel.wait({ selector: toSelectorChannel(selector), ...options });

src/server/android/android.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface Backend {
4040

4141
export interface DeviceBackend {
4242
serial: string;
43+
status: string;
4344
close(): Promise<void>;
4445
init(): Promise<void>;
4546
runCommand(command: string): Promise<string>;
@@ -65,7 +66,7 @@ export class Android {
6566
}
6667

6768
async devices(): Promise<AndroidDevice[]> {
68-
const devices = await this._backend.devices();
69+
const devices = (await this._backend.devices()).filter(d => d.status === 'device');
6970
return await Promise.all(devices.map(d => AndroidDevice.create(this, d)));
7071
}
7172
}
@@ -117,7 +118,9 @@ export class AndroidDevice extends EventEmitter {
117118
}
118119

119120
async shell(command: string): Promise<string> {
120-
return await this._backend.runCommand(`shell:${command}`);
121+
const result = await this._backend.runCommand(`shell:${command}`);
122+
await this._refreshWebViews();
123+
return result;
121124
}
122125

123126
private async _driver(): Promise<Transport> {
@@ -245,7 +248,7 @@ export class AndroidDevice extends EventEmitter {
245248
const browser = await CRBrowser.connect(androidBrowser, browserOptions);
246249
const controller = new ProgressController();
247250
await controller.run(async progress => {
248-
await browser._defaultContext!._loadDefaultContext(progress);
251+
await browser._defaultContext!._loadDefaultContextAsIs(progress);
249252
});
250253
return browser._defaultContext!;
251254
}
@@ -278,7 +281,7 @@ export class AndroidDevice extends EventEmitter {
278281
const p = match[1];
279282
if (+p !== pid)
280283
continue;
281-
pkg = proc.substring(proc.lastIndexOf(' '));
284+
pkg = proc.substring(proc.lastIndexOf(' ') + 1);
282285
}
283286
const webView = { pid, pkg };
284287
this._webViews.set(pid, webView);

src/server/android/backendAdb.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ export class AdbBackend implements Backend {
2424
async devices(): Promise<DeviceBackend[]> {
2525
const result = await runCommand('host:devices');
2626
const lines = result.toString().trim().split('\n');
27-
const serials = lines.map(line => line.split('\t')[0]);
28-
return serials.map(serial => new AdbDevice(serial));
27+
return lines.map(line => {
28+
const [serial, status] = line.trim().split('\t');
29+
return new AdbDevice(serial, status);
30+
});
2931
}
3032
}
3133

3234
class AdbDevice implements DeviceBackend {
3335
readonly serial: string;
36+
readonly status: string;
3437

35-
constructor(serial: string) {
38+
constructor(serial: string, status: string) {
3639
this.serial = serial;
40+
this.status = status;
3741
}
3842

3943
async init() {

src/server/browserContext.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,19 @@ export abstract class BrowserContext extends EventEmitter {
222222
this._timeoutSettings.setDefaultTimeout(timeout);
223223
}
224224

225-
async _loadDefaultContext(progress: Progress) {
225+
async _loadDefaultContextAsIs(progress: Progress): Promise<Page[]> {
226226
if (!this.pages().length) {
227227
const waitForEvent = helper.waitForEvent(progress, this, BrowserContext.Events.Page);
228228
progress.cleanupWhenAborted(() => waitForEvent.dispose);
229229
await waitForEvent.promise;
230230
}
231231
const pages = this.pages();
232232
await pages[0].mainFrame()._waitForLoadState(progress, 'load');
233+
return pages;
234+
}
235+
236+
async _loadDefaultContext(progress: Progress) {
237+
const pages = await this._loadDefaultContextAsIs(progress);
233238
if (pages.length !== 1 || pages[0].mainFrame().url() !== 'about:blank')
234239
throw new Error(`Arguments can not specify page to be opened (first url is ${pages[0].mainFrame().url()})`);
235240
if (this._options.isMobile || this._options.locale) {

utils/avd_install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ mkdir ${SDKDIR}/cmdline-tools
1010

1111
echo Downloading Android SDK...
1212
cd ${SDKDIR}/cmdline-tools
13-
curl https://dl.google.com/android/repository/commandlinetools-mac-6858069_latest.zip -o commandlinetools-mac-6858069_latest.zip
14-
unzip commandlinetools-mac-6858069_latest.zip
13+
curl https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip -o commandlinetools-linux-6858069_latest.zip
14+
unzip commandlinetools-linux-6858069_latest.zip
1515
mv cmdline-tools latest
1616

1717
echo Installing emulator...

utils/avd_start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export ANDROID_SDK_ROOT=${SDKDIR}
55
export ANDROID_HOME=${SDKDIR}
66
export ANDROID_AVD_HOME=${SDKDIR}/avd
77

8-
${SDKDIR}/emulator/emulator -avd android30
8+
${SDKDIR}/emulator/emulator -avd android30 -gpu guest

0 commit comments

Comments
 (0)