Skip to content

add acceptAllPermissions and denyAllPermissions method to click thru … #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
*/
public class DeviceAutomator {

private static final String TEXT_DENY_UPPER = "DENY";
private static final String TEXT_DENY_REGULAR = "Deny";
private static final String TEXT_ALLOW_UPPER = "ALLOW";
private static final String TEXT_ALLOW_REGULAR = "Allow";
private UiDevice mDevice;
private UiObjectMatcher mMatcher;

Expand Down Expand Up @@ -266,6 +270,18 @@ public DeviceAutomator acceptRuntimePermission(String permission) {
return this;
}

/**
* Clicks the accept button on runtime permission prompts on Marshmallow and above if the prompt
* is displayed. Will click all permission dialogs if there are multiple permission requested
* together.
*
* @return {@link DeviceAutomator} for method chaining.
*/
public DeviceAutomator acceptAllRuntimePermissions() {
clickAndAllowAllPermissionDialogButtons();
return this;
}

/**
* Clicks the deny button on runtime permission prompts on Marshmallow and above if the prompt
* is displayed.
Expand All @@ -278,6 +294,18 @@ public DeviceAutomator denyRuntimePermission(String permission) {
return this;
}

/**
* Clicks the deny button on runtime permission prompts on Marshmallow and above if the prompt
* is displayed. Will click all permission dialogs if there are multiple permission requested
* together.
*
* @return {@link DeviceAutomator} for method chaining.
*/
public DeviceAutomator denyAllRuntimePermissions() {
clickAndDenyAllPermissionDialogButtons();
return this;
}

private void clickPermissionDialogButton(String permission, int buttonIndex) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
ContextCompat.checkSelfPermission(getTargetContext(), permission) != PackageManager.PERMISSION_GRANTED) {
Expand All @@ -294,6 +322,37 @@ private void clickPermissionDialogButton(String permission, int buttonIndex) {
}
}

private void clickAndAllowAllPermissionDialogButtons() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
UiObject allowPermissionsUsingText = mDevice.findObject(new UiSelector().text(TEXT_ALLOW_UPPER));
while (allowPermissionsUsingText.exists()) {
allowPermissionsUsingText.click();
}
allowPermissionsUsingText = mDevice.findObject(new UiSelector().text(TEXT_ALLOW_REGULAR));
while (allowPermissionsUsingText.exists()) {
allowPermissionsUsingText.click();
}
} catch (UiObjectNotFoundException ignored) {
}
}
}

private void clickAndDenyAllPermissionDialogButtons() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
UiObject denyPermissionsUsingText = mDevice.findObject(new UiSelector().text(TEXT_DENY_UPPER));
while (denyPermissionsUsingText.exists()) {
denyPermissionsUsingText.click();
}
denyPermissionsUsingText = mDevice.findObject(new UiSelector().text(TEXT_DENY_REGULAR));
while (denyPermissionsUsingText.exists()) {
denyPermissionsUsingText.click();
}
} catch (UiObjectNotFoundException ignored) {
}
}
}
/**
* Simulates a short press of a key code for each character of the text.
*
Expand Down