-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathIOSDeviceManager.ts
More file actions
520 lines (480 loc) · 17.5 KB
/
IOSDeviceManager.ts
File metadata and controls
520 lines (480 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import { utilities as IOSUtils } from 'appium-ios-device';
import { getDeviceInfo } from 'appium-ios-device/build/lib/utilities';
import Simctl from 'node-simctl';
import os from 'os';
import path from 'path';
import { addNewDevice, generateDeviceId, removeDevice } from '../data-service/device-service';
import { startTunnel } from '../goIOSTracker';
import { asyncForEach, getFreePort } from '../helpers';
import { IDevice } from '../interfaces/IDevice';
import { IDeviceManager } from '../interfaces/IDeviceManager';
import { DeviceTypeToInclude, IDerivedDataPath, IPluginArgs } from '../interfaces/IPluginArgs';
import log from '../logger';
import Devices from './cloud/Devices';
import { IOSDeviceInfoMap } from './IOSDeviceType';
import { IosTracker } from './iOSTracker';
import NodeDevices from './NodeDevices';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { flatten, isEmpty } = require('lodash');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs-extra');
interface IDeviceInfo {
ProductType?: string;
ProductName?: string;
DeviceClass?: string;
DeviceName?: string;
[key: string]: any; // For other properties that might exist
}
export default class IOSDeviceManager implements IDeviceManager {
constructor(
private pluginArgs: IPluginArgs,
private hostPort: number,
private nodeId: string,
) {}
/**
* Method to get all ios devices and simulators
*
* @returns {Promise<Array<IDevice>>}
*/
async getDevices(
deviceTypes: { iosDeviceType: DeviceTypeToInclude },
existingDeviceDetails: Array<IDevice>,
): Promise<IDevice[]> {
if (this.pluginArgs.cloud != undefined) {
const devices: IDevice[] = [];
const cloud = new Devices(this.pluginArgs.cloud, devices, 'ios');
return await cloud.getDevices();
} else {
if (deviceTypes.iosDeviceType === 'real') {
return flatten(
await Promise.all([
this.getRealDevices(existingDeviceDetails, this.pluginArgs, this.hostPort),
]),
);
} else if (deviceTypes.iosDeviceType === 'simulated') {
const simulators = flatten(await Promise.all([this.getSimulators()]));
log.debug(`Simulators: ${JSON.stringify(simulators)}`);
return simulators;
} else {
// return both real and simulated devices
log.debug('Getting both real and simulated devices');
return flatten(
await Promise.all([
this.getRealDevices(existingDeviceDetails, this.pluginArgs, this.hostPort),
this.getSimulators(),
]),
);
}
}
}
async getConnectedDevices(): Promise<Array<string>> {
try {
const devices: string[] = await IOSUtils.getConnectedDevices();
return devices;
} catch (error) {
log.error(error);
return [];
}
}
async getOSVersion(udid: string) {
return await IOSUtils.getOSVersion(udid);
}
async getDeviceName(udid: string) {
return await IOSUtils.getDeviceName(udid);
}
private getDevicePlatformName(
name: string,
productType?: string,
width?: string,
height?: string,
deviceInfo?: IDeviceInfo,
) {
const nameLower = name?.toLowerCase() || '';
const productTypeLower = productType?.toLowerCase() || '';
// First check ProductType for definitive identification (most reliable)
if (productTypeLower.includes('appletv')) {
return 'tvos';
}
if (
productTypeLower.includes('iphone') ||
productTypeLower.includes('ipad') ||
productTypeLower.includes('ipod')
) {
return 'ios';
}
// If ProductType is not definitive, check device name patterns
// Check for iOS devices by name first (higher priority)
if (nameLower.includes('iphone') || nameLower.includes('ipad') || nameLower.includes('ipod')) {
return 'ios';
}
// Then check for Apple TV devices by name (more specific patterns)
// Only consider it tvOS if it's clearly an Apple TV device
if (
nameLower.includes('apple tv') ||
(nameLower.startsWith('tv') &&
!nameLower.includes('iphone') &&
!nameLower.includes('ipad') &&
!nameLower.includes('ipod'))
) {
return 'tvos';
}
// For unknown devices (like "Appium"), try to determine based on device characteristics
// Use ideviceinfo data for more accurate detection
if (deviceInfo) {
const { ProductName, DeviceClass, DeviceName } = deviceInfo;
// Check ProductName for Apple TV indicators
if (ProductName && ProductName.toLowerCase().includes('apple tv')) {
return 'tvos';
}
// Check DeviceClass for Apple TV indicators
if (DeviceClass && DeviceClass.toLowerCase().includes('tv')) {
return 'tvos';
}
// Check DeviceName for Apple TV indicators
if (DeviceName && DeviceName.toLowerCase().includes('apple tv')) {
return 'tvos';
}
}
// Fallback to aspect ratio detection if ideviceinfo doesn't provide clear info
if (width && height) {
const widthNum = parseInt(width);
const heightNum = parseInt(height);
if (widthNum > 0 && heightNum > 0) {
const aspectRatio = widthNum / heightNum;
// Apple TV devices typically have 16:9 aspect ratio (1.78)
// Common Apple TV resolutions: 1920x1080 (1.78), 3840x2160 (1.78)
// iOS devices have different aspect ratios:
// - iPhone: ~0.46 (portrait), ~2.17 (landscape)
// - iPad: ~0.75 (portrait), ~1.33 (landscape)
// 16:9 aspect ratio is exactly 1.777... (16/9)
// Allow very small tolerance for rounding errors only
if (aspectRatio >= 1.776 && aspectRatio <= 1.778) {
// Very close to exact 16:9 aspect ratio suggests Apple TV
return 'tvos';
} else {
// Any other aspect ratio suggests iOS device
return 'ios';
}
}
}
// If we still can't determine, default to iOS for backward compatibility
return 'ios';
}
/**
* Method to get all ios real devices
*
* @returns {Promise<Array<IDevice>>}
*/
private async getRealDevices(
existingDeviceDetails: Array<IDevice>,
pluginArgs: IPluginArgs,
hostPort: number,
): Promise<Array<IDevice>> {
let deviceState: Array<IDevice> = [];
if (this.pluginArgs.cloud !== undefined) {
const cloud = new Devices(this.pluginArgs.cloud, deviceState, 'ios');
return await cloud.getDevices();
} else {
deviceState = await this.fetchLocalIOSDevices(existingDeviceDetails, pluginArgs, hostPort);
}
const returnDevices = deviceState.filter((device) => device.realDevice === true);
return returnDevices;
}
private prepareDerivedDataPath(
derivedDataPath: IDerivedDataPath | undefined,
udid: string,
realDevice: boolean,
): string {
function derivedPathExtracted(tmpPath: string, theDerivedDataPath?: string) {
if (theDerivedDataPath !== undefined) {
fs.copySync(theDerivedDataPath, tmpPath);
} else {
if (!fs.existsSync(tmpPath)) {
log.info(`DerivedDataPath for UDID ${udid} not set, so falling back to ${tmpPath}`);
log.info(
`WDA will be build once and will use WDA Runner from path ${tmpPath}, second test run will skip the build process`,
);
fs.mkdirSync(tmpPath, { recursive: true });
}
}
}
if (derivedDataPath) {
if (typeof derivedDataPath !== 'object')
throw new Error('DerivedData Path should be able Object');
const tmpPath = path.join(
os.homedir(),
`Library/Developer/Xcode/DerivedData/WebDriverAgent-${udid}`,
);
if (realDevice) {
derivedPathExtracted(tmpPath, derivedDataPath.device);
} else {
derivedPathExtracted(tmpPath, derivedDataPath.simulator);
}
return tmpPath;
} else {
return path.join(os.homedir(), `Library/Developer/Xcode/DerivedData/WebDriverAgent-${udid}`);
}
}
private async fetchLocalIOSDevices(
existingDeviceDetails: IDevice[],
pluginArgs: IPluginArgs,
hostPort: number,
): Promise<IDevice[]> {
const devices = await this.getConnectedDevices();
const deviceState: IDevice[] = [];
this.trackIOSDevices(pluginArgs);
await asyncForEach(devices, async (udid: string) => {
const existingDevice = existingDeviceDetails.find((device) => device.udid === udid);
if (existingDevice) {
log.info(`IOS Device details for ${udid} already available`);
deviceState.push({
...existingDevice,
busy: false,
userBlocked: false,
});
} else {
log.debug(`Getting device info for ${udid}`);
const deviceInfo = await this.getDeviceInfo(udid, pluginArgs, hostPort);
deviceState.push(deviceInfo);
}
});
return deviceState;
}
trackIOSDevices(pluginArgs: IPluginArgs) {
const iosTracker = IosTracker.getInstance();
iosTracker.on('attached', async (udid: string) => {
log.info(`Attached iOS device ${udid}`);
const deviceAttached = await this.getDeviceInfo(udid, pluginArgs, this.hostPort);
if (deviceAttached.goIOSAgentPort != undefined) {
await startTunnel(udid, deviceAttached.sdk, deviceAttached.goIOSAgentPort);
}
const deviceTracked: IDevice = {
...deviceAttached,
nodeId: this.nodeId,
};
if (pluginArgs.hub !== undefined) {
log.info(`Updating Hub with iOS device ${udid}`);
const nodeDevices = new NodeDevices(pluginArgs.hub);
await nodeDevices.postDevicesToHub([deviceTracked], 'add');
}
// add device to local list
log.info(`iOS device with udid ${udid} plugged! updating device list...`);
await addNewDevice([deviceTracked], pluginArgs.bindHostOrIp);
});
iosTracker.on('detached', async (udid: string) => {
const deviceRemoved: any = [{ udid, host: pluginArgs.bindHostOrIp }];
if (pluginArgs.hub !== undefined) {
log.info(`iOS device with udid ${udid} unplugged! updating hub device list...`);
const nodeDevices = new NodeDevices(pluginArgs.hub);
await nodeDevices.postDevicesToHub(deviceRemoved, 'remove');
}
// remove device from local list
log.info(`iOS device with udid ${udid} unplugged! updating device list...`);
await removeDevice(deviceRemoved);
});
}
private async getDeviceInfo(
udid: string,
pluginArgs: IPluginArgs,
hostPort: number,
): Promise<IDevice> {
log.info(`Getting device info for ${udid}`);
let host;
if (pluginArgs.remoteMachineProxyIP) {
host = pluginArgs.remoteMachineProxyIP;
} else {
host = `http://${pluginArgs.bindHostOrIp}:${hostPort}`;
}
const wdaLocalPort = await getFreePort(pluginArgs.portRange);
const mjpegServerPort = await getFreePort(pluginArgs.portRange);
const totalUtilizationTimeMilliSec = 0;
const [sdk, name] = await Promise.all([this.getOSVersion(udid), this.getDeviceName(udid)]);
const deviceInfo = await getDeviceInfo(udid);
const { ProductType } = deviceInfo;
const modelInfo = this.findKeyByValue(ProductType) || { Width: '', Height: '' };
const platform = this.getDevicePlatformName(
name,
ProductType,
modelInfo.Width,
modelInfo.Height,
deviceInfo,
);
// Generate goIOSAgentPort if GO_IOS environment variable is set
const goIOS = process.env.GO_IOS;
const goIOSAgentPort = goIOS ? await getFreePort(pluginArgs.portRange) : undefined;
return Object.assign({
id: generateDeviceId({
udid: udid,
realDevice: true,
nodeId: this.nodeId,
platform: platform,
} as any),
wdaLocalPort,
mjpegServerPort,
udid,
sdk,
name,
busy: false,
realDevice: true,
deviceType: 'real',
platform: platform,
host,
wdaBundleId: '',
productModel: ProductType,
totalUtilizationTimeMilliSec: totalUtilizationTimeMilliSec,
sessionStartTime: 0,
width: modelInfo.Width,
height: modelInfo.Height,
tags: [],
webDriverAgentHost: `http://${pluginArgs.bindHostOrIp}`,
webDriverAgentUrl: `http://${pluginArgs.bindHostOrIp}:${wdaLocalPort}`,
...(goIOSAgentPort && { goIOSAgentPort }),
});
}
/**
* Method to get all ios simulators
*
* @returns {Promise<Array<IDevice>>}
*/
public async getSimulators(): Promise<Array<IDevice>> {
const simulators = await this.fetchLocalSimulators();
simulators.sort((a, b) => (a.state > b.state ? 1 : -1));
// should not be here, but we need to update the hub with simulators
if (this.pluginArgs.hub !== undefined) {
log.info('Updating Hub with Simulators');
const nodeDevices = new NodeDevices(this.pluginArgs.hub);
await nodeDevices.postDevicesToHub(simulators, 'add');
}
return simulators;
}
public async fetchLocalSimulators() {
log.debug('Fetching local simulators');
const returnedSimulators: IDevice[] = [];
const { simctl, list } = await this.createSimctlInstance();
const flattenValued = await this.getLocalSims(simctl, list);
let filteredSimulators: Array<IDevice> = [];
const localPluginArgs = this.pluginArgs;
if (this.pluginArgs.simulators !== undefined) {
filteredSimulators = flattenValued.filter((device: IDevice) =>
localPluginArgs.simulators.some(
(simulator: IDevice) => device.name === simulator.name && device.sdk === simulator.sdk,
),
);
}
if (this.pluginArgs.bootedSimulators) {
filteredSimulators = flattenValued.filter((device) => {
return device.state === 'Booted';
});
}
//log.debug(`Filtered Simulators: ${JSON.stringify(filteredSimulators)}`);
const buildSimulators = !isEmpty(filteredSimulators) ? filteredSimulators : flattenValued;
//log.debug(`Build Simulators: ${JSON.stringify(buildSimulators)}`);
const deviceTypes = await list.devicetypes;
for await (const device of buildSimulators) {
const productModel = IOSDeviceManager.getProductModel(deviceTypes, device);
const wdaLocalPort = await getFreePort(this.pluginArgs.portRange);
const mjpegServerPort = await getFreePort(this.pluginArgs.portRange);
const totalUtilizationTimeMilliSec = 0;
const modelInfo = this.findKeyByValue(productModel) || { Width: '', Height: '' };
returnedSimulators.push(
Object.assign({
...device,
id: generateDeviceId({
udid: device.udid,
realDevice: false,
nodeId: this.nodeId,
platform: 'ios',
} as any),
wdaLocalPort,
mjpegServerPort,
busy: false,
realDevice: false,
platform: this.getDevicePlatformName(
device.name,
productModel,
modelInfo.Width,
modelInfo.Height,
undefined,
),
deviceType: 'simulator',
host: `http://${this.pluginArgs.bindHostOrIp}:${this.hostPort}`,
totalUtilizationTimeMilliSec: totalUtilizationTimeMilliSec,
sessionStartTime: 0,
productModel,
width: modelInfo.Width,
height: modelInfo.Height,
derivedDataPath: this.prepareDerivedDataPath(
this.pluginArgs.derivedDataPath,
device.udid,
false,
),
preBuildWDAPath: this.pluginArgs.preBuildWDAPath,
tags: [],
nodeId: this.nodeId,
}),
);
}
return returnedSimulators;
}
static getProductModel(deviceTypes: any, device: IDevice) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (device.platform === 'tvOS') {
return undefined;
} else {
return deviceTypes.filter(
(deviceType: any) => deviceType.identifier === device.deviceTypeIdentifier,
)[0]?.modelIdentifier;
}
}
findKeyByValue(model: string): any {
if (model !== undefined) {
for (const [key, value] of Object.entries(IOSDeviceInfoMap)) {
if (model === key) {
return value;
}
}
}
}
private async getLocalSims(simctl: any, list: any): Promise<Array<IDevice>> {
try {
const runtimes = list.runtimes;
const unAavailableRuntimes = runtimes
.filter((runtime: any) => !runtime.isAvailable)
.map((runtime: any) => runtime.name);
if (unAavailableRuntimes.length > 0) {
log.error(`The following runtimes are not available: ${unAavailableRuntimes.join(', ')}`);
}
const iOSSimulators = flatten(Object.values(await simctl.getDevices(null, 'iOS'))).length > 0;
const tvSimulators = flatten(Object.values(await simctl.getDevices(null, 'tvOS'))).length > 0;
log.debug(`iOS Simulators: ${iOSSimulators}`);
log.debug(`tvOS Simulators: ${tvSimulators}`);
let iosSimulators: any = [];
let tvosSimulators: any = [];
if (iOSSimulators) {
iosSimulators = flatten(
Object.values((await simctl.getDevices(null, 'iOS')) as Array<IDevice>),
);
} else {
log.info('No iOS simulators found!');
}
if (tvSimulators) {
tvosSimulators = flatten(
Object.values((await simctl.getDevicesByParsing('tvOS')) as Array<IDevice>),
);
} else {
log.info('No tvOS simulators found!');
}
return [...iosSimulators, ...tvosSimulators];
} catch (error) {
log.error(error);
return [];
}
}
private async createSimctlInstance() {
const simctl = new Simctl();
// list runtimes and log availability errors
const list = await simctl.list();
return { simctl, list };
}
}