Skip to content

Commit 2158d6d

Browse files
authored
feat(scopes): make page a scope (#4385)
1 parent 58b5872 commit 2158d6d

File tree

6 files changed

+117
-18
lines changed

6 files changed

+117
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ lib/
1616
jest-report.json
1717
drivers/
1818
/docs/api.json
19+
.android-sdk/

src/server/clank/android.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as debug from 'debug';
1818
import { EventEmitter } from 'events';
1919
import * as stream from 'stream';
2020
import * as ws from 'ws';
21-
import { makeWaitForNextTask } from '../../utils/utils';
21+
import { createGuid, makeWaitForNextTask } from '../../utils/utils';
2222

2323
export interface Backend {
2424
devices(): Promise<DeviceBackend[]>;
@@ -69,20 +69,13 @@ export class AndroidDevice {
6969
async launchBrowser(packageName: string): Promise<AndroidBrowser> {
7070
debug('pw:android')('Force-stopping', packageName);
7171
await this.backend.runCommand(`shell:am force-stop ${packageName}`);
72-
const hasDefaultSocket = !!(await this.backend.runCommand(`shell:cat /proc/net/unix | grep chrome_devtools_remote$`));
73-
debug('pw:android')('Starting', packageName);
72+
73+
const socketName = createGuid();
74+
const commandLine = `_ --disable-fre --no-default-browser-check --no-first-run --remote-debugging-socket-name=${socketName}`;
75+
debug('pw:android')('Starting', packageName, commandLine);
76+
await this.backend.runCommand(`shell:echo "${commandLine}" > /data/local/tmp/chrome-command-line`);
7477
await this.backend.runCommand(`shell:am start -n ${packageName}/com.google.android.apps.chrome.Main about:blank`);
75-
let pid = 0;
76-
debug('pw:android')('Polling pid for', packageName);
77-
while (!pid) {
78-
const ps = (await this.backend.runCommand(`shell:ps -A | grep ${packageName}`)).split('\n');
79-
const proc = ps.find(line => line.endsWith(packageName));
80-
if (proc)
81-
pid = +proc.replace(/\s+/g, ' ').split(' ')[1];
82-
await new Promise(f => setTimeout(f, 100));
83-
}
84-
debug('pw:android')('PID=' + pid);
85-
const socketName = hasDefaultSocket ? `chrome_devtools_remote_${pid}` : 'chrome_devtools_remote';
78+
8679
debug('pw:android')('Polling for socket', socketName);
8780
while (true) {
8881
const net = await this.backend.runCommand(`shell:cat /proc/net/unix | grep ${socketName}$`);
@@ -91,7 +84,7 @@ export class AndroidDevice {
9184
await new Promise(f => setTimeout(f, 100));
9285
}
9386
debug('pw:android')('Got the socket, connecting');
94-
const browser = new AndroidBrowser(this, packageName, socketName, pid);
87+
const browser = new AndroidBrowser(this, packageName, socketName);
9588
await browser._open();
9689
return browser;
9790
}
@@ -104,20 +97,18 @@ export class AndroidDevice {
10497
export class AndroidBrowser extends EventEmitter {
10598
readonly device: AndroidDevice;
10699
readonly socketName: string;
107-
readonly pid: number;
108100
private _socket: SocketBackend | undefined;
109101
private _receiver: stream.Writable;
110102
private _waitForNextTask = makeWaitForNextTask();
111103
onmessage?: (message: any) => void;
112104
onclose?: () => void;
113105
private _packageName: string;
114106

115-
constructor(device: AndroidDevice, packageName: string, socketName: string, pid: number) {
107+
constructor(device: AndroidDevice, packageName: string, socketName: string) {
116108
super();
117109
this._packageName = packageName;
118110
this.device = device;
119111
this.socketName = socketName;
120-
this.pid = pid;
121112
this._receiver = new (ws as any).Receiver() as stream.Writable;
122113
this._receiver.on('message', message => {
123114
this._waitForNextTask(() => {

utils/avd_install.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
SDKDIR=$PWD/.android-sdk
4+
export ANDROID_SDK_ROOT=${SDKDIR}
5+
export ANDROID_HOME=${SDKDIR}
6+
export ANDROID_AVD_HOME=${SDKDIR}/avd
7+
8+
mkdir ${SDKDIR}
9+
mkdir ${SDKDIR}/cmdline-tools
10+
11+
echo Downloading Android SDK...
12+
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
15+
mv cmdline-tools latest
16+
17+
echo Installing emulator...
18+
yes | ${SDKDIR}/cmdline-tools/latest/bin/sdkmanager platform-tools emulator
19+
20+
echo Installing system image...
21+
${SDKDIR}/cmdline-tools/latest/bin/sdkmanager "system-images;android-30;google_apis;x86"
22+
23+
echo Installing platform SDK...
24+
${SDKDIR}/cmdline-tools/latest/bin/sdkmanager "platforms;android-30"
25+
26+
echo Starting ADB...
27+
${SDKDIR}/platform-tools/adb devices

utils/avd_recreate.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
SDKDIR=$PWD/.android-sdk
4+
export ANDROID_SDK_ROOT=${SDKDIR}
5+
export ANDROID_HOME=${SDKDIR}
6+
export ANDROID_AVD_HOME=${SDKDIR}/avd
7+
8+
${SDKDIR}/cmdline-tools/latest/bin/avdmanager delete avd --name android30
9+
echo -ne '\n' | ${SDKDIR}/cmdline-tools/latest/bin/avdmanager create avd --name android30 --device pixel_4_xl --package "system-images;android-30;google_apis;x86"

utils/avd_start.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
SDKDIR=$PWD/.android-sdk
4+
export ANDROID_SDK_ROOT=${SDKDIR}
5+
export ANDROID_HOME=${SDKDIR}
6+
export ANDROID_AVD_HOME=${SDKDIR}/avd
7+
8+
${SDKDIR}/emulator/emulator -avd android30

utils/avd_test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const { _clank } = require('..');
18+
const assert = require('assert');
19+
const childProcess = require('child_process');
20+
const path = require('path');
21+
const readline = require('readline');
22+
23+
(async () => {
24+
setTimeout(() => {
25+
console.error('Timed out starting emulator');
26+
process.exit(1);
27+
}, 60000);
28+
const proc = childProcess.spawn(path.join(process.cwd(), '.android-sdk/emulator/emulator'), ['-no-window', '-avd', 'android30', '-verbose'], {
29+
env: {
30+
...process.env,
31+
ANDROID_SDK_ROOT: path.join(process.cwd(), '.android-sdk'),
32+
ANDROID_HOME: path.join(process.cwd(), '.android-sdk'),
33+
}
34+
});
35+
proc.stdout.on('data', data => console.log(data.toString()));
36+
proc.stderr.on('data', data => console.log(data.toString()));
37+
await waitForLine(proc, /boot completed/);
38+
39+
const context = await _clank.launchPersistentContext('');
40+
const [page] = context.pages();
41+
await page.goto('data:text/html,<title>Hello world</title>');
42+
assert(await page.title() === 'Hello world');
43+
await context.close();
44+
process.exit(0);
45+
})();
46+
47+
async function waitForLine(proc, regex) {
48+
return new Promise((resolve, reject) => {
49+
const rl = readline.createInterface({ input: proc.stdout });
50+
const failError = new Error('Process failed to launch!');
51+
rl.on('line', onLine);
52+
rl.on('close', reject.bind(null, failError));
53+
proc.on('exit', reject.bind(null, failError));
54+
proc.on('error', reject.bind(null, failError));
55+
56+
function onLine(line) {
57+
const match = line.match(regex);
58+
if (!match)
59+
return;
60+
resolve(match);
61+
}
62+
});
63+
}

0 commit comments

Comments
 (0)