如何在UI测试Espresso Android中跳过位置启用对话框?

6
我正在使用Espresso进行Android UI测试。我想在设置中关闭位置后运行测试,但我因为位置启用对话框而卡住了,这会影响其他测试。我已经提到了我的观察和尝试过的方法。
  • 使用UiAutomator,它只适用于单个测试用例,但在完整的测试套件运行中失败。
  • 使用授予权限规则,它授予了权限,但对话框仍然存在。
  • 使用Roboelectric,对问题没有影响。

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

我还附上了位置对话框的示例图像。 enter image description here

谢谢。

1个回答

7

我目前正在使用grantPerms方法,该方法应添加到所有测试类(所有需要访问Android权限的测试类),因此只需在正确的时间调用它(如果应用程序需要权限)。

以下是方案:

     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
        }
}

你是指有具体的问题需要解决吗?


更新我明白了,我将展示一个我在自己的项目中如何解决它的例子,对我来说这是一个好的解决方案。

  • example: imagine that you have some app which interact with phone contacts (1-st tap "Contact btn" in your app should cause raise of android permissions alert "Deny/Allow" btns)
  • so currently your test will have a line-wise structure (access activity/screen -> check presence of UI component by certain params (title, ID, package name etc...) -> click on this component)
  • NOW after first tap you should perform android permissions alert (YOU know after which action (tap etc.) this alert suppose to be shown)
  • so YOU just need to create a inner private method inside your test class (which currently require access to android permissions) scheme was added above


    UPDATE2: generally you can't turn ON GPS services programmatically, this is not allowed since android v.4.2, so generally it's better to turn ON GPS services manually before test was started, but take a look at this

    solution, may be this is what you want:

        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());
                }
           }
    

    }

因此,应该在您的应用程序中的所有位置调用这个方法,以便触发 GPS 警报 ()。


我是指提供实时权限弹出窗口,以便为该解决方案崩溃测试。 - SURYA N
是的,它几乎相同,但问题在于如果我运行单个文件测试,它可以完美运行。如果运行整个UI文件,则由于弹出窗口阻止了测试,测试会崩溃。 - SURYA N
我已经编辑了我的评论,通常情况下,你应该在每个需要引发Android权限警报的操作之后调用内部授予权限方法(允许Android权限)。你需要授予权限的代码示例和使用样例吗? - Boris Bobrov
你有跟进上一个评论吗? - Boris Bobrov
它起作用了,你的代码没有问题。问题出在我的代码上,在重构后它通过了。谢谢,伙计。 - SURYA N
显示剩余13条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接