Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

like image 740
CodeBender Avatar asked Oct 14 '19 17:10

CodeBender


People also ask

How do I remove UI tests in Xcode?

You can either right click the test and delete or select the target then select the minus symbol ( - ) at the bottom of the Document Navigator pane.

How do I turn off unit test in Xcode?

One of the quickest ways to disable a unit test is to simply rename its name. Xcode will execute only those test methods that start with a prefix “test”. If a method name does not start with a test prefix, it will not be executed.

How do I run a UI test in Xcode?

If you are making a new app, make sure you select the “Use UI Tests” checkbox when creating a new Xcode project. To add it in an existing project go to the build folder and on the bottom left you will see the plus button. Selecting that will give you a dropdown menu, where you can then select “UI Testing Bundle”.


1 Answers

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()
like image 51
CodeBender Avatar answered Sep 23 '22 02:09

CodeBender