Search code examples
core-locationabaddressbookxcode-ui-testing

Can I reset the simulator's contacts and location permission settings for my app, to simulate a first-time usage scenario?


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.


Solution

  • 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.)