@@ -55,50 +55,34 @@ extension RunnerTests {
5555 guard let button = chooseAlertButton ( alert . buttons , action : action ) else {
5656 return Response ( ok : false , error : ErrorPayload ( message : " alert \( action ) button not found " ) )
5757 }
58- let previousTitle = preferredAlertTitle ( alert . root , buttons : alert . buttons )
59- let actionButtonLabel = button . label . trimmingCharacters ( in : . whitespacesAndNewlines )
60- let actionButtonFrame = button . frame
58+ guard Date ( ) < deadline else {
59+ return alertVerificationResponse ( . timedOut , action : action , activated : false )
60+ }
61+ guard let original = captureAlertPresentation ( alert . root ) else {
62+ return alertVerificationResponse ( . unconfirmed , action : action , activated : false )
63+ }
64+ let observesApplicationRoot = alert . root . elementType == . application
65+ guard Date ( ) < deadline else {
66+ return alertVerificationResponse ( . timedOut , action : action , activated : false )
67+ }
6168 let outcome = activateElement ( app : alert . ownerApp , element : button , action : " alert \( action ) " )
6269 if let response = unsupportedResponse ( for : outcome ) {
6370 return response
6471 }
65- sleepFor ( 0.2 )
66- if alertStillVisible (
67- in : alert . ownerApp ,
68- source : alert . source ,
69- previousTitle : previousTitle ,
70- actionButtonLabel : actionButtonLabel ,
71- deadline : deadline
72- ) {
73- if ! actionButtonFrame . isNull && ! actionButtonFrame . isEmpty {
74- let coordinateOutcome = tapAt (
75- app : alert . ownerApp ,
76- x : actionButtonFrame . midX ,
77- y : actionButtonFrame . midY
78- )
79- if let response = unsupportedResponse ( for : coordinateOutcome ) {
80- return response
81- }
82- sleepFor ( 0.2 )
83- }
84- }
85- if alertStillVisible (
86- in : alert . ownerApp ,
87- source : alert . source ,
88- previousTitle : previousTitle ,
89- actionButtonLabel : actionButtonLabel ,
90- deadline : deadline
91- ) {
92- return Response (
93- ok : false ,
94- error : ErrorPayload (
95- code : " INTERACTION_FAILED " ,
96- message : " alert \( action ) did not dismiss the visible alert " ,
97- hint : " The alert button was found but the system still reports the alert after tapping it. "
72+ while true {
73+ sleepFor ( min ( 0.2 , max ( 0 , deadline . timeIntervalSinceNow ) ) )
74+ let verification = RunnerAlertVerification . verify (
75+ original : original ,
76+ observation : observeAlert (
77+ in : alert . ownerApp ,
78+ source : alert . source ,
79+ observesApplicationRoot : observesApplicationRoot ,
80+ deadline : deadline
9881 )
9982 )
83+ if verification == . stillVisible { continue }
84+ return alertVerificationResponse ( verification , action : action , activated : true )
10085 }
101- return Response ( ok : true , data : DataPayload ( message : action == " accept " ? " accepted " : " dismissed " ) )
10286 }
10387
10488 return Response (
@@ -135,57 +119,6 @@ extension RunnerTests {
135119 return RunnerAlert ( root : root , ownerApp : ownerApp , buttons : buttons , source : source )
136120 }
137121
138- private func alertStillVisible (
139- in ownerApp : XCUIApplication ,
140- source : RunnerAlertSource ,
141- previousTitle : String ,
142- actionButtonLabel : String ,
143- deadline : Date
144- ) -> Bool {
145- guard Date ( ) < deadline ,
146- let current = resolveAlert ( source : source , app : ownerApp , deadline : deadline )
147- else {
148- return false
149- }
150- let currentTitle = preferredAlertTitle ( current . root , buttons : current . buttons )
151- if previousTitle == currentTitle {
152- return true
153- }
154- return current . buttons . contains { button in
155- button . label . trimmingCharacters ( in : . whitespacesAndNewlines ) == actionButtonLabel
156- }
157- }
158-
159- private func resolveAlert (
160- source : RunnerAlertSource ,
161- app : XCUIApplication ,
162- deadline : Date
163- ) -> RunnerAlert ? {
164- switch source {
165- case . blockingSystemModal :
166- #if os( macOS )
167- return nil
168- #else
169- guard case . resolved ( let modal ) = resolveBlockingSystemModal ( deadline : deadline ) else {
170- return nil
171- }
172- return runnerAlert ( modal )
173- #endif
174- case . appAlert :
175- guard let alert = firstExistingElement (
176- in : safeElementsQuery { app . alerts . allElementsBoundByIndex }
177- ) else {
178- return nil
179- }
180- return runnerAlert ( root : alert , ownerApp : app , source : . appAlert )
181- case . dismissPopup :
182- guard let popup = firstDismissPopupWindow ( in : app ) else {
183- return nil
184- }
185- return runnerAlert ( root : popup , ownerApp : app , source : . dismissPopup )
186- }
187- }
188-
189122 private func firstExistingElement ( in elements : [ XCUIElement ] ) -> XCUIElement ? {
190123 elements . first { isVisibleElement ( $0 ) }
191124 }
0 commit comments