-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathCapabilityManager.ts
More file actions
162 lines (152 loc) · 6.17 KB
/
CapabilityManager.ts
File metadata and controls
162 lines (152 loc) · 6.17 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
import _ from 'lodash';
import { updatedAllocatedDevice } from './data-service/device-service';
import { getFreePort } from './helpers';
import { IDevice } from './interfaces/IDevice';
import { ISessionCapability } from './interfaces/ISessionCapability';
import log from './logger';
import { DevicePlugin } from './plugin';
import { prisma } from './prisma';
export enum DEVICE_FARM_CAPABILITIES {
BUILD_NAME = 'build',
SESSION_NAME = 'name',
VIDEO_RECORDING = 'recordVideo',
VIDEO_RESOLUTION = 'videoResolution',
VIDEO_TIME_LIMIT = 'videoTimeLimit',
LIVE_VIDEO = 'liveVideo',
SCREENSHOT_ON_FAILURE = 'screenshotOnFailure',
SCREENSHOT_ON_ALL = 'screenshotOnAll',
DEVICE_FARM_OPTIONS = 'df:options',
DEVICE_TIMEOUT = 'deviceAvailabilityTimeout',
DEVICE_QUERY_INTERVAL = 'deviceRetryInterval',
iPHONEONLY = 'iPhoneOnly',
iPADONLY = 'iPadOnly',
UDIDS = 'udids',
MIN_SDK = 'minSDK',
MAX_SDK = 'maxSDK',
FILTER_BY_HOST = 'filterByHost',
SAVE_DEVICE_LOGS = 'saveDeviceLogs',
TAGS = 'tags',
}
function isCapabilityAlreadyPresent(caps: ISessionCapability, capabilityName: string) {
return _.has(caps.alwaysMatch, capabilityName) || _.has(caps.firstMatch[0], capabilityName);
}
function deleteAlwaysMatch(caps: ISessionCapability, capabilityName: string) {
if (_.has(caps.alwaysMatch, capabilityName)) delete caps.alwaysMatch[capabilityName];
}
async function findAppPath(caps: any) {
const mergedCaps = Object.assign({}, caps.firstMatch ? caps.firstMatch[0] : {}, caps.alwaysMatch);
const fileName = mergedCaps['appium:app'];
if (fileName?.startsWith('file')) {
const appInfo: any = await prisma.appInformation.findFirst({
where: { uploadedFileName: fileName as string },
});
return `${DevicePlugin.serverUrl}${appInfo?.path}`;
} else {
return fileName;
}
}
export async function androidCapabilities(
caps: ISessionCapability,
freeDevice: IDevice,
options: { liveVideo: boolean; portRange?: string },
) {
caps.firstMatch[0] = caps.firstMatch[0] || {};
caps.firstMatch[0]['appium:app'] = await findAppPath(caps);
caps.firstMatch[0]['appium:udid'] = freeDevice.udid;
caps.firstMatch[0]['appium:systemPort'] = await getFreePort(options.portRange);
caps.firstMatch[0]['appium:chromeDriverPort'] = await getFreePort(options.portRange);
caps.firstMatch[0]['appium:adbRemoteHost'] = freeDevice.adbRemoteHost;
caps.firstMatch[0]['appium:adbPort'] = freeDevice.adbPort;
if (freeDevice.chromeDriverPath)
caps.firstMatch[0]['appium:chromedriverExecutable'] = freeDevice.chromeDriverPath;
if (!isCapabilityAlreadyPresent(caps, 'appium:mjpegServerPort')) {
caps.firstMatch[0]['appium:mjpegServerPort'] = options.liveVideo
? await getFreePort(options.portRange)
: undefined;
}
if (!options.liveVideo) {
deleteAlwaysMatch(caps, 'appium:mjpegServerPort');
}
deleteAlwaysMatch(caps, 'appium:udid');
deleteAlwaysMatch(caps, 'appium:systemPort');
deleteAlwaysMatch(caps, 'appium:chromeDriverPort');
deleteAlwaysMatch(caps, 'appium:adbRemoteHost');
deleteAlwaysMatch(caps, 'appium:adbPort');
deleteAlwaysMatch(caps, 'appium:app');
}
export async function iOSCapabilities(
caps: ISessionCapability,
freeDevice: IDevice,
options: {
liveVideo: boolean;
portRange?: string;
},
) {
if (!process.env.GO_IOS) {
freeDevice.mjpegServerPort = options.liveVideo
? await getFreePort(options.portRange)
: undefined;
}
caps.firstMatch[0] = caps.firstMatch[0] || {};
caps.firstMatch[0]['appium:app'] = await findAppPath(caps);
caps.firstMatch[0]['appium:udid'] = freeDevice.udid;
caps.firstMatch[0]['appium:deviceName'] = freeDevice.name;
caps.firstMatch[0]['appium:platformVersion'] = freeDevice.sdk;
caps.firstMatch[0]['appium:mjpegServerPort'] = freeDevice.mjpegServerPort;
caps.firstMatch[0]['appium:wdaLocalPort'] = freeDevice.wdaLocalPort = await getFreePort(
options.portRange,
);
if (freeDevice.realDevice && !caps.firstMatch[0]['df:skipReport']) {
const wdaFileName = freeDevice.platform === 'tvos' ? 'wda-resign_tvos.ipa' : 'wda-resign.ipa';
const wdaInfo = await prisma.appInformation.findFirst({
where: { fileName: wdaFileName },
});
if (wdaInfo && !process.env.GO_IOS) {
caps.firstMatch[0]['appium:usePreinstalledWDA'] = true;
caps.firstMatch[0]['appium:updatedWDABundleId'] = wdaInfo.appBundleId;
caps.firstMatch[0]['appium:updatedWDABundleIdSuffix'] = '';
} else if (wdaInfo && process.env.GO_IOS && !caps.alwaysMatch?.['appium:webDriverAgentUrl']) {
log.info('Setting webDriverAgentUrl for real device');
caps.firstMatch[0]['appium:webDriverAgentUrl'] =
freeDevice.webDriverAgentUrl = `${freeDevice.webDriverAgentHost}:${freeDevice.wdaLocalPort}`;
delete caps.firstMatch[0]['appium:wdaLocalPort'];
}
}
if (!freeDevice.realDevice) {
caps.firstMatch[0]['appium:derivedDataPath'] = `${freeDevice.derivedDataPath}`;
}
const deleteMatch = [
'appium:derivedDataPath',
'appium:platformVersion',
'appium:wdaLocalPort',
'appium:mjpegServerPort',
'appium:udid',
'appium:deviceName',
'appium:app',
];
if (!options.liveVideo) {
deleteMatch.push('appium:mjpegServerPort');
}
deleteMatch.forEach((value) => deleteAlwaysMatch(caps, value));
await updatedAllocatedDevice(freeDevice, {
wdaLocalPort: freeDevice.wdaLocalPort,
mjpegServerPort: freeDevice.mjpegServerPort,
webDriverAgentUrl: freeDevice.webDriverAgentUrl,
});
}
export function getDeviceFarmCapabilities(caps: ISessionCapability) {
const mergedCapabilites = Object.assign({}, caps.firstMatch[0], caps.alwaysMatch);
const deviceFarmOptions = mergedCapabilites[DEVICE_FARM_CAPABILITIES.DEVICE_FARM_OPTIONS] || {};
// Pick all capabilities that starts with df: and store it as a separate object
const individualCapabilities = Object.keys(mergedCapabilites)
.filter((key) => key.toLowerCase().trim().startsWith('df:'))
.reduce(
(acc: Record<string, any>, originalkey: string) => {
const strippedKey = originalkey.split(':')[1];
acc[strippedKey] = mergedCapabilites[originalkey];
return acc;
},
{} as Record<string, any>,
);
return _.merge(deviceFarmOptions, individualCapabilities);
}