There's nothing more frustrating when running an iOS test than to have a system dialog pop up right in the middle of your test. And it only makes matters worse when that dialog is asking whether to grant a certain permission to your app. You're testing the app, so obviously you want it granted! (Unless of course you're testing what happens when it's not granted, in which case, dialog away.)
Luckily, recent versions of Appium (the latest 1.9.2-beta.2 and up) have a new capability that makes it possible to do away with both of these problems simultaneously. The new capability is permissions, and it lets you set any permission that iOS knows about, from location to notifications to photos. One caveat: the new capability only works for simulators, so those of you running on real devices will have to sit tight and keep manually automating those popups.
Because Appium relies on some newfangled 3rd-party software to make the permissions magic happen, we need to get said software on our machines before we try and run our test. Here's what to do (assuming you're on a Mac and have Homebrew installed; and if you're not using a Mac this guide won't be very useful to you anyway!):
The permissions Capability
There are no new commands tied to this feature, only the permissions capability. It should be a string, in valid JSON format, with the following structure:
Of course, replace all these fake values with ones that make sense for your app. How do you know what permission names and values are available? You can always check out the applesimutils documentation for the full list, but I will reproduce them here:
So let's say we wanted to allow our typical test app to access the GPS data while the app is in use. In that case, our JSON object should look like:
But of course we need to include this as the capability, so we have to stringify it and include it in our Java client's capability builder, like so:
(Notice that I've had to escape the inner quotes).
This is all we need to do to ensure that, when my app launches and before my test even begins, the permissions I need will already be set, so that I don't have to worry about any popups in the course of my test. I can also experiment with setting the values differently for different tests, if I want to see how my app will behave if a user changes the permission to something else, or turns it off entirely.
I've incorporated this example into a complete code sample, which navigates to a geolocation demo view on The App, and attempts to read the latitude and longitude from the view. This of course requires location permissions to be granted, and will only work with the new permissions capability (try removing the capability, and you'll see the popup asking for authorization).
Don't forget to also have a look at the sample on GitHub.