Search code examples
iosiphoneios13uiactivityviewcontrollerxcode-ui-testing

How to dismiss the UIActivityViewController during a UI test with Xcode 11 & iOS 13


Apple has re-designed the share sheet that appears, which has now broken my UI tests.

I have attempted to record a new UI test through Xcode, but as soon as I tap on the dismiss button, the test terminates so I have not been able to capture the event.

Ultimately, I just want to know how I can access the gray 'X' shown with the arrow below:


Solution

  • I have just tested this with Xcode 13 and have found that the original answer no longer works. However, I am keeping it for posterity, or those using previous versions of Xcode.

    Xcode 13

    I have tested this with Xcode 13.0 and verified it works for iPhone and iPad:

    let activityListView = app.otherElements.element(matching: .other,
                                                     identifier: "ActivityListView")
    XCTAssertTrue(activityListView.waitForExistence(timeout: 2.0))
    activityListView.buttons["Close"].tap()
    

    Previous versions

    After some trial and error, I was able to locate where my specific elements were with the following:

    app.otherElements.element(boundBy: 1).buttons.element(boundBy: 0).tap()

    Using app.otherElements.element(boundBy: 1) would identify the share sheet for me. I had attempted to locate it through accessibility identifiers, but I could not find one that worked, including previously valid ones used in iOS 12 and below.

    Please note that based on the layout of your screen, the index value may differ from what I am seeing.

    Next, .buttons.element(boundBy: 0).tap() was used to locate the Close button. I again attempted to use identifiers, but could not find anything that represented the button.

    When I attempted to discern additional information through the console while testing, I would always wind up crashing the test. This result was surprising, as I was able to query these elements with Xcode 10.

    Ultimately, I would like to find working identifier values so that I can have something that works reliably across products, without the trial and error to find the share sheet's index value.

    For iPad

    The following will dismiss the popover for an iPad:

    app.otherElements["PopoverDismissRegion"].tap()