forked from appium/node-simctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimctl-e2e-specs.ts
More file actions
317 lines (283 loc) · 10.5 KB
/
simctl-e2e-specs.ts
File metadata and controls
317 lines (283 loc) · 10.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
import {Simctl} from '../../lib/simctl';
import xcode from 'appium-xcode';
import {retryInterval} from 'asyncbox';
import {rimraf} from 'rimraf';
import {randomUUID} from 'node:crypto';
import path from 'node:path';
import os from 'node:os';
import fs from 'node:fs/promises';
import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised';
use(chaiAsPromised);
describe('simctl', function () {
const DEVICE_NAME = process.env.DEVICE_NAME || 'iPhone 17';
const MOCHA_TIMEOUT = 200000;
this.timeout(MOCHA_TIMEOUT);
let randName: string;
let validSdks: string[] = [];
let sdk: string;
let simctl: Simctl;
before(async function () {
simctl = new Simctl();
const devices = await simctl.getDevices();
console.log(`Found devices: ${JSON.stringify(devices, null, 2)}`); // eslint-disable-line no-console
validSdks = Object.keys(devices)
.filter((key) => devices[key].length > 0)
.sort((a, b) => a.localeCompare(b));
if (!validSdks.length) {
throw new Error('No valid SDKs');
}
console.log(`Found valid SDKs: ${validSdks.join(', ')}`); // eslint-disable-line no-console
sdk = `${process.env.IOS_SDK || validSdks.at(-1)}`;
// need to find a random name that does not already exist
// give it 5 tries
for (let i = 0; i < 5; i++) {
const randNum = parseInt((Math.random() * 100).toString(), 10);
randName = `device${randNum}`;
let nameFound = false;
for (const list of Object.values(devices)) {
if (list.map((item) => item.name).includes(randName)) {
// need to find another random name
nameFound = true;
break;
}
}
if (!nameFound) break; // eslint-disable-line curly
}
});
it('should retrieve a device with compatible properties', async function () {
const devices = await simctl.getDevices();
const sdkDevices = devices[sdk];
const firstDevice = sdkDevices[0];
const expectedList = ['name', 'sdk', 'state', 'udid'];
expect(firstDevice).to.have.any.keys(...expectedList);
});
describe('createDevice', function () {
after(async function () {
if (simctl.udid) {
await simctl.deleteDevice();
simctl.udid = null;
}
});
it('should create a device', async function () {
simctl.udid = await simctl.createDevice(randName, DEVICE_NAME, sdk);
expect(typeof simctl.udid).to.equal('string');
expect(simctl.udid.length).to.equal(36);
});
it('should create a device and be able to see it in devices list right away', async function () {
const devicesBefore = await simctl.getDevices();
const numSimsBefore = devicesBefore[sdk].length;
simctl.udid = await simctl.createDevice('node-simctl test', DEVICE_NAME, sdk);
const devicesAfter = await simctl.getDevices();
const numSimsAfter = devicesAfter[sdk].length;
expect(numSimsAfter).to.equal(numSimsBefore + 1);
});
});
describe('device manipulation', function () {
let simctl: Simctl;
const name = 'node-simctl test';
beforeEach(async function () {
simctl = new Simctl();
simctl.udid = await simctl.createDevice('node-simctl test', DEVICE_NAME, sdk);
});
afterEach(async function () {
if (simctl.udid) {
await simctl.deleteDevice();
simctl.udid = null;
}
});
it('should get devices', async function () {
const sdkDevices = await simctl.getDevices(sdk);
expect(sdkDevices.map((item) => item.name)).to.include(name);
});
it('should erase devices', async function () {
await simctl.eraseDevice(16000);
});
it('should delete devices', async function () {
await simctl.deleteDevice();
const sdkDevices = await simctl.getDevices(sdk);
expect(sdkDevices.map((item) => item.udid)).to.not.include(simctl.udid);
// so we do not delete again
simctl.udid = null;
});
it('should not fail to shutdown a shutdown simulator', async function () {
await expect(simctl.shutdownDevice()).to.eventually.not.be.rejected;
});
});
it('should return a nice error for invalid usage', async function () {
let err: Error | null = null;
try {
await simctl.createDevice('foo', 'bar', 'baz');
} catch (e) {
err = e as Error;
}
expect(err).to.exist;
expect(err!.message).to.include(`Unable to parse version 'baz'`);
});
describe('on running Simulator', function () {
if (process.env.TRAVIS) {
this.retries(3);
}
let major: number, minor: number;
before(async function () {
const version = await xcode.getVersion(true);
if (typeof version === 'string') {
return this.skip();
}
({major, minor} = version);
if (major < 8 || (major === 8 && minor < 1)) {
return this.skip();
}
const sdk = process.env.IOS_SDK || validSdks.at(-1);
simctl.udid = await simctl.createDevice('runningSimTest', DEVICE_NAME, sdk!);
await simctl.bootDevice();
await simctl.startBootMonitor({timeout: MOCHA_TIMEOUT});
});
after(async function () {
if (simctl.udid) {
try {
await simctl.shutdownDevice();
} catch {}
await simctl.deleteDevice();
simctl.udid = null;
}
});
describe('startBootMonitor', function () {
it('should be fulfilled if the simulator is already booted', async function () {
if (major < 8 || (major === 8 && minor < 1)) {
return this.skip();
}
await expect(simctl.startBootMonitor()).to.eventually.be.fulfilled;
});
it('should fail to monitor booting of non-existing simulator', async function () {
if (major < 8 || (major === 8 && minor < 1)) {
return this.skip();
}
const udid = simctl.udid;
try {
simctl.udid = 'blabla';
await expect(simctl.startBootMonitor({timeout: 1000})).to.eventually.be.rejected;
} finally {
simctl.udid = udid;
}
});
});
describe('pasteboard', function () {
let pbRetries = 0;
before(function () {
if (major < 8 || (major === 8 && minor < 1)) {
return this.skip();
}
if (major === 9) {
if (process.env.TRAVIS) {
return this.skip();
}
// TODO: recheck when full Xcode 9 comes out to see if pasteboard works better
pbRetries = 200;
this.timeout(200 * 1000 * 2);
}
});
it('should set and get the content of the pasteboard', async function () {
const pbContent = 'blablabla';
const encoding = 'ascii';
await retryInterval(pbRetries, 1000, async () => {
await simctl.setPasteboard(pbContent, encoding);
expect(await simctl.getPasteboard(encoding)).to.eql(pbContent);
});
});
});
describe('add media', function () {
const BASE64_PNG =
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
let picturePath: string | undefined;
before(async function () {
if (major < 8 || (major === 8 && minor < 1)) {
return this.skip();
}
picturePath = path.join(os.tmpdir(), `${randomUUID()}.png`);
await fs.writeFile(
picturePath,
Buffer.from(BASE64_PNG, 'base64').toString('binary'),
'binary',
);
});
after(async function () {
if (picturePath) {
await rimraf(picturePath);
}
});
it('should add media files', async function () {
expect((await simctl.addMedia(picturePath!)).code).to.eql(0);
});
});
describe('appInfo', function () {
it('should extract applications information', async function () {
const appInfo = await simctl.appInfo('com.apple.springboard');
expect(appInfo.ApplicationType).to.equal('Hidden');
});
it('should throw an error if the app is not installed', async function () {
await expect(simctl.appInfo('com.apple.notinstalled')).to.be.eventually.rejected;
});
});
describe('getEnv', function () {
it('should get env variable value', async function () {
const udid = await simctl.getEnv('SIMULATOR_UDID');
expect(udid!.length).to.be.above(0);
});
it('should return null if no var is found', async function () {
const udid = await simctl.getEnv('SIMULATOR_UDD');
expect(udid).to.be.null;
});
});
describe('getDeviceTypes', function () {
it('should get device types', async function () {
const deviceTypes = await simctl.getDeviceTypes();
expect(deviceTypes).to.have.length;
expect(deviceTypes.length).to.be.above(0);
// at least one type, no matter the version of Xcode, should be an iPhone
expect(deviceTypes.filter((el) => el.includes('iPhone')).length).to.be.above(1);
});
});
describe('list', function () {
it('should get everything from xcrun simctl list', async function () {
const fullList = await simctl.list();
expect(fullList).to.have.property('devicetypes');
expect(fullList).to.have.property('runtimes');
expect(fullList).to.have.property('devices');
expect(fullList).to.have.property('pairs');
expect(fullList.devicetypes.length).to.be.above(1);
// at least one type, no matter the version of Xcode, should be an iPhone
expect(
fullList.devicetypes.filter((el) => el.identifier.includes('iPhone')).length,
).to.be.above(0);
// at least one runtime should be iOS
expect(fullList.runtimes.filter((el) => el.identifier.includes('iOS')).length).to.be.above(
0,
);
});
});
describe('getScreenshot', function () {
it('should get a base64 string', async function () {
const image = await simctl.getScreenshot();
expect(Buffer.from(image, 'base64').toString('base64') === image).to.be.true;
});
});
describe('pushNotification', function () {
it('should not throw an error when sending a push notification', async function () {
if (process.env.CI) {
// This test is unstable in CI env
return this.skip();
}
const payload = {
'Simulator Target Bundle': 'com.apple.Preferences',
aps: {
alert: 'This is a simulated notification!',
badge: 3,
sound: 'default',
},
};
await expect(simctl.pushNotification(payload)).to.be.fulfilled;
});
});
});
});