5

My iOS app currently has a deployment target of iOS 7 (although my scheme runs the iOS 9.2 simulator), and links with the AddressBook, AddressBookUI, and CoreLocation frameworks. I have a suite of Swift UI tests in Xcode 7 (not in Instruments). When a user first uses my app, they are asked for permission to access their contacts, and their location while the app is running. Since this involves a fair amount of code that needs to execute perfectly, I'd love to automate all of the possible outcomes and ensure the UI is in the desired state.

Can I use Xcode UI testing to reset the Simulator's settings for access to contacts and locations? For example, I could implement this in the setUp() function within my test case class. I've found some tutorials about how to respond to the dialogs, but before I get to that point, I need to simulate the condition where the permissions are requested. Here's a question that explains how to respond to such an alert.

Community
  • 1
  • 1
bneely
  • 9,083
  • 4
  • 38
  • 46
  • what about full reset like `xcrun simctl erase all`. Having similar challenges/approach that's what works for me. As over 3 years passed, I'd be happy to know how are you doing with this issue :) – ciekawy Jul 03 '19 at 15:44

4 Answers4

6

Short answer: no. There is no API for UI Testing to reset the simulator (and therefore reset the permission dialogs). I suggest filing a bug report with Apple explaining why you need this.

UI Testing doesn't make it easy to test "unhappy paths." In your question you mentioned testing all of the flows for permissions. Without manually reseting the simulator between tests this will prove impossible. I suggest only testing the happy path with UI Testing and leaving the other cases to unit tests.

I understand that this is not ideal but for now it's a decent workaround. For what it's worth, that is the approach I take when testing our app. (The app asks for push notifications and location permissions.)

Joe Masilotti
  • 16,815
  • 6
  • 77
  • 87
  • Thanks! As of now, I've implemented the UI tests anyway, with the caveat that the simulator first needs a manual reset for valid results. I can exclude this collection (one superclass with common vars and funcs, and four subclasses to handle alerts and verify results) from the test target, and enable and manually run when needed. Then the core UI Tests target focuses on tests that are fully automated and expected to always pass. I'll file the bug but expect that it might not get fixed (although I can imagine an easy sandbox approach). I also need to pursue better unit test coverage. – bneely May 07 '16 at 00:06
4

xcrun simctl privacy booted reset location <your_app_bundle_id>

Source: https://developer.apple.com/videos/play/wwdc2020/10647/

Nikolay Derkach
  • 1,734
  • 2
  • 22
  • 34
2

In UITesting, it is simple:

let app = XCUIApplication()

app.resetAuthorizationStatus(for: .location)
J. Doe
  • 12,159
  • 9
  • 60
  • 114
-1

Here is how I would do it. I would delete the simulator data which will delete the saved settings, hopefully this works in your situation. In Swift:

let filemanager = NSFileManager.defaultManager()

do {
    try filemanager.removeItemAtPath(NSHomeDirectory())
}
catch let error as NSError {
    print("Ooops! Something went wrong: \(error)")
}

Just figure out a good spot to put that in your code and poof--simulator data gone.

cakes88
  • 1,857
  • 5
  • 24
  • 33
  • 1
    It's a good suggestion, although it appears that those privacy settings aren't stored in the home directory. It's likely that if they are in the filesystem, it's not a user-writable path. I tried this in my setUp() function for the test class where I create interruption monitors for the alerts, but those handlers aren't executing (I was checking with an NSLog and a breakpoint) when I run all of the tests in that class. Thanks though! – bneely May 05 '16 at 22:16
  • 1
    Actually I think an approach that might work, although likely unsupported, is to remove the entire simulator directory from the development machine rather than whatever paths are accessible inside of iOS (whether simulated or on a device). I'll explore this. – bneely May 07 '16 at 00:07