如何在 UI 测试 Espresso android 中超越位置启动器对话?

SUR*_*A N 5 android android-permissions android-espresso

我正在使用 Espresso 进行 UI 测试 android。我想在设置中关闭位置的情况下运行测试,但由于其他测试未通过,我被定位启用对话框卡住了。我已经提到了我的观察以及到目前为止我所尝试的

  • 使用 UiAutomator,它仅适用于单个测试用例,但在完整运行测试套件时失败。
  • 使用授予权限规则,它授予权限但对话框仍然存在。
  • 用过Roboelectric,对问题没有影响。

  • 使用Shadow操作,对问题没有影响。

我还附上了位置对话的示例图像。 在此处输入图片说明

谢谢你。

Bor*_*rov 5

目前我正在使用应该添加到所有测试类(all = test-classes 需要访问 android 权限)的 grantPerms 方法,所以我只是在正确的时间调用它(如果应用程序需要 perms)。

这是方案:

     public class MyClass {
        // Rules etc.

        @Test
        public void myTestWithPerms() {
        // click view, if it's need permissions to camera etc., just call
        grantPerms(); //calling inner grantPerms method

        // Another code, if you need access to gallery now
         grantPerms(); //calling inner grantPerms method

        // Some test code
        }

        private void grantPerms(){
        // grantPermsCode
        }
}
Run Code Online (Sandbox Code Playgroud)

或者你的意思是具体的?


更新 我明白了,所以我将展示一个例子,我是如何解决它的,对我来说这是一个很好的解决方案。

  • 示例:假设您有一些与电话联系人交互的应用程序(在您的应用程序中第一次点击“联系人 btn”应该会导致 android 权限警报“拒绝/允许”btns 的提高)
  • 所以目前您的测试将具有逐行结构(访问活动/屏幕 -> 通过某些参数(标题、ID、包名称等)检查 UI 组件的存在 -> 单击此组件)
  • 现在,在第一次点击后,您应该执行 android 权限警报(您知道应该在哪个操作(点击等)之后显示此警报)
  • 所以你只需要在你的测试类(目前需要访问 android 权限)中创建一个内部私有方法,上面添加了方案


    UPDATE2:通常你不能以编程方式打开 GPS 服务,这是不允许的,因为 android v.4.2,所以通常最好在测试开始之前手动打开 GPS 服务,但看看这个

    解决方案,可能这就是你想要的:

        public class MyTestClass {
    
                // Rules etc.
    
            @Test
            public void myTestWithTurnOnGPS() {
                // once access the map check alert presence
                tapTurnOnGpsBtn(); //call inner **tapTurnOnGpsBtn** method
            }
    
            private void tapTurnOnGpsBtn() throws UiObjectNotFoundException {
    
                UiObject allowGpsBtn = device.findObject(new UiSelector()
                                                              .className("android.widget.Button").packageName("com.google.android.gms")
                                                              .resourceId("android:id/button1")
                                                              .clickable(true).checkable(false));
                device.pressDelete(); // just in case to turn ON blur screen (not a wake up) for some devices like HTC and some other
                if (allowGpsBtn.exists() && allowGpsBtn.isEnabled()) {
                    do {
                        allowGpsBtn.click();
                    } while (allowGpsBtn.exists());
                }
           }
    
    Run Code Online (Sandbox Code Playgroud)

    }

所以这个方法应该在你的应用程序中的所有地方调用,这些地方假设会引发 GPS 警报 ()