无法使用Appium在Android中的权限对话框中单击“允许”按钮

时间:2016-10-19 05:06:24

标签: java android selenium selenium-webdriver appium

我无法使用Appium + Java在Android的权限对话框中点击“拒绝”或“允许”按钮。在点击这些按钮之前,是否需要添加任何功能? 以下是代码:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "ASUS_Z00LD");
capabilities.setCapability("platformVersion", "6.0");
capabilities.setCapability("app","<AppPath>");
capabilities.setCapability("browserName", "");
AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        Thread.sleep(10000);
driver.findElement(MobileBy.id("permission_allow_button")).click();

以下是Eclipse控制台中的错误:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

下面是Appium日志:

info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"6.0.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","browserName":"","platformName":"Android","deviceName":"ASUS_Z00LD","platformVersion":"6.0"},"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","platformName":"Android","deviceName":"FAAZCY127084"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}
info: <-- GET /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e 200 1.534 ms - 625 {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"6.0.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","browserName":"","platformName":"Android","deviceName":"ASUS_Z00LD","platformVersion":"6.0"},"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","platformName":"Android","deviceName":"FAAZCY127084"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}

info: --> POST /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e/element {"using":"id","value":"permission_allow_button"}

info: [debug] Waiting up to 0ms for condition

info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"permission_allow_button","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"permission_allow_button","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding permission_allow_button using ID with the contextId:  multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.****.****:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=permission_allow_button, INSTANCE=0]

info: [debug] [BOOTSTRAP] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
info: [debug] [BOOTSTRAP] [debug] Finding permission_allow_button using ID with the contextId:  multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.****.****:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=permission_allow_button, INSTANCE=0]

info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"No element found"}

info: [debug] Condition unmet after 178ms. Timing out.

info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"No element found"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}
info: <-- POST /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e/element 500 181.769 ms - 195 

任何人帮助克服这一点都会受到赞赏吗?

5 个答案:

答案 0 :(得分:6)

使用完整的资源ID ......它对我有用....

下面的行为我工作.... driver.findElement(MobileBy.id(&#34; com.android.packageinstaller:id / permission_allow_button&#34;))。click();

答案 1 :(得分:5)

使用下面的代码片段,我可以点击所有允许按钮来获取权限。

while (driver.findElements(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).size()>0) {
    driver.findElement(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).click();
}

答案 2 :(得分:1)

Appium为您提供了一个检测活动的API。根据您的设备,您可以获得两项活动 - 包名称可能会被剥离或不被删除:

'com.android.packageinstaller.permission.ui.GrantPermissionsActivity',
'.permission.ui.GrantPermissionsActivity'

检测到此活动后,您需要按定位器(id / xpath)找到一个元素:

'com.android.packageinstaller:id/permission_message'

如果您对此消息感兴趣,可以获取该消息的文本。如果您关心它是哪个权限,则可以将其与预期的字符串或正则表达式进行匹配。如果没有,您可以通过ID找到并单击元素来盲目接受:

'com.android.packageinstaller:id/permission_allow_button'

如果您不想在所有这些窗口上单击“允许”,则可以在开始测试之前使用adb一次性添加所有权限(但在Appium安装了您的应用程序之后)。如果您知道应用程序需要的所有权限,则可以使用以下命令添加它们:

pm grant $app_name $space_delimited_set_of_perms

或者您可以一次添加一个权限,每次尝试需要1.5-2秒。

参考:https://discuss.appium.io/t/android-m-and-permissions/5760/13

答案 3 :(得分:0)

从appium 1.6.3开始,你可以添加:

capabilities.setCapability("autoGrantPermissions", "true");

并且您将始终允许您的应用所需的所有权限。

答案 4 :(得分:0)

在找到解决方案之前,我尝试了许多解决方案。 我已启用触摸球/快速球功能,并且所使用的图标始终显示在屏幕上,以帮助我导航。

此应用程序的罪魁祸首是启用了叠加功能,并且通过浏览已安装应用程序下的活动应用程序来查找它并不容易。

注意:我的设备是用于Redmi Note 4的Android 7的MIUI 11。