Skip to content

Commit 1a234e1

Browse files
abdelhamid-f-nasserHeshamMegid
authored andcommitted
chore: add auto device selection detection (#1009)
Jira ID: MOB-12654
1 parent 4c30e75 commit 1a234e1

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

examples/default/.detoxrc.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,69 @@
11
/** @type {Detox.DetoxConfig} */
2+
3+
const { execSync } = require('child_process');
4+
5+
// Currently we only test on `Pixel_4_API_31`
6+
const specificAvdName = null;
7+
8+
// Returns the AVD name from available installed AVDs
9+
function getAVDName() {
10+
try {
11+
// Execute 'emulator -list-avds' command to get the list of AVDs
12+
const avdListOutput = execSync('emulator -list-avds').toString();
13+
14+
// Split the output to get individual AVD names
15+
const avdList = avdListOutput.trim().split('\n');
16+
17+
// Assuming you want to use the first AVD from the list, you can modify this logic as needed
18+
return specificAvdName !== null ? specificAvdName : avdList.length > 0 ? avdList[0] : null;
19+
} catch (error) {
20+
console.error('Error while fetching AVD list:', error);
21+
return null;
22+
}
23+
}
24+
25+
// Function to get the latest iOS simulator device type
26+
function getLatestIphoneSimulatorDeviceType() {
27+
try {
28+
// Execute 'xcrun simctl list devices' command to get the list of iOS simulator devices
29+
const allSimulatorDevices = execSync('xcrun simctl list devices').toString();
30+
31+
// Filter the output to get the list of iPhone devices
32+
const filteredDevices = allSimulatorDevices.match(/iPhone[^)]*/g);
33+
34+
// Clean the device names to get the final device type (excluding the device ID)
35+
const cleanedDevices = filteredDevices.map((device) => device.replace(/\(.*$/, '').trim());
36+
37+
// Order of iPhone models to prioritize
38+
const priorityOrder = [/Pro Max/i, /Pro/i, /\d/];
39+
40+
let selectedDeviceType = null;
41+
42+
// Loop through the priority order and select the first matching device type
43+
for (const regex of priorityOrder) {
44+
const matchingDevice = cleanedDevices.find((device) => regex.test(device));
45+
if (matchingDevice) {
46+
selectedDeviceType = matchingDevice;
47+
break;
48+
}
49+
}
50+
51+
// If no matching device is found, default to the last device in the list
52+
if (!selectedDeviceType) {
53+
selectedDeviceType = cleanedDevices[cleanedDevices.length - 1];
54+
}
55+
56+
// Remove the trailing newline character from the device type
57+
const trimmedDeviceType = selectedDeviceType.replace('\n', '');
58+
59+
// Return the final device type
60+
return trimmedDeviceType;
61+
} catch (error) {
62+
console.error('Error while fetching iOS simulator device list:', error);
63+
return null;
64+
}
65+
}
66+
267
module.exports = {
368
testRunner: {
469
$0: 'jest',
@@ -37,13 +102,13 @@ module.exports = {
37102
simulator: {
38103
type: 'ios.simulator',
39104
device: {
40-
type: 'iPhone 13 Pro Max',
105+
type: getLatestIphoneSimulatorDeviceType(),
41106
},
42107
},
43108
emulator: {
44109
type: 'android.emulator',
45110
device: {
46-
avdName: 'Nexus_6P_API_27',
111+
avdName: getAVDName(),
47112
},
48113
},
49114
},

0 commit comments

Comments
 (0)