forked from appium/WebDriverAgent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFBAlert.m
More file actions
265 lines (239 loc) · 8.63 KB
/
Copy pathFBAlert.m
File metadata and controls
265 lines (239 loc) · 8.63 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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "FBAlert.h"
#import <XCTest/XCUICoordinate.h>
#import "FBApplication.h"
#import "FBConfiguration.h"
#import "FBErrorBuilder.h"
#import "FBFindElementCommands.h"
#import "FBSpringboardApplication.h"
#import "FBLogger.h"
#import "FBXCodeCompatibility.h"
#import "XCElementSnapshot+FBHelpers.h"
#import "XCElementSnapshot.h"
#import "XCTestManager_ManagerInterface-Protocol.h"
#import "XCUIApplication+FBAlert.h"
#import "XCUICoordinate.h"
#import "XCUIElement+FBClassChain.h"
#import "XCUIElement+FBTap.h"
#import "XCUIElement+FBTyping.h"
#import "XCUIElement+FBUtilities.h"
#import "XCUIElement+FBWebDriverAttributes.h"
#import "XCUIElement.h"
#import "XCUIElementQuery.h"
NSString *const FBAlertObstructingElementException = @"FBAlertObstructingElementException";
@interface FBAlert ()
@property (nonatomic, strong) XCUIApplication *application;
@property (nonatomic, strong, nullable) XCUIElement *element;
@end
@implementation FBAlert
+ (void)throwRequestedItemObstructedByAlertException __attribute__((noreturn))
{
@throw [NSException exceptionWithName:FBAlertObstructingElementException reason:@"Requested element is obstructed by alert or action sheet" userInfo:@{}];
}
+ (instancetype)alertWithApplication:(XCUIApplication *)application
{
FBAlert *alert = [FBAlert new];
alert.application = application;
return alert;
}
+ (instancetype)alertWithElement:(XCUIElement *)element
{
FBAlert *alert = [FBAlert new];
alert.element = element;
alert.application = element.application;
return alert;
}
- (BOOL)isPresent
{
return self.alertElement.exists;
}
- (NSString *)text
{
XCUIElement *alert = self.alertElement;
if (!alert) {
return nil;
}
NSArray<XCUIElement *> *staticTextList = [alert.fb_query descendantsMatchingType:XCUIElementTypeStaticText].allElementsBoundByAccessibilityElement;
NSMutableArray<NSString *> *resultText = [NSMutableArray array];
for (XCUIElement *staticText in staticTextList) {
if (staticText.isWDVisible) {
if (staticText.wdLabel) {
[resultText addObject:[NSString stringWithFormat:@"%@", staticText.wdLabel]];
} else if (staticText.wdValue) {
[resultText addObject:[NSString stringWithFormat:@"%@", staticText.wdValue]];
}
}
}
if (resultText.count) {
return [resultText componentsJoinedByString:@"\n"];
}
// return an empty string to reflect the fact there is an alert, but it does not contain any text
return @"";
}
- (BOOL)typeText:(NSString *)text error:(NSError **)error
{
XCUIElement *alert = self.alertElement;
NSArray<XCUIElement *> *textFields = alert.textFields.allElementsBoundByAccessibilityElement;
NSArray<XCUIElement *> *secureTextFiels = alert.secureTextFields.allElementsBoundByAccessibilityElement;
if (textFields.count + secureTextFiels.count > 1) {
return [[[FBErrorBuilder builder]
withDescriptionFormat:@"The alert contains more than one input field"]
buildError:error];
}
if (0 == textFields.count + secureTextFiels.count) {
return [[[FBErrorBuilder builder]
withDescriptionFormat:@"The alert contains no input fields"]
buildError:error];
}
if (secureTextFiels.count > 0) {
return [secureTextFiels.firstObject fb_typeText:text error:error];
}
return [textFields.firstObject fb_typeText:text error:error];
}
- (NSArray *)buttonLabels
{
NSMutableArray *value = [NSMutableArray array];
XCUIElement *alertElement = self.alertElement;
if (!alertElement) {
return nil;
}
NSArray<XCUIElement *> *buttons = [alertElement.fb_query descendantsMatchingType:XCUIElementTypeButton].allElementsBoundByAccessibilityElement;
for(XCUIElement *button in buttons) {
[value addObject:[button wdLabel]];
}
return value;
}
- (BOOL)acceptWithError:(NSError **)error
{
XCUIElement *alertElement = self.alertElement;
XCUIElement *acceptButton = nil;
if (FBConfiguration.acceptAlertButtonSelector.length) {
NSString *errorReason = nil;
@try {
acceptButton = [[alertElement fb_descendantsMatchingClassChain:FBConfiguration.acceptAlertButtonSelector shouldReturnAfterFirstMatch:YES] firstObject];
} @catch (NSException *ex) {
errorReason = ex.reason;
}
if (nil == acceptButton) {
[FBLogger logFmt:@"Cannot find any match for Accept alert button using the class chain selector '%@'", FBConfiguration.acceptAlertButtonSelector];
if (nil != errorReason) {
[FBLogger logFmt:@"Original error: %@", errorReason];
}
[FBLogger log:@"Will fallback to the default button location algorithm"];
}
}
if (nil == acceptButton) {
NSArray<XCUIElement *> *buttons = [alertElement.fb_query descendantsMatchingType:XCUIElementTypeButton].allElementsBoundByAccessibilityElement;
acceptButton = alertElement.elementType == XCUIElementTypeAlert
? buttons.lastObject
: buttons.firstObject;
}
return nil == acceptButton
? [[[FBErrorBuilder builder]
withDescriptionFormat:@"Failed to find accept button for alert: %@", alertElement]
buildError:error]
: [acceptButton fb_tapWithError:error];
}
- (BOOL)dismissWithError:(NSError **)error
{
XCUIElement *alertElement = self.alertElement;
XCUIElement *dismissButton = nil;
if (FBConfiguration.dismissAlertButtonSelector.length) {
NSString *errorReason = nil;
@try {
dismissButton = [[alertElement fb_descendantsMatchingClassChain:FBConfiguration.dismissAlertButtonSelector shouldReturnAfterFirstMatch:YES] firstObject];
} @catch (NSException *ex) {
errorReason = ex.reason;
}
if (nil == dismissButton) {
[FBLogger logFmt:@"Cannot find any match for Dismiss alert button using the class chain selector '%@'", FBConfiguration.dismissAlertButtonSelector];
if (nil != errorReason) {
[FBLogger logFmt:@"Original error: %@", errorReason];
}
[FBLogger log:@"Will fallback to the default button location algorithm"];
}
}
if (nil == dismissButton) {
NSArray<XCUIElement *> *buttons = [alertElement.fb_query descendantsMatchingType:XCUIElementTypeButton].allElementsBoundByAccessibilityElement;
dismissButton = alertElement.elementType == XCUIElementTypeAlert
? buttons.firstObject
: buttons.lastObject;
}
return nil == dismissButton
? [[[FBErrorBuilder builder]
withDescriptionFormat:@"Failed to find dismiss button for alert: %@", alertElement]
buildError:error]
: [dismissButton fb_tapWithError:error];
}
- (BOOL)clickAlertButton:(NSString *)label error:(NSError **)error {
XCUIElement *alertElement = self.alertElement;
NSArray<XCUIElement *> *buttons = [alertElement.fb_query descendantsMatchingType:XCUIElementTypeButton].allElementsBoundByAccessibilityElement;
XCUIElement *requestedButton;
for(XCUIElement *button in buttons) {
if([[button wdLabel] isEqualToString:label]){
requestedButton = button;
break;
}
}
if(!requestedButton) {
return
[[[FBErrorBuilder builder]
withDescriptionFormat:@"Failed to find button with label %@ for alert: %@", label, alertElement]
buildError:error];
}
return [requestedButton fb_tapWithError:error];
}
+ (BOOL)isElementObstructedByAlertView:(XCUIElement *)element alert:(XCUIElement *)alert
{
if (!alert.exists) {
return NO;
}
XCElementSnapshot *alertSnapshot = alert.fb_lastSnapshot;
XCElementSnapshot *elementSnapshot = element.fb_lastSnapshot;
if ([alertSnapshot _isAncestorOfElement:elementSnapshot]) {
return NO;
}
if ([alertSnapshot _matchesElement:elementSnapshot]) {
return NO;
}
return YES;
}
- (NSArray<XCUIElement *> *)filterObstructedElements:(NSArray<XCUIElement *> *)elements
{
XCUIElement *alertElement = self.alertElement;
XCUIElement *element = elements.lastObject;
if (!element) {
return elements;
}
NSMutableArray *elementBox = [NSMutableArray array];
for (XCUIElement *iElement in elements) {
if ([FBAlert isElementObstructedByAlertView:iElement alert:alertElement]) {
continue;
}
[elementBox addObject:iElement];
}
if (elementBox.count == 0 && elements.count != 0) {
[FBAlert throwRequestedItemObstructedByAlertException];
}
return elementBox.copy;
}
- (XCUIElement *)alertElement
{
XCUIElement *alert = self.element;
if (nil == alert) {
alert = self.application.fb_alertElement ?: [FBSpringboardApplication fb_springboard].fb_alertElement;
}
if (!alert.exists) {
return nil;
}
[alert fb_nativeResolve];
return alert;
}
@end