【问题标题】:Can't click Allow button in permission dialog in Android using Appium无法使用 Appium 在 Android 的权限对话框中单击允许按钮
【发布时间】:2017-02-28 13:45:20
【问题描述】:

我无法在使用 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 

如果有人帮助解决这个问题,我们将不胜感激?

【问题讨论】:

    标签: java android selenium selenium-webdriver appium


    【解决方案1】:

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

    下面一行对我有用.... driver.findElement(MobileBy.id("com.android.packageinstaller:id/permission_allow_button")).click();

    【讨论】:

    • 对于Android Q有问题的人,id是com.android.permissioncontroller:id/permission_allow_foreground_only_button
    【解决方案2】:

    使用下面的 sn-p,我可以单击所有允许按钮来获取权限。

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

    【讨论】:

      【解决方案3】:

      从 appium 1.6.3 开始,您只需添加:

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

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

      【讨论】:

        【解决方案4】:

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

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

        检测到这个activity后,需要通过locator(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

        【讨论】:

        • 感谢您的快速回复。我以前尝试过,但没有成功。这就是在 SO 中发布问题的原因。但我没有尝试 'pm grant $app_name $space_delimited_set_of_perms' 。你能告诉我在'$space_delimited_set_of_perms'需要做什么吗?谢谢
        • 您可以使用 adb 的简单命令(在引号中-忽略引号),“adb shell pm grant com.name.app android.permission.READ_PROFILE”,应用程序名称来授予您的应用程序权限输入您正在测试的应用程序的包 ID
        • 我在现有代码中添加了以下几行。但没有成功。你能看一下这个,如果我做错了,请告诉我。 ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", "/usr/local/bin/adb shell pm grant android.permission.READ_PROFILE"); builder.redirectErrorStream(true); builder.start();
        • 1.此代码应在安装应用程序后工作。 2.我提到的权限活动是一个例子,你应该使用适合你目的的权限,你可以在这个页面上查看权限活动列表developer.android.com/guide/topics/security/permissions.html
        【解决方案5】:

        在找到解决方法之前,我尝试了很多解决方案。 我启用了触控球/快速球功能,并且使用的图标始终保持在屏幕上以帮助我导航。

        这个应用程序是启用了覆盖功能的罪魁祸首,通过浏览已安装应用程序下的活动应用程序并不容易找出它。

        注意:我的是 MIUI 11 和 Android 7 for Redmi Note 4。

        【讨论】:

          猜你喜欢
          • 2018-05-06
          • 2021-03-16
          • 2018-12-12
          • 1970-01-01
          • 2020-03-26
          • 1970-01-01
          • 2012-07-06
          • 2019-10-14
          • 1970-01-01
          相关资源
          最近更新 更多