appium-xcuitest-driver 10.13.0 → 10.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/build/lib/commands/certificate.d.ts +14 -19
  3. package/build/lib/commands/certificate.d.ts.map +1 -1
  4. package/build/lib/commands/certificate.js +24 -31
  5. package/build/lib/commands/certificate.js.map +1 -1
  6. package/build/lib/commands/element.d.ts +83 -67
  7. package/build/lib/commands/element.d.ts.map +1 -1
  8. package/build/lib/commands/element.js +111 -134
  9. package/build/lib/commands/element.js.map +1 -1
  10. package/build/lib/commands/file-movement.d.ts +31 -42
  11. package/build/lib/commands/file-movement.d.ts.map +1 -1
  12. package/build/lib/commands/file-movement.js +146 -205
  13. package/build/lib/commands/file-movement.js.map +1 -1
  14. package/build/lib/commands/find.d.ts +20 -12
  15. package/build/lib/commands/find.d.ts.map +1 -1
  16. package/build/lib/commands/find.js +27 -65
  17. package/build/lib/commands/find.js.map +1 -1
  18. package/build/lib/commands/navigation.d.ts.map +1 -1
  19. package/build/lib/commands/navigation.js +12 -14
  20. package/build/lib/commands/navigation.js.map +1 -1
  21. package/build/lib/commands/web.d.ts.map +1 -1
  22. package/build/lib/commands/web.js +10 -1
  23. package/build/lib/commands/web.js.map +1 -1
  24. package/lib/commands/{certificate.js → certificate.ts} +55 -50
  25. package/lib/commands/element.ts +419 -0
  26. package/lib/commands/{file-movement.js → file-movement.ts} +212 -235
  27. package/lib/commands/find.ts +277 -0
  28. package/lib/commands/navigation.js +20 -14
  29. package/lib/commands/web.ts +9 -1
  30. package/npm-shrinkwrap.json +2 -2
  31. package/package.json +1 -1
  32. package/lib/commands/element.js +0 -423
  33. package/lib/commands/find.js +0 -205
@@ -9,6 +9,9 @@ import {exec} from 'teen_process';
9
9
  import {findAPortNotInUse, checkPortStatus} from 'portscanner';
10
10
  import {Pyidevice} from '../device/clients/py-ios-device-client';
11
11
  import {errors} from 'appium/driver';
12
+ import type {Simulator} from 'appium-ios-simulator';
13
+ import type {XCUITestDriver} from '../driver';
14
+ import type {CertificateList} from './types';
12
15
 
13
16
  const CONFIG_EXTENSION = 'mobileconfig';
14
17
  const HOST_PORT_RANGE = [38200, 38299];
@@ -62,11 +65,8 @@ const OPEN_SSL_PATTERN = /,\sCN\s=\s([^,]+)/;
62
65
 
63
66
  /**
64
67
  * Parses the common name of the certificate from the given string.
65
- *
66
- * @param {string} stringCertificate
67
- * @returns {string} The common name of the certificate
68
68
  */
69
- export function parseCommonName(stringCertificate) {
69
+ export function parseCommonName(stringCertificate: string): string {
70
70
  const result = [LIBRE_SSL_PATTERN, OPEN_SSL_PATTERN].reduce((acc, r) => {
71
71
  if (acc) {
72
72
  return acc;
@@ -87,22 +87,28 @@ export function parseCommonName(stringCertificate) {
87
87
  * certificates on Simulator over CLI.
88
88
  *
89
89
  * On real devices (or simulators before Xcode SDK 11.4), Apple provides no "official" way to do this via the command line. In such a case (and also as a fallback if CLI setup fails), this method tries to wrap the certificate into `.mobileconfig` format, then deploys the wrapped file to the internal HTTP server so that it can be opened via mobile Safari. This command then goes through the profile installation procedure by clicking the necessary buttons using WebDriverAgent.
90
- * @param {string} content - Base64-encoded content of the public certificate in [PEM](https://knowledge.digicert.com/quovadis/ssl-certificates/ssl-general-topics/what-is-pem-format.html) format
91
- * @param {string} [commonName] - Common name of the certificate. If this is not set, the command will try to parse it from the provided `content`.
92
- * @param {boolean} isRoot - Defines where the certificate should be installed; either the Trusted Root Store (`true`) or the Keychain (`false`). On environments other than Xcode 11.4+ Simulator, this option is ignored.
93
- * @returns {Promise<string|void>} The content of the generated `.mobileconfig` file as
90
+ * @param content - Base64-encoded content of the public certificate in [PEM](https://knowledge.digicert.com/quovadis/ssl-certificates/ssl-general-topics/what-is-pem-format.html) format
91
+ * @param commonName - Common name of the certificate. If this is not set, the command will try to parse it from the provided `content`.
92
+ * @param isRoot - Defines where the certificate should be installed; either the Trusted Root Store (`true`) or the Keychain (`false`). On environments other than Xcode 11.4+ Simulator, this option is ignored.
93
+ * @returns The content of the generated `.mobileconfig` file as
94
94
  * a base64-encoded string. This config might be useful for debugging purposes. If the certificate has been successfully set via CLI, then nothing is returned.
95
- * @this {XCUITestDriver}
96
95
  */
97
- export async function mobileInstallCertificate(content, commonName, isRoot = true) {
96
+ export async function mobileInstallCertificate(
97
+ this: XCUITestDriver,
98
+ content: string,
99
+ commonName?: string,
100
+ isRoot = true,
101
+ ): Promise<string | void> {
98
102
  if (_.isEmpty(content)) {
99
103
  throw new Error('Certificate content should not be empty');
100
104
  }
101
105
 
102
106
  if (this.isSimulator()) {
103
107
  try {
104
- const methodName = isRoot ? 'addRootCertificate' : 'addCertificate';
105
- await /** @type {import('appium-ios-simulator').Simulator} */ (this.device).simctl[methodName](Buffer.from(content, 'base64').toString(), {
108
+ const methodName: 'addRootCertificate' | 'addCertificate' = isRoot
109
+ ? 'addRootCertificate'
110
+ : 'addCertificate';
111
+ await (this.device as Simulator).simctl[methodName](Buffer.from(content, 'base64').toString(), {
106
112
  raw: true,
107
113
  });
108
114
  return;
@@ -188,11 +194,12 @@ export async function mobileInstallCertificate(content, commonName, isRoot = tru
188
194
  }
189
195
  }
190
196
  } else {
191
- await /** @type {import('appium-ios-simulator').Simulator} */ (this.device).openUrl(certUrl);
197
+ await (this.device as Simulator).openUrl(certUrl);
192
198
  }
193
199
 
194
200
  let isCertAlreadyInstalled = false;
195
- if (util.compareVersions(/** @type {string} */ (this.opts.platformVersion), '>=', '12.2')) {
201
+ const platformVersion = this.opts.platformVersion ?? '';
202
+ if (util.compareVersions(platformVersion, '>=', '12.2')) {
196
203
  if (await installPost122Certificate(this, cn)) {
197
204
  await clickElement(this, Settings.Profile);
198
205
  await trustCertificateInPreferences(this, cn);
@@ -238,17 +245,19 @@ export async function mobileInstallCertificate(content, commonName, isRoot = tru
238
245
  *
239
246
  * @see https://github.com/YueChen-C/py-ios-device
240
247
  * @since 4.19.2
241
- * @param {string} name - Name of the profile
242
- * @returns {Promise<string>} Returns status acknowledgment status if
243
- * tht certificate is successfully removed or 'None' (basically just
244
- * forwards the original pyidevice output)
248
+ * @param name - Name of the profile
249
+ * @returns Status acknowledgment if
250
+ * the certificate is successfully removed or 'None' (forwards pyidevice output)
245
251
  * @throws {Error} If attempting to remove certificates for a simulated device or if `py-ios-device` is not installed
246
252
  * @group Real Device Only
247
253
  */
248
- export async function mobileRemoveCertificate(name) {
254
+ export async function mobileRemoveCertificate(this: XCUITestDriver, name: string): Promise<string> {
249
255
  if (!this.isRealDevice()) {
250
256
  throw new errors.NotImplementedError('This extension is only supported on real devices');
251
257
  }
258
+ if (!this.opts.udid) {
259
+ throw new Error('udid capability is required');
260
+ }
252
261
  const client = new Pyidevice({
253
262
  udid: this.opts.udid,
254
263
  log: this.log,
@@ -263,11 +272,10 @@ export async function mobileRemoveCertificate(name) {
263
272
  * This only works _if and only if_ `py-ios-device` is installed on the same machine Appium is running on.
264
273
  * @since 4.10.0
265
274
  * @see https://github.com/YueChen-C/py-ios-device
266
- * @returns {Promise<import('./types').CertificateList>} An object describing the certificates installed on the real device.
275
+ * @returns An object describing the certificates installed on the real device.
267
276
  * @throws {Error} If attempting to list certificates for a simulated device or if `py-ios-device` is not installed
268
- * @this {XCUITestDriver}
269
277
  */
270
- export async function mobileListCertificates() {
278
+ export async function mobileListCertificates(this: XCUITestDriver): Promise<CertificateList> {
271
279
  if (!this.isRealDevice()) {
272
280
  throw new errors.NotImplementedError('This extension is only supported on real devices');
273
281
  }
@@ -287,9 +295,9 @@ export async function mobileListCertificates() {
287
295
  * Extracts the common name of the certificate from the given buffer.
288
296
  *
289
297
  * @param {Buffer} certBuffer
290
- * @returns {Promise<string>} The common name of the certificate
298
+ * @returns The common name of the certificate
291
299
  */
292
- async function extractCommonName(certBuffer) {
300
+ async function extractCommonName(certBuffer: Buffer): Promise<string> {
293
301
  const tempCert = await tempDir.open({
294
302
  prefix: 'cert',
295
303
  suffix: '.cer',
@@ -315,12 +323,12 @@ async function extractCommonName(certBuffer) {
315
323
  * for more details on such profiles.
316
324
  *
317
325
  * @param {Buffer} certBuffer - The actual content of PEM certificate encoded into NodeJS buffer
318
- * @param {string} commonName - Certificate's common name
326
+ * @param commonName - Certificate's common name
319
327
  * @returns {Object} The encoded structure of the given certificate, which is ready to be passed
320
328
  * as an argument to plist builder
321
329
  * @throws {Error} If the given certificate cannot be parsed
322
330
  */
323
- function toMobileConfig(certBuffer, commonName) {
331
+ function toMobileConfig(certBuffer: Buffer, commonName: string): Record<string, any> {
324
332
  const getUUID = () => util.uuidV4().toUpperCase();
325
333
  const contentUuid = getUUID();
326
334
  return {
@@ -345,8 +353,12 @@ function toMobileConfig(certBuffer, commonName) {
345
353
  };
346
354
  }
347
355
 
348
- async function clickElement(driver, locator, options = {}) {
349
- let element = null;
356
+ async function clickElement(
357
+ driver: XCUITestDriver,
358
+ locator: {type: string; value: string},
359
+ options: {timeout?: number; skipIfInvisible?: boolean} = {},
360
+ ): Promise<boolean> {
361
+ let element: any = null;
350
362
  const {timeout = 5000, skipIfInvisible = false} = options;
351
363
  const lookupDelay = 500;
352
364
  try {
@@ -365,7 +377,7 @@ async function clickElement(driver, locator, options = {}) {
365
377
  return true;
366
378
  }
367
379
 
368
- async function installPre122Certificate(driver) {
380
+ async function installPre122Certificate(driver: XCUITestDriver): Promise<boolean> {
369
381
  // Accept Safari alert
370
382
  await clickElement(driver, Button.Allow, {
371
383
  // certificate load might take some time on slow machines
@@ -393,7 +405,7 @@ async function installPre122Certificate(driver) {
393
405
  return true;
394
406
  }
395
407
 
396
- async function trustCertificateInPreferences(driver, name) {
408
+ async function trustCertificateInPreferences(driver: XCUITestDriver, name: string): Promise<void> {
397
409
  await clickElement(driver, Settings.General);
398
410
  await clickElement(driver, Settings.About);
399
411
  const switchLocator = {
@@ -401,14 +413,12 @@ async function trustCertificateInPreferences(driver, name) {
401
413
  value: `**/XCUIElementTypeCell[\`label == '${name}'\`]/**/XCUIElementTypeSwitch`,
402
414
  };
403
415
  await retry(5, async () => {
404
- await driver.mobileSwipe({
405
- element: await driver.findNativeElementOrElements(
406
- 'class name',
407
- 'XCUIElementTypeTable',
408
- false,
409
- ),
410
- direction: 'up',
411
- });
416
+ const tableEl = await driver.findNativeElementOrElements(
417
+ 'class name',
418
+ 'XCUIElementTypeTable',
419
+ false,
420
+ );
421
+ await driver.mobileSwipe('up', undefined, tableEl);
412
422
  await clickElement(driver, Settings.Certificate_Trust_Settings, {
413
423
  timeout: 500,
414
424
  });
@@ -433,7 +443,7 @@ async function trustCertificateInPreferences(driver, name) {
433
443
  }
434
444
  }
435
445
 
436
- async function installPost122Certificate(driver, name) {
446
+ async function installPost122Certificate(driver: XCUITestDriver, name: string): Promise<boolean> {
437
447
  // Accept Safari alert
438
448
  await clickElement(driver, Button.Allow, {
439
449
  // certificate load might take some time on slow machines
@@ -466,14 +476,12 @@ async function installPost122Certificate(driver, name) {
466
476
  break;
467
477
  }
468
478
 
469
- await driver.mobileSwipe({
470
- element: await driver.findNativeElementOrElements(
471
- 'class name',
472
- 'XCUIElementTypeTable',
473
- false,
474
- ),
475
- direction: 'up',
476
- });
479
+ const tableEl = await driver.findNativeElementOrElements(
480
+ 'class name',
481
+ 'XCUIElementTypeTable',
482
+ false,
483
+ );
484
+ await driver.mobileSwipe('up', undefined, tableEl);
477
485
  }
478
486
  if (!isCertFound) {
479
487
  throw new Error(`'${name}' cannot be found in the certificates list`);
@@ -498,6 +506,3 @@ async function installPost122Certificate(driver, name) {
498
506
  return true;
499
507
  }
500
508
 
501
- /**
502
- * @typedef {import('../driver').XCUITestDriver} XCUITestDriver
503
- */
@@ -0,0 +1,419 @@
1
+ import _ from 'lodash';
2
+ import {errors} from 'appium/driver';
3
+ import {util} from 'appium/support';
4
+ import type {Element, Position, Size, Rect} from '@appium/types';
5
+ import type {XCUITestDriver} from '../driver';
6
+ import type {AtomsElement} from './types';
7
+
8
+ /**
9
+ * Checks whether an element is displayed.
10
+ *
11
+ * @param el - Element or element ID
12
+ */
13
+ export async function elementDisplayed(this: XCUITestDriver, el: Element | string): Promise<boolean> {
14
+ const elementId = util.unwrapElement(el);
15
+ if (this.isWebContext()) {
16
+ const atomsElement = this.getAtomsElement(elementId);
17
+ return await this.executeAtom('is_displayed', [atomsElement]) as boolean;
18
+ }
19
+ return await this.proxyCommand(`/element/${elementId}/displayed`, 'GET') as boolean;
20
+ }
21
+
22
+ /**
23
+ * Checks whether an element is enabled.
24
+ *
25
+ * @param el - Element or element ID
26
+ */
27
+ export async function elementEnabled(this: XCUITestDriver, el: Element | string): Promise<boolean> {
28
+ const elementId = util.unwrapElement(el);
29
+ if (this.isWebContext()) {
30
+ const atomsElement = this.getAtomsElement(elementId);
31
+ return await this.executeAtom('is_enabled', [atomsElement]) as boolean;
32
+ }
33
+ return await this.proxyCommand(`/element/${elementId}/enabled`, 'GET') as boolean;
34
+ }
35
+
36
+ /**
37
+ * Checks whether an element is selected.
38
+ *
39
+ * @param el - Element or element ID
40
+ */
41
+ export async function elementSelected(this: XCUITestDriver, el: Element | string): Promise<boolean> {
42
+ const elementId = util.unwrapElement(el);
43
+ if (this.isWebContext()) {
44
+ const atomsElement = this.getAtomsElement(elementId);
45
+ return await this.executeAtom('is_selected', [atomsElement]) as boolean;
46
+ }
47
+ return await this.proxyCommand(`/element/${elementId}/selected`, 'GET') as boolean;
48
+ }
49
+
50
+ /**
51
+ * Gets the tag/name of an element.
52
+ *
53
+ * @param el - Element or element ID
54
+ */
55
+ export async function getName(this: XCUITestDriver, el: Element | string): Promise<string> {
56
+ const elementId = util.unwrapElement(el);
57
+ if (this.isWebContext()) {
58
+ const atomsElement = this.getAtomsElement(elementId);
59
+ const script = 'return arguments[0].tagName.toLowerCase()';
60
+ return await this.executeAtom('execute_script', [script, [atomsElement]]) as string;
61
+ }
62
+ return await this.proxyCommand(`/element/${elementId}/name`, 'GET') as string;
63
+ }
64
+
65
+ /**
66
+ * Gets a native attribute (non-web) from an element.
67
+ *
68
+ * @param attribute - Attribute name
69
+ * @param el - Element or element ID
70
+ */
71
+ export async function getNativeAttribute(
72
+ this: XCUITestDriver,
73
+ attribute: string,
74
+ el: Element | string,
75
+ ): Promise<string | null> {
76
+ if (attribute === 'contentSize') {
77
+ return await this.getContentSize(el);
78
+ }
79
+
80
+ const elementId = util.unwrapElement(el);
81
+ let value = await this.proxyCommand(`/element/${elementId}/attribute/${attribute}`, 'GET') as
82
+ | string
83
+ | number
84
+ | null
85
+ | undefined
86
+ | boolean;
87
+ if ([0, 1].includes(value as number)) {
88
+ value = !!value;
89
+ }
90
+ return _.isNull(value) || _.isString(value) ? value : JSON.stringify(value);
91
+ }
92
+
93
+ /**
94
+ * Gets an element attribute (web or native).
95
+ *
96
+ * @param attribute - Attribute name
97
+ * @param el - Element or element ID
98
+ */
99
+ export async function getAttribute(
100
+ this: XCUITestDriver,
101
+ attribute: string,
102
+ el: Element | string,
103
+ ): Promise<string | null> {
104
+ const elementId = util.unwrapElement(el);
105
+ if (!this.isWebContext()) {
106
+ return await this.getNativeAttribute(attribute, elementId);
107
+ }
108
+ const atomsElement = this.getAtomsElement(elementId);
109
+ return await this.executeAtom('get_attribute_value', [atomsElement, attribute]) as string | null;
110
+ }
111
+
112
+ /**
113
+ * Gets an element property (web) or native attribute fallback.
114
+ *
115
+ * @param property - Property name
116
+ * @param el - Element or element ID
117
+ */
118
+ export async function getProperty(
119
+ this: XCUITestDriver,
120
+ property: string,
121
+ el: Element | string,
122
+ ): Promise<string | null> {
123
+ const elementId = util.unwrapElement(el);
124
+ if (!this.isWebContext()) {
125
+ return await this.getNativeAttribute(property, elementId);
126
+ }
127
+ const atomsElement = this.getAtomsElement(elementId);
128
+ return await this.executeAtom('get_attribute_value', [atomsElement, property]) as string | null;
129
+ }
130
+
131
+ /**
132
+ * Gets the text content of an element.
133
+ *
134
+ * @param el - Element or element ID
135
+ */
136
+ export async function getText(this: XCUITestDriver, el: Element | string): Promise<string> {
137
+ const elementId = util.unwrapElement(el);
138
+ if (!this.isWebContext()) {
139
+ return await this.proxyCommand(`/element/${elementId}/text`, 'GET') as string;
140
+ }
141
+ const atomsElement = this.getAtomsElement(elementId);
142
+ return await this.executeAtom('get_text', [atomsElement]) as string;
143
+ }
144
+
145
+ /**
146
+ * Gets the bounding rect of an element.
147
+ *
148
+ * @param el - Element or element ID
149
+ */
150
+ export async function getElementRect(this: XCUITestDriver, el: Element | string): Promise<Rect> {
151
+ if (this.isWebContext()) {
152
+ const {x, y} = await this.getLocation(el);
153
+ const {width, height} = await this.getSize(el);
154
+ return {x, y, width, height};
155
+ }
156
+ const elementId = util.unwrapElement(el);
157
+ return await this.getNativeRect(elementId);
158
+ }
159
+
160
+ /**
161
+ * Gets the top-left location of an element.
162
+ *
163
+ * @param elementId - Element or element ID
164
+ */
165
+ export async function getLocation(this: XCUITestDriver, elementId: Element | string): Promise<Position> {
166
+ const el = util.unwrapElement(elementId);
167
+ if (this.isWebContext()) {
168
+ const atomsElement = this.getAtomsElement(el);
169
+ const loc = await this.executeAtom('get_top_left_coordinates', [atomsElement]) as Position;
170
+ if (this.opts.absoluteWebLocations) {
171
+ const script =
172
+ 'return [' +
173
+ 'Math.max(window.pageXOffset,document.documentElement.scrollLeft,document.body.scrollLeft),' +
174
+ 'Math.max(window.pageYOffset,document.documentElement.scrollTop,document.body.scrollTop)];';
175
+ const [xOffset, yOffset] = await this.execute(script) as [number, number];
176
+ loc.x += xOffset;
177
+ loc.y += yOffset;
178
+ }
179
+ return loc;
180
+ }
181
+ const rect = await this.getElementRect(el);
182
+ return {x: rect.x, y: rect.y};
183
+ }
184
+
185
+ /**
186
+ * Alias for getLocation.
187
+ *
188
+ * @param elementId - Element or element ID
189
+ */
190
+ export async function getLocationInView(this: XCUITestDriver, elementId: Element | string): Promise<Position> {
191
+ return await this.getLocation(elementId);
192
+ }
193
+
194
+ /**
195
+ * Gets the size of an element.
196
+ *
197
+ * @param el - Element or element ID
198
+ */
199
+ export async function getSize(this: XCUITestDriver, el: Element | string): Promise<Size> {
200
+ const elementId = util.unwrapElement(el);
201
+ if (this.isWebContext()) {
202
+ return await this.executeAtom('get_size', [this.getAtomsElement(elementId)]) as Size;
203
+ }
204
+ const rect = await this.getElementRect(elementId);
205
+ return {width: rect.width, height: rect.height};
206
+ }
207
+
208
+ /**
209
+ * Legacy alias for setValue; always types via keyboard.
210
+ *
211
+ * @param value - Value to set
212
+ * @param el - Element or element ID
213
+ */
214
+ export async function setValueImmediate(
215
+ this: XCUITestDriver,
216
+ value: string | string[] | number,
217
+ el: Element | string,
218
+ ): Promise<void> {
219
+ this.log.info(
220
+ 'There is currently no way to bypass typing using XCUITest. Setting value through keyboard',
221
+ );
222
+ await this.setValue(value, el);
223
+ }
224
+
225
+ /**
226
+ * Sets an element value (native or web).
227
+ *
228
+ * @param value - Value to set
229
+ * @param el - Element or element ID
230
+ */
231
+ export async function setValue(
232
+ this: XCUITestDriver,
233
+ value: string | string[] | number,
234
+ el: Element | string,
235
+ ): Promise<void> {
236
+ const elementId = util.unwrapElement(el);
237
+ if (!this.isWebContext()) {
238
+ await this.proxyCommand(`/element/${elementId}/value`, 'POST', {
239
+ value: prepareInputValue(value),
240
+ });
241
+ return;
242
+ }
243
+
244
+ const atomsElement = this.getAtomsElement(elementId);
245
+ await this.executeAtom('click', [atomsElement]);
246
+
247
+ if (this.opts.sendKeyStrategy !== 'oneByOne') {
248
+ await this.setValueWithWebAtom(atomsElement, value);
249
+ return;
250
+ }
251
+ for (const char of prepareInputValue(value)) {
252
+ await this.setValueWithWebAtom(atomsElement, char);
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Types text into a web element using atoms.
258
+ *
259
+ * @param atomsElement - Target atoms element
260
+ * @param value - Text to type
261
+ */
262
+ export async function setValueWithWebAtom(
263
+ this: XCUITestDriver,
264
+ atomsElement: AtomsElement<string>,
265
+ value: string | string[] | number,
266
+ ): Promise<void> {
267
+ await this.executeAtom('type', [atomsElement, value]);
268
+
269
+ if (this.opts.skipTriggerInputEventAfterSendkeys) {
270
+ return;
271
+ }
272
+
273
+ function triggerInputEvent(input: EventTarget & {_valueTracker?: any}) {
274
+ const lastValue = '';
275
+ const event = new Event('input', {bubbles: true});
276
+ const tracker = input._valueTracker;
277
+ if (tracker) {
278
+ tracker.setValue(lastValue);
279
+ }
280
+ input.dispatchEvent(event);
281
+ }
282
+
283
+ const scriptAsString = `return (${triggerInputEvent}).apply(null, arguments)`;
284
+ await this.executeAtom('execute_script', [scriptAsString, [atomsElement]]);
285
+ }
286
+
287
+ /**
288
+ * Sends raw key sequences via WDA.
289
+ *
290
+ * @param value - Keys to send
291
+ */
292
+ export async function keys(this: XCUITestDriver, value: string[] | string | number): Promise<void> {
293
+ await this.proxyCommand('/wda/keys', 'POST', {
294
+ value: prepareInputValue(value),
295
+ });
296
+ }
297
+
298
+ /**
299
+ * Clears the contents of an element.
300
+ *
301
+ * @param el - Element or element ID
302
+ */
303
+ export async function clear(this: XCUITestDriver, el: Element | string): Promise<void> {
304
+ const elementId = util.unwrapElement(el);
305
+ if (this.isWebContext()) {
306
+ const atomsElement = this.getAtomsElement(elementId);
307
+ await this.executeAtom('clear', [atomsElement]);
308
+ return;
309
+ }
310
+ await this.proxyCommand(`/element/${elementId}/clear`, 'POST');
311
+ }
312
+
313
+ /**
314
+ * Gets content size for table/collection views (native only).
315
+ *
316
+ * @param el - Element or element ID
317
+ */
318
+ export async function getContentSize(this: XCUITestDriver, el: Element | string): Promise<string> {
319
+ if (this.isWebContext()) {
320
+ throw new errors.NotYetImplementedError(
321
+ 'Support for getContentSize for web context is not yet implemented. Please contact an Appium dev',
322
+ );
323
+ }
324
+
325
+ const type = await this.getAttribute('type', el);
326
+
327
+ if (type !== 'XCUIElementTypeTable' && type !== 'XCUIElementTypeCollectionView') {
328
+ throw new Error(
329
+ `Can't get content size for type '${type}', only for tables and collection views`,
330
+ );
331
+ }
332
+ let locator = '*';
333
+ if (type === 'XCUIElementTypeTable') {
334
+ locator = 'XCUIElementTypeCell';
335
+ }
336
+
337
+ let contentHeight = 0;
338
+ const children = await this.findElOrEls('class chain', locator, true, el);
339
+ if (children.length === 1) {
340
+ const rect = await this.getElementRect(children[0]);
341
+ contentHeight = rect.height;
342
+ } else if (children.length) {
343
+ switch (type) {
344
+ case 'XCUIElementTypeTable': {
345
+ const firstRect = await this.getElementRect(_.head(children) as Element);
346
+ const lastRect = await this.getElementRect(_.last(children) as Element);
347
+ contentHeight = lastRect.y + lastRect.height - firstRect.y;
348
+ break;
349
+ }
350
+ case 'XCUIElementTypeCollectionView': {
351
+ let elsInRow = 1;
352
+ const firstRect = await this.getElementRect(_.head(children) as Element);
353
+ const initialRects = [firstRect];
354
+ for (let i = 1; i < children.length; i++) {
355
+ const rect = await this.getElementRect(children[i] as Element);
356
+ initialRects.push(rect);
357
+ if (rect.y !== firstRect.y) {
358
+ elsInRow = i;
359
+ break;
360
+ }
361
+ }
362
+ const spaceBetweenEls =
363
+ initialRects[elsInRow].y -
364
+ initialRects[elsInRow - 1].y -
365
+ initialRects[elsInRow - 1].height;
366
+ const numRows = Math.ceil(children.length / elsInRow);
367
+ contentHeight = numRows * firstRect.height + spaceBetweenEls * (numRows - 1);
368
+ break;
369
+ }
370
+ default:
371
+ throw new Error(
372
+ `Programming error: type '${type}' was not valid but should have already been rejected`,
373
+ );
374
+ }
375
+ }
376
+ const size = await this.getSize(el);
377
+ const origin = await this.getLocationInView(el);
378
+ return JSON.stringify({
379
+ width: size.width,
380
+ height: size.height,
381
+ top: origin.y,
382
+ left: origin.x,
383
+ scrollableOffset: contentHeight,
384
+ });
385
+ }
386
+
387
+ /**
388
+ * Gets the native rect of an element (no web fallback).
389
+ *
390
+ * @param el - Element or element ID
391
+ */
392
+ export async function getNativeRect(this: XCUITestDriver, el: Element | string): Promise<Rect> {
393
+ const elementId = util.unwrapElement(el);
394
+ return await this.proxyCommand(`/element/${elementId}/rect`, 'GET') as Rect;
395
+ }
396
+
397
+ function prepareInputValue(inp: string | string[] | number): string[] {
398
+ if (![_.isArray, _.isString, _.isFinite].some((f) => f(inp))) {
399
+ throw new Error(
400
+ `Only strings, numbers and arrays are supported as input arguments. ` +
401
+ `Received: ${JSON.stringify(inp)}`,
402
+ );
403
+ }
404
+
405
+ if (_.isArray(inp)) {
406
+ inp = inp.join('');
407
+ } else if (_.isFinite(inp)) {
408
+ inp = `${inp}`;
409
+ }
410
+ return [...String(inp)].map((k) => {
411
+ if (['\uE006', '\uE007'].includes(k)) {
412
+ return '\n';
413
+ }
414
+ if (['\uE003', '\ue017'].includes(k)) {
415
+ return '\b';
416
+ }
417
+ return k;
418
+ });
419
+ }