forked from webdriverio/appium-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.ts
77 lines (68 loc) · 3.5 KB
/
Utils.ts
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
/**
* Get the time difference in seconds
*/
export function timeDifference (string: string, start:number, end:number) {
const elapsed = (end - start) / 1000;
console.log(`${string} It took ${elapsed} seconds.`);
}
/**
* Create a cross platform solution for opening a deep link
*/
export async function openDeepLinkUrl(url:string) {
const prefix = 'wdio://';
if (driver.isAndroid) {
// Life is so much easier
return driver.execute('mobile:deepLink', {
url: `${ prefix }${ url }`,
package: 'com.wdiodemoapp',
});
}
// We can use `driver.url` on iOS simulators, but not on iOS real devices. The reason is that iOS real devices
// open Siri when you call `driver.url('')` to use a deep link. This means that real devices need to have a different implementation
// then iOS sims
// iOS sims and real devices can be distinguished by their UDID. Based on these sources there is a diff in the UDIDS
// - https://blog.diawi.com/2018/10/15/2018-apple-devices-and-their-new-udid-format/
// - https://www.theiphonewiki.com/wiki/UDID
// iOS sims have more than 1 `-` in the UDID and the UDID is being
const simulatorRegex = new RegExp('(.*-.*){2,}');
// Check if we are a simulator
if ('udid' in driver.capabilities && simulatorRegex.test( driver.capabilities.udid as string )){
await driver.url(`${ prefix }${ url }`);
} else {
// Else we are a real device and we need to take some extra steps
// Launch Safari to open the deep link
await driver.execute('mobile: launchApp', { bundleId: 'com.apple.mobilesafari' });
// Add the deep link url in Safari in the `URL`-field
// This can be 2 different elements, or the button, or the text field
// Use the predicate string because the accessibility label will return 2 different types
// of elements making it flaky to use. With predicate string we can be more precise
const addressBarSelector = 'label == "Address" OR name == "URL"';
const urlFieldSelector = 'type == "XCUIElementTypeTextField" && name CONTAINS "URL"';
const addressBar = $(`-ios predicate string:${ addressBarSelector }`);
const urlField = $(`-ios predicate string:${ urlFieldSelector }`);
// Wait for the url button to appear and click on it so the text field will appear
// iOS 13 now has the keyboard open by default because the URL field has focus when opening the Safari browser
if (!(await driver.isKeyboardShown())) {
await addressBar.waitForDisplayed();
await addressBar.click();
}
// Submit the url and add a break
await urlField.setValue(`${ prefix }${ url }\uE007`);
}
/**
* PRO TIP:
* if you started the iOS device with `autoAcceptAlerts:true` in the capabilities then Appium will auto accept the alert that should
* be shown now. You can then comment out the code below
*/
// Wait for the notification and accept it
// When using an iOS simulator you will only get the pop-up once, all the other times it won't be shown
try {
const openSelector = 'type == \'XCUIElementTypeButton\' && name CONTAINS \'Open\'';
const openButton = $(`-ios predicate string:${ openSelector }`);
// Assumption is made that the alert will be seen within 2 seconds, if not it did not appear
await openButton.waitForDisplayed({ timeout: 2000 });
await openButton.click();
} catch (e) {
// ignore
}
}