Skip to content

Commit 979e320

Browse files
committed
Add support for specifying attached device by its serial & document
accepted env vars. Detox `device.adbName` property expects values as listed by `adb devices` command. According to `adb --help` this is device "serial", hence the naming. This commit also drops E2E part of env vars, since it does not fit everywhere & doesn't seem reqruied. If we encounter some namespace conflicts we will consider adding it back. This code is an example. **THE RUNTIME HAS NOT BEEN TESTED**.
1 parent 8c2044b commit 979e320

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

scripts/e2e/detox-utils.cjs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const testButlerApkPath = isRunningCI ? ['../Example/e2e/apps/test-butler-app-2.
1414

1515
function detectLocalAndroidEmulator() {
1616
// "RNS_E2E_AVD_NAME" can be set for local developement
17-
const avdName = process.env.RNS_E2E_AVD_NAME ?? null;
17+
const avdName = process.env.RNS_AVD_NAME ?? null;
1818
if (avdName !== null) {
1919
return avdName
2020
}
@@ -50,6 +50,67 @@ function detectAndroidEmulatorName() {
5050
}
5151

5252
/**
53+
* @returns {string | null} Device serial as requested by user, first serial from adb list or null
54+
*/
55+
function resolveAndroidDeviceSerial() {
56+
const deviceSerial = process.env.RNS_DEVICE_SERIAL ?? null;
57+
58+
if (deviceSerial !== null) {
59+
return deviceSerial;
60+
}
61+
62+
// Fallback: try to use adb
63+
try {
64+
let stdout = ChildProcess.execSync("adb devices");
65+
66+
// Possibly convert Buffer to string
67+
if (typeof stdout !== 'string') {
68+
stdout = stdout.toString();
69+
}
70+
71+
/** @type {string} */
72+
const stringStdout = stdout;
73+
74+
// Example `adb devices` output:
75+
//
76+
// List of devices attached
77+
// 6lh6huzhr48lu8t8 device
78+
// emulator-5554 device
79+
80+
const deviceList = stringStdout
81+
.trim()
82+
.split('\n')
83+
.map(line => line.trim())
84+
.filter((line, index) => line !== '' && index !== 0) // empty lines & header
85+
.map(line => line.split(' ', 1)[0]);
86+
87+
88+
if (deviceList.length === 0) {
89+
throw new Error("Seems that the attached device list is empty");
90+
}
91+
92+
// Just select first one in the list.
93+
// TODO: consider giving user a choice here.
94+
return deviceList[0];
95+
} catch (error) {
96+
console.error(`Failed to find attached device. Try setting "RNS_DEVICE_SERIAL" env variable pointing to one. Cause: ${error}`);
97+
}
98+
99+
return null;
100+
}
101+
102+
/**
103+
* The output of this function can be controlled through couple of env vars.
104+
*
105+
* * `RNS_DEVICE_SERIAL` env var can be specified in case of running
106+
* tests with an attached device. It can also be an emulator.
107+
* The expected value here is the same as you would pass to `adb -s`.
108+
* You can find device serial by running `adb devices` command.
109+
*
110+
* * `RNS_AVD_NAME` env var can be specified in case of running tests on emulator.
111+
* The exepected value here is the same as displayed in Android Studio or listed by
112+
* `emulator -list-avds`.
113+
*
53114
* @param {string} applicationName name (FabricExample / ScreensExample)
54115
* @returns {Detox.DetoxConfig}
55116
*/
@@ -101,7 +162,7 @@ function commonDetoxConfigFactory(applicationName) {
101162
attached: {
102163
type: 'android.attached',
103164
device: {
104-
adbName: process.env.RNS_ADB_NAME,
165+
adbName: resolveAndroidDeviceSerial(),
105166
},
106167
utilBinaryPaths: testButlerApkPath,
107168
},

0 commit comments

Comments
 (0)