Detox: Handle iOS permission dialogs

Created on 3 Aug 2016  路  26Comments  路  Source: wix/Detox

EarlGrey has a hard time dealing with them since they're external to the app

https://github.com/google/EarlGrey/issues/55

this thread offers some interesting workarounds.. we can add them directly into detox

accepteenhancement

Most helpful comment

Guys, support has landed in Detox for this feature. As we have not yet updated the documentation yet, for now, you can see the syntax here

Currently supported permissions:

calendar=YES|NO
camera=YES|NO
contacts=YES|NO
health=YES|NO
homekit=YES|NO
location=always|inuse|never
medialibrary=YES|NO
microphone=YES|NO
motion=YES|NO
notifications=YES|NO
photos=YES|NO
reminders=YES|NO
siri=YES|NO

As described in the Getting Started document, you need to install applesimutils for this.

All 26 comments

@LeoNatan this is not urgent since it's not for Version 1 milestone

Urgent. All tests fail once you get the dialog (for example notifications permissions usually happen on app launch or close to it)

@DanielZlotin This is a big problem in Earl Grey also, due to in-process architecture. There seem to be several workarounds, including setting a DB file in the simulator subsystem to automatically allow permissions.

If you can swizzle the permissions on demand at runtime, and have some JS API to mange it per test that would be best. If you could expect it that would be better. Something like:
expect(permission.photos).toBeRequested()
allowNextPermission()
rejectNextPermission()

@yedidyak This will be a big issue to fix which will take much time. In the meantime, do not stop working on tests. Instead, mock the permission API to return success or failure.

@LeoNatan That's possible in some cases, but in others the permission itself is necessary for the functionality.

I have decided to go against swizzling APIs. It just does not scale, as more and more APIs are added each year. Detox is a testing framework for native apps, not just the small subset of APIs wrapped in RN.

Instead, I have created applesimutils and DetoxHelper.

applesimutils allows setting permissions before a process is launched, and thus can be used together with relaunchApp API to set permissions.

DetoxHelper is not ready for prime time just yet, but it is intended to support switching of permissions on the fly. This requires investigation and I am not 100% if possible at all.

For now, let's integrate applesimutils with Detox.

Kudos for this ! @LeoNatan Has applesimutils been integrated into Detox yet? If not, how can we use it to set permissions before Detox test?

@irrigator Unfortunately, integrating applesimutils with Detox is still not ready. In the mean time, you can indeed use applesimutils before running your Detox test. Just make sure to pass the same simulator name (or preferrably, unique identifier), and set the permissions as you need. You can read the usage in the readme for applesimutils; it should be simple and intuitive.

@LeoNatan I used applesimutils in the following manner:

function setPermissions(permissions) {
  cp.execSync(
    `applesimutils \\
    --simulator "1991C14B-9892-44E8-BCEF-EA63F308972E" \\
    --bundle io.rangle.starter.app \\
    --setPermissions "${permissions.join()}"`,
    {
      stdio: 'inherit',
    },
  );
}


beforeAll(async () => {
  await detox.init(config);
  await setPermissions(['camera=YES']);
}, 25000);

It causes the simulator to refresh going black with a spinner then when the simulator is back on the app is closed and the tests fail. How would you suggest running it before the tests?

Update:

I figured out a way to do it by doing the following:

beforeAll(async () => {
  await detox.init(config);
  await setPermissions(['camera=YES']);
  await device.relaunchApp();
}, 25000);

Yes, you need to launch the app.
This is being developed now, and we will have an official support soon. Stay tuned.

Great news @LeoNatan !

@LeoNatan How far is this? As a current workaround I take the simulator ID from the global device object in the function mentioned by @mobinni but this approach feels a little hacky to me 馃憤

In development. Cannot comment on how long it will take, as I am not the one developing it, but it is in active development and is coming soon.

Guys, support has landed in Detox for this feature. As we have not yet updated the documentation yet, for now, you can see the syntax here

Currently supported permissions:

calendar=YES|NO
camera=YES|NO
contacts=YES|NO
health=YES|NO
homekit=YES|NO
location=always|inuse|never
medialibrary=YES|NO
microphone=YES|NO
motion=YES|NO
notifications=YES|NO
photos=YES|NO
reminders=YES|NO
siri=YES|NO

As described in the Getting Started document, you need to install applesimutils for this.

@LeoNatan @rotemmiz how to initial run app with permissions?

@LeoNatan Thanks a lot for adding this feature! 馃挴 It works perfectly and it's super simple to use as well!

@Monte9 Cheers, thanks for the kind words!

Can anyone give me an example to approve location permission

@yogeshthanvi it's now documented here
the options for the location permission are:

location=always|inuse|never

so you use it as the following

await device.launchApp({ permissions: { location: 'always' } })

@yogeshthanvi Could you tell me where you have looked for this information? Might make sense to add this info right there to help the next person even faster 馃

@menasameh Thanks man , love you

Please make sure to read the documentation.

For ease of other users:

describe(':ios: Permissions', () => {
  beforeAll(async () => {
    await device.launchApp({ permissions: { notifications: 'YES' } });
  });

  it('Permissions is granted', async () => {
    await device.launchApp({permissions: {calendar: 'YES'}});
    await element(by.text('Permissions')).tap();
    await expect(element(by.text('granted'))).toBeVisible();
  });

  it('Permissions denied', async () => {
    await device.launchApp({permissions: {calendar: 'NO'}});
    await element(by.text('Permissions')).tap();
    await expect(element(by.text('denied'))).toBeVisible();
  });
});

Quoting from https://github.com/wix/detox/blob/master/detox/test/e2e/13.permissions.test.js

Was this page helpful?
0 / 5 - 0 ratings