@@ -33,6 +33,113 @@ + (nullable XCUIElement *)fb_limitedAccessPromptAlertElement
3333 return promptApp.fb_alertElement ;
3434}
3535
36+ + (nullable id <FBXCElementSnapshot>)fb_findSafariAlertSnapshotInScrollView : (id <FBXCElementSnapshot>)scrollViewSnapshot
37+ {
38+ CGRect appFrame = scrollViewSnapshot.frame ;
39+
40+ __block id <FBXCElementSnapshot> webView = nil ;
41+ [scrollViewSnapshot enumerateDescendantsUsingBlock: ^(id <FBXCElementSnapshot> descendant) {
42+ if (nil == webView && nil != descendant.identifier && [descendant.identifier isEqualToString: @" WebView" ]) {
43+ webView = descendant;
44+ }
45+ }];
46+ if (nil == webView) {
47+ return nil ;
48+ }
49+
50+ // Find the first XCUIElementTypeOther which is the grandchild of the web view
51+ // and is horizontally aligned to the center of the screen, and contains one
52+ // to two buttons and at least one text view.
53+ __block id <FBXCElementSnapshot> candidate = nil ;
54+ [webView enumerateDescendantsUsingBlock: ^(id <FBXCElementSnapshot> descendant) {
55+ if (nil != candidate || descendant.elementType != XCUIElementTypeOther) {
56+ return ;
57+ }
58+ CGRect curFrame = descendant.frame ;
59+ if (CGRectEqualToRect (appFrame, curFrame)
60+ || curFrame.origin .x <= 0
61+ || curFrame.size .width >= appFrame.size .width ) {
62+ return ;
63+ }
64+ CGFloat possibleCenterX = (appFrame.size .width - curFrame.size .width ) / 2 ;
65+ if (fabs (possibleCenterX - curFrame.origin .x ) >= MAX_CENTER_DELTA ) {
66+ return ;
67+ }
68+
69+ __block NSUInteger buttonsCount = 0 ;
70+ __block NSUInteger textViewsCount = 0 ;
71+ [descendant enumerateDescendantsUsingBlock: ^(id <FBXCElementSnapshot> innerDescendant) {
72+ XCUIElementType curType = innerDescendant.elementType ;
73+ if (curType == XCUIElementTypeButton) {
74+ buttonsCount++;
75+ } else if (curType == XCUIElementTypeTextView) {
76+ textViewsCount++;
77+ }
78+ }];
79+ if (buttonsCount >= 1 && buttonsCount <= 2 && textViewsCount > 0 ) {
80+ candidate = descendant;
81+ }
82+ }];
83+ return candidate;
84+ }
85+
86+ + (nullable id <FBXCElementSnapshot>)fb_findAlertSnapshotInApplicationSnapshot : (id <FBXCElementSnapshot>)appSnapshot
87+ {
88+ __block id <FBXCElementSnapshot> found = nil ;
89+ [appSnapshot enumerateDescendantsUsingBlock: ^(id <FBXCElementSnapshot> descendant) {
90+ if (nil != found) {
91+ return ;
92+ }
93+ XCUIElementType curType = descendant.elementType ;
94+ if (curType == XCUIElementTypeAlert || curType == XCUIElementTypeSheet || curType == XCUIElementTypeScrollView) {
95+ found = descendant;
96+ }
97+ }];
98+ if (nil == found) {
99+ return nil ;
100+ }
101+
102+ if (found.elementType == XCUIElementTypeAlert) {
103+ return found;
104+ }
105+
106+ if (found.elementType == XCUIElementTypeSheet) {
107+ if ([UIDevice currentDevice ].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
108+ return found;
109+ }
110+
111+ // In case of iPad we want to check if sheet isn't contained by popover.
112+ // In that case we ignore it.
113+ id <FBXCElementSnapshot> ancestor = found.parent ;
114+ while (nil != ancestor) {
115+ if (nil != ancestor.identifier && [ancestor.identifier isEqualToString: @" PopoverDismissRegion" ]) {
116+ return nil ;
117+ }
118+ ancestor = ancestor.parent ;
119+ }
120+ return found;
121+ }
122+
123+ if (found.elementType == XCUIElementTypeScrollView) {
124+ id <FBXCElementSnapshot> app = [[FBXCElementSnapshotWrapper ensureWrapped: found] fb_parentMatchingType: XCUIElementTypeApplication];
125+ if (nil != app && [app.label isEqualToString: FB_SAFARI_APP_NAME ]) {
126+ // Check alert presence in Safari web view
127+ return [self fb_findSafariAlertSnapshotInScrollView: found];
128+ }
129+ }
130+
131+ return nil ;
132+ }
133+
134+ - (nullable id <FBXCElementSnapshot>)fb_alertSnapshot
135+ {
136+ id <FBXCElementSnapshot> appSnapshot = self.fb_cachedSnapshot ?: [self fb_customSnapshot ];
137+ if (nil == appSnapshot) {
138+ return nil ;
139+ }
140+ return [self .class fb_findAlertSnapshotInApplicationSnapshot: appSnapshot];
141+ }
142+
36143- (nullable XCUIElement *)fb_alertElementFromSafariWithScrollView : (XCUIElement *)scrollView
37144 viewSnapshot : (id <FBXCElementSnapshot>)viewSnapshot
38145{
0 commit comments