-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathFIREGHelper.m
135 lines (114 loc) · 5.02 KB
/
FIREGHelper.m
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
//
// Copyright (c) 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "FIREGHelper.h"
#pragma mark - Helpers
// Returns the coordinates of the element via debug description of the app.
XCUICoordinate *FIRViewCoordinate(XCUIApplication *app, XCUIElement *element);
BOOL FIRSystemAlertShown() {
return FIRSystemAlert().exists;
}
XCUIElement *FIRSystemAlert() {
XCUIApplication *springboard =
[[XCUIApplication alloc] initWithBundleIdentifier:@"com.apple.springboard"];
return [springboard.alerts firstMatch];
}
void FIRWaitTillAlertPresent(NSTimeInterval timeout) {
FIRWaitForVisibleWithTimeout(FIRSystemAlert(), timeout);
}
void FIRWaitTillAlertGone(NSTimeInterval timeout) {
NSPredicate *gone = [NSPredicate predicateWithFormat:@"exists == false"];
FIRWaitForPredicateWithTimeout(gone, FIRSystemAlert(), timeout);
}
void FIRDismissAlertIfOpen(XCUIApplication *app) {
FIRWaitForVisible([app.alerts firstMatch]);
if ([app.alerts firstMatch].exists) {
[app.alerts.buttons[@"OK"] tap];
}
}
BOOL FIRSignedIn(XCUIApplication *app) {
FIRWaitForVisible(app.buttons[@"Sign Out"]);
FIRWaitForVisible(app.buttons[@"Sign In"]);
return !app.buttons[@"Sign In"].exists;
}
void FIRDumpUIHierarchy(XCUIApplication *app) { NSLog(@"Hierarchy: %@", [app debugDescription]); }
NSString *randomString(NSUInteger length) {
NSMutableString *returnString = [NSMutableString stringWithCapacity:length];
NSString *numbers = @"0123456789";
for (int i = 0; i < length; i++) {
[returnString appendFormat:@"%C", [numbers characterAtIndex:arc4random() % [numbers length]]];
}
return returnString;
}
NSString *timestamp() {
NSDate *now = [NSDate date];
NSNumber *seconds = [NSNumber numberWithLong:[now timeIntervalSince1970]];
return [seconds stringValue];
}
void FIRWaitForPredicateWithTimeout(NSPredicate *predicate, XCUIElement *element,
NSUInteger timeout) {
XCTestExpectation *expectation =
[[XCTNSPredicateExpectation alloc] initWithPredicate:predicate object:element];
NSArray *expectationArray = @[ expectation ];
(void)[XCTWaiter waitForExpectations:expectationArray timeout:timeout];
}
void FIRWaitForPredicate(NSPredicate *predicate, XCUIElement *element) {
FIRWaitForPredicateWithTimeout(predicate, element, defaultTimeout);
}
void FIRWaitForVisibleWithTimeout(XCUIElement *element, NSUInteger timeout) {
NSPredicate *visible = [NSPredicate predicateWithFormat:@"exists == true"];
FIRWaitForPredicateWithTimeout(visible, element, timeout);
}
void FIRWaitForVisible(XCUIElement *element) {
FIRWaitForVisibleWithTimeout(element, defaultTimeout);
}
XCUICoordinate *FIRViewCoordinate(XCUIApplication *app, XCUIElement *element) {
// Finds the printable description of the element.
NSString *description = [[element firstMatch] debugDescription];
@try {
NSRange rangeOpen = [description rangeOfString:@"{{" options:NSBackwardsSearch];
NSRange rangeClose = [description rangeOfString:@"}}" options:NSBackwardsSearch];
NSRange subRange = NSMakeRange(rangeOpen.location,
rangeClose.location + rangeClose.length - rangeOpen.location);
NSString *frameCoordinates = [description substringWithRange:subRange];
// Finds the coordinates of the center.
CGRect rect = CGRectFromString(frameCoordinates);
CGVector center = CGVectorMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
return [[app coordinateWithNormalizedOffset:CGVectorMake(0, 0)] coordinateWithOffset:center];
}
// This is an edge case when debug description doesn't contain coordinates information.
@catch (NSException *e) {
NSLog(@"Exception happened when trying to get coordinates");
FIRDumpUIHierarchy(app);
NSLog(@"Use the regular way of obtaining coordinates");
return [element coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
}
}
void FIRTapSafely(XCUIApplication *app, XCUIElement *element) {
XCUICoordinate *coordinates = FIRViewCoordinate(app, element);
NSLog(@"Trying to tap on the coordinates: %@", NSStringFromCGPoint([coordinates screenPoint]));
FIRDumpUIHierarchy(app);
[coordinates tap];
}
void FIRTypeWithPastebin(XCUIApplication *app, XCUIElement *element, NSString *text) {
// Adds the text to pastebin.
[[UIPasteboard generalPasteboard] setString:text];
// Long click on the element.
XCUICoordinate *textViewCoordinate = FIRViewCoordinate(app, element);
[textViewCoordinate pressForDuration:2];
// Pastes the text.
FIRWaitForVisible(app.menuItems[@"Paste"]);
[app.menuItems[@"Paste"] tap];
}