@@ -157,98 +157,38 @@ extension RunnerTests {
157157 return true
158158 }
159159
160- private static let textInputElementTypeRawValues : [ UInt ] = [
161- XCUIElement . ElementType . textField ,
162- . secureTextField ,
163- . searchField ,
164- . textView ,
165- ] . map ( \. rawValue )
166-
167160 private func queryTextInputs (
168161 app : XCUIApplication ,
169162 point : CGPoint ,
170163 shouldStop : ( ) -> Bool
171164 ) -> [ ResolvedTextEntryInput ] {
172- // One query for every text input type: each query snapshots the whole application.
173- let candidates = app . descendants ( matching : . any )
174- . matching ( NSPredicate ( format : " elementType IN %@ " , Self . textInputElementTypeRawValues ) )
175- . allElementsBoundByIndex
176- #if AGENT_DEVICE_RUNNER_UNIT_TESTS
177- if let issue = textInputProbeIssueForTesting {
178- textInputProbeIssueForTesting = nil
179- record ( issue )
180- }
181- #endif
182- guard ! shouldStop ( ) else { return [ ] }
183- // One snapshot per candidate answers enablement, frame, and the ordering below: each attribute
184- // read from the live element runs the candidate's query again. A candidate that cannot be
185- // snapshotted is read live, so its failure still reaches the caller's issue handling.
186- // Each handle binds by index within its own element type, so an input of another type that
187- // appears ahead of it later does not take its place.
188- var indexByType : [ XCUIElement . ElementType : Int ] = [ : ]
189165 var inputs : [ ResolvedTextEntryInput ] = [ ]
190- for ( position , element ) in candidates . enumerated ( ) {
166+ for query in [ app . textFields , app . secureTextFields , app . searchFields , app . textViews ] {
191167 if shouldStop ( ) { break }
192- // Each candidate re-runs the shared query, so an input that left the app since an earlier
193- // candidate's read moves a later input into a position already read, and the per-type count
194- // no longer names the input a position reads. Every position must still exist and every
195- // handle must read as its position's input, at the point or not, or the lookup can miss an
196- // input. A missing position's snapshot throws only after XCTest waits for a match, which only
197- // a lookup that falls back pays, so existence is read only when the snapshot fails.
198- let snapshot = try? element . snapshot ( )
199- guard snapshot != nil || element . exists else {
200- return queryTextInputsByType ( app : app , point : point , shouldStop : shouldStop )
201- }
202- let elementType = snapshot ? . elementType ?? element . elementType
203- let typeIndex = indexByType [ elementType , default : 0 ]
204- indexByType [ elementType ] = typeIndex + 1
205- let handle = app . descendants ( matching : elementType ) . element ( boundBy : typeIndex )
206- let candidate : ResolvedTextEntryInput
207- if let snapshot {
208- guard let handleSnapshot = try? handle . snapshot ( ) , Self . isSameTextInput ( handleSnapshot , as : snapshot ) else {
209- return queryTextInputsByType ( app : app , point : point , shouldStop : shouldStop )
210- }
211- candidate = ResolvedTextEntryInput ( element : handle , snapshot : handleSnapshot )
212- } else {
213- candidate = ResolvedTextEntryInput ( element : handle , snapshot : nil )
214- }
168+ let candidates = query . allElementsBoundByIndex
215169#if AGENT_DEVICE_RUNNER_UNIT_TESTS
216- textInputCandidateReadForTesting ? ( position )
217- #endif
218- let attributes = candidate . attributes
219- if isCoordinateTextInputCandidate ( enabled : attributes . isEnabled , frame : attributes . frame , point : point ) {
220- inputs . append ( candidate )
170+ if let issue = textInputProbeIssueForTesting {
171+ textInputProbeIssueForTesting = nil
172+ record ( issue )
221173 }
222- }
223- return inputs . sorted { smallestElementFirst ( $0 . attributes , $1 . attributes ) }
224- }
225-
226- /// `queryTextInputs` with one query per element type, whose handles each read through their own
227- /// snapshot, for a lookup whose shared query changed while it read the candidates.
228- private func queryTextInputsByType (
229- app : XCUIApplication ,
230- point : CGPoint ,
231- shouldStop : ( ) -> Bool
232- ) -> [ ResolvedTextEntryInput ] {
233- NSLog ( " AGENT_DEVICE_RUNNER_TEXT_INPUT_LOOKUP_BY_TYPE reason=inputs-changed-during-lookup " )
234- var inputs : [ ResolvedTextEntryInput ] = [ ]
235- for query in [ app . textFields , app . secureTextFields , app . searchFields , app . textViews ] {
236- for element in query . allElementsBoundByIndex {
237- guard ! shouldStop ( ) else { return [ ] }
238- guard element . exists else { continue }
239- let input = ResolvedTextEntryInput ( element : element , snapshot : try? element . snapshot ( ) )
240- let attributes = input . attributes
174+ #endif
175+ // One snapshot per candidate answers enablement, frame, and the ordering below: each attribute
176+ // read from the live element runs the candidate's query again. A candidate that cannot be
177+ // snapshotted is read live, so its failure still reaches the caller's issue handling. A missing
178+ // candidate's snapshot throws only after XCTest waits for a match, so existence is read only
179+ // when the snapshot fails.
180+ for element in candidates {
181+ if shouldStop ( ) { break }
182+ let snapshot = try? element . snapshot ( )
183+ guard snapshot != nil || element . exists else { continue }
184+ let candidate = ResolvedTextEntryInput ( element : element , snapshot : snapshot )
185+ let attributes = candidate . attributes
241186 if isCoordinateTextInputCandidate ( enabled : attributes . isEnabled , frame : attributes . frame , point : point ) {
242- inputs . append ( input )
187+ inputs . append ( candidate )
243188 }
244189 }
245190 }
191+ guard ! shouldStop ( ) else { return [ ] }
246192 return inputs . sorted { smallestElementFirst ( $0 . attributes , $1 . attributes ) }
247193 }
248-
249- private static func isSameTextInput ( _ read : XCUIElementSnapshot , as candidate : XCUIElementSnapshot ) -> Bool {
250- read . elementType == candidate . elementType
251- && read . identifier == candidate . identifier
252- && read . frame == candidate . frame
253- }
254194}
0 commit comments