Skip to content

Commit 1596b53

Browse files
authored
test(adb): fix browser tests (#4700)
1 parent f89dcc7 commit 1596b53

File tree

5 files changed

+35
-41
lines changed

5 files changed

+35
-41
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ jobs:
232232
name: video-${{ matrix.browser }}-linux-test-results
233233
path: test-results
234234
test_android:
235-
name: Android on macOS
235+
name: Android Emulator
236236
runs-on: macos-10.15
237237
steps:
238238
- uses: actions/checkout@v2
@@ -250,6 +250,7 @@ jobs:
250250
env:
251251
FOLIO_JSON_OUTPUT_NAME: "test-results/report.json"
252252
PW_ANDROID_TESTS: 1
253+
DEBUG: pw:api
253254
- run: ./utils/upload_flakiness_dashboard.sh ./test-results/report.json
254255
if: always() && github.ref == 'refs/heads/master'
255256
- uses: actions/upload-artifact@v1

src/server/android/android.ts

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class AndroidDevice extends EventEmitter {
145145
}
146146

147147
async open(command: string): Promise<SocketBackend> {
148-
return await this._backend.open(`shell:${command}`);
148+
return await this._backend.open(`${command}`);
149149
}
150150

151151
private async _driver(): Promise<Transport> {
@@ -167,18 +167,7 @@ export class AndroidDevice extends EventEmitter {
167167

168168
debug('pw:android')('Starting the new driver');
169169
this.shell(`am instrument -w com.microsoft.playwright.androiddriver.test/androidx.test.runner.AndroidJUnitRunner`);
170-
171-
debug('pw:android')('Polling the socket');
172-
let socket;
173-
while (!socket) {
174-
try {
175-
socket = await this._backend.open(`localabstract:playwright_android_driver_socket`);
176-
} catch (e) {
177-
await new Promise(f => setTimeout(f, 100));
178-
}
179-
}
180-
181-
debug('pw:android')('Connected to driver');
170+
const socket = await this._waitForLocalAbstract('playwright_android_driver_socket');
182171
const transport = new Transport(socket, socket, socket, 'be');
183172
transport.onmessage = message => {
184173
const response = JSON.parse(message);
@@ -197,6 +186,20 @@ export class AndroidDevice extends EventEmitter {
197186
return this._driverPromise;
198187
}
199188

189+
private async _waitForLocalAbstract(socketName: string): Promise<SocketBackend> {
190+
let socket: SocketBackend | undefined;
191+
debug('pw:android')(`Polling the socket localabstract:${socketName}`);
192+
while (!socket) {
193+
try {
194+
socket = await this._backend.open(`localabstract:${socketName}`);
195+
} catch (e) {
196+
await new Promise(f => setTimeout(f, 250));
197+
}
198+
}
199+
debug('pw:android')(`Connected to localabstract:${socketName}`);
200+
return socket;
201+
}
202+
200203
async send(method: string, params: any): Promise<any> {
201204
const driver = await this._driver();
202205
const id = ++this._lastId;
@@ -224,20 +227,11 @@ export class AndroidDevice extends EventEmitter {
224227
debug('pw:android')('Force-stopping', pkg);
225228
await this._backend.runCommand(`shell:am force-stop ${pkg}`);
226229

227-
const socketName = createGuid();
230+
const socketName = 'playwright-' + createGuid();
228231
const commandLine = `_ --disable-fre --no-default-browser-check --no-first-run --remote-debugging-socket-name=${socketName}`;
229232
debug('pw:android')('Starting', pkg, commandLine);
230233
await this._backend.runCommand(`shell:echo "${commandLine}" > /data/local/tmp/chrome-command-line`);
231234
await this._backend.runCommand(`shell:am start -n ${pkg}/com.google.android.apps.chrome.Main about:blank`);
232-
233-
debug('pw:android')('Polling for socket', socketName);
234-
while (true) {
235-
const net = await this._backend.runCommand(`shell:cat /proc/net/unix | grep ${socketName}$`);
236-
if (net)
237-
break;
238-
await new Promise(f => setTimeout(f, 100));
239-
}
240-
debug('pw:android')('Got the socket, connecting');
241235
return await this._connectToBrowser(socketName, options);
242236
}
243237

@@ -249,8 +243,9 @@ export class AndroidDevice extends EventEmitter {
249243
}
250244

251245
private async _connectToBrowser(socketName: string, options: types.BrowserContextOptions = {}): Promise<BrowserContext> {
252-
const androidBrowser = new AndroidBrowser(this, socketName);
253-
await androidBrowser._open();
246+
const socket = await this._waitForLocalAbstract(socketName);
247+
const androidBrowser = new AndroidBrowser(this, socket);
248+
await androidBrowser._init();
254249
this._browserConnections.add(androidBrowser);
255250

256251
const browserOptions: BrowserOptions = {
@@ -357,17 +352,22 @@ export class AndroidDevice extends EventEmitter {
357352

358353
class AndroidBrowser extends EventEmitter {
359354
readonly device: AndroidDevice;
360-
readonly socketName: string;
361-
private _socket: SocketBackend | undefined;
355+
private _socket: SocketBackend;
362356
private _receiver: stream.Writable;
363357
private _waitForNextTask = makeWaitForNextTask();
364358
onmessage?: (message: any) => void;
365359
onclose?: () => void;
366360

367-
constructor(device: AndroidDevice, socketName: string) {
361+
constructor(device: AndroidDevice, socket: SocketBackend) {
368362
super();
369363
this.device = device;
370-
this.socketName = socketName;
364+
this._socket = socket;
365+
this._socket.on('close', () => {
366+
this._waitForNextTask(() => {
367+
if (this.onclose)
368+
this.onclose();
369+
});
370+
});
371371
this._receiver = new (ws as any).Receiver() as stream.Writable;
372372
this._receiver.on('message', message => {
373373
this._waitForNextTask(() => {
@@ -377,14 +377,7 @@ class AndroidBrowser extends EventEmitter {
377377
});
378378
}
379379

380-
async _open() {
381-
this._socket = await this.device._backend.open(`localabstract:${this.socketName}`);
382-
this._socket.on('close', () => {
383-
this._waitForNextTask(() => {
384-
if (this.onclose)
385-
this.onclose();
386-
});
387-
});
380+
async _init() {
388381
await this._socket.write(Buffer.from(`GET /devtools/browser HTTP/1.1\r
389382
Upgrade: WebSocket\r
390383
Connection: Upgrade\r

test/android/device.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (process.env.PW_ANDROID_TESTS) {
2424
});
2525

2626
it('should open a ADB socket', async function({ device }) {
27-
const socket = await device.open('/bin/cat');
27+
const socket = await device.open('shell:/bin/cat');
2828
await socket.write(Buffer.from('321\n'));
2929
const output = await new Promise(resolve => socket.on('data', resolve));
3030
expect(output.toString()).toBe('321\n');

utils/avd_recreate.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ fi
1010

1111
${ANDROID_HOME}/tools/bin/avdmanager delete avd --name android30 || true
1212
echo "y" | ${ANDROID_HOME}/tools/bin/sdkmanager --install "system-images;android-30;google_apis;x86"
13-
echo "no" | ${ANDROID_HOME}/tools/bin/avdmanager create avd --force --name android30 --device "Nexus 5X" -k 'system-images;android-30;google_apis;x86'
13+
echo "no" | ${ANDROID_HOME}/tools/bin/avdmanager create avd --force --name android30 --device pixel_4 --package "system-images;android-30;google_apis;x86"
1414
${ANDROID_HOME}/emulator/emulator -list-avds

utils/avd_start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if [[ -z "${ANDROID_HOME}" ]]; then
99
fi
1010

1111
echo "Killing previous emulators"
12-
adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
12+
${ANDROID_HOME}/platform-tools/adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
1313

1414
echo "Starting emulator"
1515
nohup ${ANDROID_HOME}/emulator/emulator -avd android30 -no-audio -no-snapshot -no-window -gpu swiftshader_indirect &

0 commit comments

Comments
 (0)