1
1
import { AppiumXCUITestCapabilities } from '@wdio/types/build/Capabilities' ;
2
2
import { W3CCapabilities } from '@wdio/types/build/Capabilities' ;
3
3
import dotenv from 'dotenv' ;
4
+ import { existsSync , readFileSync } from 'fs' ;
4
5
5
6
import { IntRange } from '../../../types/RangeType' ;
7
+
6
8
dotenv . config ( ) ;
9
+
7
10
const iosPathPrefix = process . env . IOS_APP_PATH_PREFIX ;
8
11
9
12
if ( ! iosPathPrefix ) {
@@ -16,8 +19,8 @@ console.log(`iOS app full path: ${iosAppFullPath}`);
16
19
const sharediOSCapabilities : AppiumXCUITestCapabilities = {
17
20
'appium:app' : iosAppFullPath ,
18
21
'appium:platformName' : 'iOS' ,
19
- 'appium:platformVersion' : '17.2 ' ,
20
- 'appium:deviceName' : 'iPhone 15 Pro Max' ,
22
+ 'appium:platformVersion' : '18.3 ' ,
23
+ 'appium:deviceName' : 'iPhone 16 Pro Max' ,
21
24
'appium:automationName' : 'XCUITest' ,
22
25
'appium:bundleId' : 'com.loki-project.loki-messenger' ,
23
26
'appium:newCommandTimeout' : 300000 ,
@@ -31,62 +34,92 @@ const sharediOSCapabilities: AppiumXCUITestCapabilities = {
31
34
communityPollLimit : 5 ,
32
35
} ,
33
36
} ,
34
- // "appium:isHeadless": true,
35
37
} as AppiumXCUITestCapabilities ;
36
38
37
- const envVars = [
38
- 'IOS_1_SIMULATOR' ,
39
- 'IOS_2_SIMULATOR' ,
40
- 'IOS_3_SIMULATOR' ,
41
- 'IOS_4_SIMULATOR' ,
42
- 'IOS_5_SIMULATOR' ,
43
- 'IOS_6_SIMULATOR' ,
44
- 'IOS_7_SIMULATOR' ,
45
- 'IOS_8_SIMULATOR' ,
46
- 'IOS_9_SIMULATOR' ,
47
- 'IOS_10_SIMULATOR' ,
48
- 'IOS_11_SIMULATOR' ,
49
- 'IOS_12_SIMULATOR' ,
50
- ] as const ;
51
-
52
- function getIOSSimulatorUUIDFromEnv ( index : CapabilitiesIndexType ) : string {
53
- const envVar = envVars [ index ] ;
54
- const uuid = process . env [ envVar ] ;
55
-
56
- if ( ! uuid ) {
57
- throw new Error ( `Environment variable ${ envVar } is not set` ) ;
39
+ export type Simulator = {
40
+ name : string ;
41
+ udid : string ;
42
+ wdaPort : number ;
43
+ } ;
44
+
45
+ function loadSimulators ( ) : Simulator [ ] {
46
+ const jsonPath = 'ci-simulators.json' ;
47
+
48
+ // Load from .env variables
49
+ const envVars = [
50
+ 'IOS_1_SIMULATOR' ,
51
+ 'IOS_2_SIMULATOR' ,
52
+ 'IOS_3_SIMULATOR' ,
53
+ 'IOS_4_SIMULATOR' ,
54
+ 'IOS_5_SIMULATOR' ,
55
+ 'IOS_6_SIMULATOR' ,
56
+ 'IOS_7_SIMULATOR' ,
57
+ 'IOS_8_SIMULATOR' ,
58
+ 'IOS_9_SIMULATOR' ,
59
+ 'IOS_10_SIMULATOR' ,
60
+ 'IOS_11_SIMULATOR' ,
61
+ 'IOS_12_SIMULATOR' ,
62
+ ] ;
63
+
64
+ const simulators = envVars
65
+ . map ( ( envVar , index ) => {
66
+ const udid = process . env [ envVar ] ;
67
+ if ( ! udid ) return null ; // No need for all 12 sim variables to be set
68
+ return { name : `Sim-${ index + 1 } ` , udid, wdaPort : 1253 + index } ;
69
+ } )
70
+ . filter ( ( sim ) : sim is Simulator => sim !== null ) ;
71
+
72
+ // If we have simulators from env, use them (local dev)
73
+ if ( simulators . length > 0 ) {
74
+ console . log ( `Using ${ simulators . length } simulators from .env file` ) ;
75
+ return simulators ;
76
+ }
77
+
78
+ // No env simulators - check if we're on CI
79
+ if ( process . env . CI === '1' ) {
80
+ // CI should use JSON
81
+ if ( existsSync ( jsonPath ) ) {
82
+ console . log ( 'Using simulators from ios-simulators.json (CI)' ) ;
83
+ const sims : Simulator [ ] = JSON . parse ( readFileSync ( jsonPath , 'utf-8' ) ) ;
84
+ return sims ;
85
+ }
86
+ throw new Error ( 'CI mode: ios-simulators.json not found' ) ;
58
87
}
59
88
60
- return uuid ;
89
+ // Local dev with no .env entries
90
+ throw new Error (
91
+ 'No iOS simulators found in .env\n' +
92
+ 'Run: yarn create-simulators <number>\n' +
93
+ 'Example: yarn create-simulators 4'
94
+ ) ;
61
95
}
62
- const MAX_CAPABILITIES_INDEX = envVars . length ;
96
+ const simulators = loadSimulators ( ) ;
97
+
98
+ const capabilities = simulators . map ( sim => ( {
99
+ ...sharediOSCapabilities ,
100
+ 'appium:udid' : sim . udid ,
101
+ 'appium:wdaLocalPort' : sim . wdaPort ,
102
+ } ) ) ;
103
+
104
+ // Use a constant max that matches the envVars array length for type safety
105
+ const _MAX_CAPABILITIES_INDEX = 12 as const ;
63
106
64
- export type CapabilitiesIndexType = IntRange < 0 , typeof MAX_CAPABILITIES_INDEX > ;
107
+ // For runtime validation, check against actual loaded simulators
108
+ export const getMaxCapabilitiesIndex = ( ) => capabilities . length ;
109
+
110
+ // Type is still based on the constant for compile-time safety
111
+ export type CapabilitiesIndexType = IntRange < 0 , typeof _MAX_CAPABILITIES_INDEX > ;
65
112
66
113
export function capabilityIsValid (
67
114
capabilitiesIndex : number
68
115
) : capabilitiesIndex is CapabilitiesIndexType {
69
- if ( capabilitiesIndex < 0 || capabilitiesIndex > MAX_CAPABILITIES_INDEX ) {
116
+ // Runtime validation against actual loaded capabilities
117
+ if ( capabilitiesIndex < 0 || capabilitiesIndex >= capabilities . length ) {
70
118
return false ;
71
119
}
72
120
return true ;
73
121
}
74
122
75
- interface CustomW3CCapabilities extends W3CCapabilities {
76
- 'appium:wdaLocalPort' : number ;
77
- 'appium:udid' : string ;
78
- }
79
-
80
- const emulatorUUIDs = Array . from ( { length : MAX_CAPABILITIES_INDEX } , ( _ , index ) =>
81
- getIOSSimulatorUUIDFromEnv ( index as CapabilitiesIndexType )
82
- ) ;
83
-
84
- const capabilities = emulatorUUIDs . map ( ( udid , index ) => ( {
85
- ...sharediOSCapabilities ,
86
- 'appium:udid' : udid ,
87
- 'appium:wdaLocalPort' : 1253 + index ,
88
- } ) ) ;
89
-
90
123
export function getIosCapabilities ( capabilitiesIndex : CapabilitiesIndexType ) : W3CCapabilities {
91
124
if ( capabilitiesIndex >= capabilities . length ) {
92
125
throw new Error (
@@ -102,11 +135,11 @@ export function getIosCapabilities(capabilitiesIndex: CapabilitiesIndexType): W3
102
135
} ;
103
136
}
104
137
105
- export function getCapabilitiesForWorker ( workerId : number ) : CustomW3CCapabilities {
138
+ export function getCapabilitiesForWorker ( workerId : number ) {
106
139
const emulator = capabilities [ workerId % capabilities . length ] ;
107
140
return {
108
141
...sharediOSCapabilities ,
109
142
'appium:udid' : emulator [ 'appium:udid' ] ,
110
143
'appium:wdaLocalPort' : emulator [ 'appium:wdaLocalPort' ] ,
111
- } as CustomW3CCapabilities ;
144
+ } ;
112
145
}
0 commit comments