Skip to content

Instantly share code, notes, and snippets.

@rocboronat
Last active December 26, 2022 07:05
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rocboronat/65b1187a9fca9eabfebb5121d818a3c4 to your computer and use it in GitHub Desktop.
Save rocboronat/65b1187a9fca9eabfebb5121d818a3c4 to your computer and use it in GitHub Desktop.
Tap the "allow" button while running an Android instrumental test using UIAutomator
package com.fewlaps.android.permissiongranter;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.v4.content.ContextCompat;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
public class PermissionGranter {
private static final int PERMISSIONS_DIALOG_DELAY = 3000;
private static final int GRANT_BUTTON_INDEX = 1;
public static void allowPermissionsIfNeeded(String permissionNeeded) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(permissionNeeded)) {
sleep(PERMISSIONS_DIALOG_DELAY);
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject allowPermissions = device.findObject(new UiSelector()
.clickable(true)
.checkable(false)
.index(GRANT_BUTTON_INDEX));
if (allowPermissions.exists()) {
allowPermissions.click();
}
}
} catch (UiObjectNotFoundException e) {
System.out.println("There is no permissions dialog to interact with");
}
}
private static boolean hasNeededPermission(String permissionNeeded) {
Context context = InstrumentationRegistry.getTargetContext();
int permissionStatus = ContextCompat.checkSelfPermission(context, permissionNeeded);
return permissionStatus == PackageManager.PERMISSION_GRANTED;
}
private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException("Cannot execute Thread.sleep()");
}
}
}
WEP! Instead of copying this class, please, just use this project: https://github.com/SchibstedSpain/Barista it's easier to maintain it :·) By the way, this line is written there to fail the compilation. Feel free to remove the whole class and depend on Barista :·)
@paristote
Copy link

You can use new UiSelector().clickable(true).checkable(false).index(1) to select the button even when the Never ask again checkbox is visible.

@rocboronat
Copy link
Author

rocboronat commented Jul 21, 2016

@paristote boom! Nice idea! Going to try it and update the GIST! 👍

EDIT: Done! 🎉

@Sloy
Copy link

Sloy commented Aug 2, 2016

Here's another improvement: You don't need to pass the Activity around. You only need the Context, and you can get it through InstrumentationRegistry.getTargetContext().

So sad you can't submit pull requests to gists :(

@rocboronat
Copy link
Author

Thank you @Sloy!

@konk3r
Copy link

konk3r commented Aug 4, 2016

Quick note, you removed the context from the method signature but you're still passing it in on line 22.

@prolificcoder
Copy link

One problem that I saw with the new UiSelector() .clickable(true).checkable(false).index(GRANT_BUTTON_INDEX is that it clicks on menu button if it exists

@rocboronat
Copy link
Author

@konk3r yes, but it's get from InstrumentationRegistry.getTargetContext(), as @Sloy recommended :·)

@rocboronat
Copy link
Author

@prolificcoder but we only launch the Dialog if we don't have the permission, so the menu button doesn't apply... isn't it? BTW, we are totally open to improve this thing. Actually, we improve it day by day as we find issues with it :·)

@tomhermann
Copy link

Thanks for the gist! Worked great with a minor alteration: line 22 should not be passing the context to the hasNeededPermission() method.

@rocboronat
Copy link
Author

Thanks @tomhermann!

@bingzer
Copy link

bingzer commented Nov 19, 2016

Thank you! This is awesome. Will GRANT_BUTTON_INDEX = 1 be consistent across different locale, device, etc..?

@rocboronat
Copy link
Author

@bindzer as far as we know (and we test the app lots of times a day), yes :·) BTW, we don't work with RTL languages... if you find any issue, mention me and we'll fix it asap! Or propose a solution if possible, hihi! Thanks :·)

@sfoley-gpqa
Copy link

sfoley-gpqa commented Jan 10, 2017

Thank you. This solved a huge problem I was having.

@autonomousapps
Copy link

Not a big deal, but you'll make a lawyer happy if you add a free-to-use-for-commercial-purposes license to the gist :)

@rocboronat
Copy link
Author

Wep @everyone! We added this class to Barista, our tool above Espresso and UiAutomator to make instrumental tests green: https://github.com/SchibstedSpain/Barista By the way, @autonomousapps Barista has a License so you can use wherever you want 🎉

Thanks for your suggestions, that made this class better :·) Now, let's move to PR's! :·D

@knezmilos13
Copy link

Hey, so for some reason index 1 is deny for me, index 0 doesn't work (allowPermissions.exists() is false), and index 2 is allow. I've tested it a bit, and it seems it is like that because I request two permissions at once:
perm

When I ask for only a single permission, it works as you stated.

@rocboronat
Copy link
Author

Wep @knezmilos13! Please, open a issue in Barista: https://github.com/SchibstedSpain/Barista we will manage all issues about this class there :·)

@pdhamodaran-cpi
Copy link

Please upgrade to androidx

@inhaledesign
Copy link

inhaledesign commented Sep 11, 2019

I'm not really sure what the use-case is for this. Is it to grant your tests permission before they run? If so, then the GrantPermissionRule would be a better option.

If this is intended to be used as part of tests, then I'd remove the try/catch block and hasNeededPermission check, as those are both going to result in false positives. Edit: And probably a lot of other code in this. Really, the upshot anything that would keep the test from failing if the button is not found should be removed.

@rocboronat
Copy link
Author

Hey @inhaledesign! All this work is now part of Barista, a framework over Espresso to test Android apps in no time. Feel free to open the discussion there: https://github.com/AdevintaSpain/Barista

Thanks for your feedback, though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment