2

I wanna UI test an app workflow that calls, beside others, an UIDocumentPickerViewController. I tried to record this workflow in Xcode, but when i reach this controller i get an error message saying

Timestamped Event Matching Error: Failed to find matching element

I there a way to pass such controllers or mock them to, in this case, return a file?

Tobe
  • 508
  • 5
  • 14

1 Answers1

2

I ran into the same trouble. My workaround:

Place a breakpoint in your UI test that will be hit when the file picker is in the foreground.

Sample test:

    func testBlah() {
        let app = XCUIApplication()
        // The next 2 lines interact with my app to cause it to pop up the file picker.
        // These will be different for your app :)
        app.navigationBars["Dashboard"].buttons["download"].tap()
        app.staticTexts["Browse"].tap()
        sleep(3) // Can place breakpoint here for example
    }

Once you've hit the breakpoint, view the hierarchy of views by typing po app (replacing app with the name of your XCUIApplication object) into the right pane of the debugger:

(lldb) po app
    t =   193.67s Snapshot accessibility hierarchy for app with pid 941
    t =   194.22s Snapshot accessibility hierarchy for app with pid 941
Attributes: Application, pid: 941, label: 'Redacted'
Element subtree:
 →Application, 0x2814fdea0, pid: 941, label: 'Redacted'
    Window (Main), 0x2814fe4c0, {{0.0, 0.0}, {375.0, 667.0}}
      Other, 0x2814fe3e0, {{0.0, 0.0}, {375.0, 667.0}}

snip

Cell, 0x2814f42a0, {{257.0, 131.0}, {90.0, 175.0}}, identifier: 'Waterfall Loop Trail, xml', label: 'Waterfall Loop Trail, xml, 9/16/19, 42 KB'
Cell, 0x2814f4380, {{28.0, 321.0}, {90.0, 175.0}}, identifier: 'Waterfall Loop Trail, gpx', label: 'Waterfall Loop Trail, gpx, 9/16/19, 42 KB'

Since I'm trying to tap Waterfall Loop Trail, gpx, I can now do: app.cells["Waterfall Loop Trail, gpx"].tap()

I imagine I could use a similar strategy to interact with the other elements on this screen. It's super annoying that Xcode doesn't seem to support it in the recorder.

Jacob Rau
  • 25
  • 5
  • What a great trick, solved my problem, thank you very much! I had to add some more things to make it work for selecting a cell which represents a document, hope it may help someone else.. Using an explicit cells.matching with the identifier. Get the second element for the cell in a collection view as the first one triggered the sorting button. Finally use a forceTapElement which is an extension found here: https://stackoverflow.com/a/33534187/4417912 Code: `app.cells.matching(identifier: "your identifier found with po app").element(boundBy: 1).forceTapElement()` – Jim B Nov 02 '19 at 18:57
  • Excellent answer. – arcdale Sep 08 '20 at 11:39