@@ -25,10 +25,26 @@ node maestro-android.js <path to app> <app_id> <maestro_flow> <flavor> <working_
2525
2626const MAX_ATTEMPTS = 5 ;
2727
28- function launchSimulator ( simulatorName ) {
29- console . log ( `Launching simulator ${ simulatorName } ` ) ;
28+ function findAvailableSimulator ( ) {
29+ const output = childProcess . execSync (
30+ 'xcrun simctl list devices available -j' ,
31+ ) ;
32+ const devices = Object . values ( JSON . parse ( String ( output ) ) . devices )
33+ . flat ( )
34+ . reverse ( ) ;
35+ const simulator = devices . find ( device => / ^ i P h o n e .* P r o $ / . test ( device . name ) ) ;
36+
37+ if ( simulator == null ) {
38+ throw new Error ( 'Unable to find an available iPhone Pro simulator' ) ;
39+ }
40+
41+ return simulator ;
42+ }
43+
44+ function launchSimulator ( simulator ) {
45+ console . log ( `Launching simulator ${ simulator . name } (${ simulator . udid } )` ) ;
3046 try {
31- childProcess . execSync ( `xcrun simctl boot "${ simulatorName } "` ) ;
47+ childProcess . execSync ( `xcrun simctl boot "${ simulator . udid } "` ) ;
3248 } catch ( error ) {
3349 if (
3450 ! error . message . includes ( 'Unable to boot device in current state: Booted' )
@@ -43,14 +59,6 @@ function installAppOnSimulator(appPath) {
4359 childProcess . execSync ( `xcrun simctl install booted "${ appPath } "` ) ;
4460}
4561
46- function extractSimulatorUDID ( ) {
47- console . log ( 'Retrieving device UDID' ) ;
48- const command = `xcrun simctl list devices booted -j | jq -r '[.devices[]] | add | first | .udid'` ;
49- const udid = String ( childProcess . execSync ( command ) ) . trim ( ) ;
50- console . log ( `UDID is ${ udid } ` ) ;
51- return udid ;
52- }
53-
5462function bringSimulatorInForeground ( ) {
5563 console . log ( 'Bringing simulator in foreground' ) ;
5664 childProcess . execSync ( 'open -a simulator' ) ;
@@ -166,13 +174,12 @@ async function main(args = process.argv.slice(2)) {
166174 console . info ( `WORKING_DIRECTORY: ${ workingDirectory } ` ) ;
167175 console . info ( '==============================\n' ) ;
168176
169- const simulatorName = 'iPhone 16 Pro' ;
170- launchSimulator ( simulatorName ) ;
177+ const simulator = findAvailableSimulator ( ) ;
178+ launchSimulator ( simulator ) ;
171179 installAppOnSimulator ( appPath ) ;
172- const udid = extractSimulatorUDID ( ) ;
173180 bringSimulatorInForeground ( ) ;
174- await launchAppOnSimulator ( appId , udid , isDebug ) ;
175- executeFlows ( appId , udid , maestroFlow , jsengine ) ;
181+ await launchAppOnSimulator ( appId , simulator . udid , isDebug ) ;
182+ executeFlows ( appId , simulator . udid , maestroFlow , jsengine ) ;
176183 console . log ( 'Test finished' ) ;
177184}
178185
@@ -182,4 +189,5 @@ if (require.main === module) {
182189
183190module . exports = {
184191 executeFlows,
192+ findAvailableSimulator,
185193} ;
0 commit comments