如何在UI测试Espresso android中超越位置启动器对话框? [英] How to surpass location enabler dialogue in UI testing Espresso android?

查看:45
本文介绍了如何在UI测试Espresso android中超越位置启动器对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Espresso进行UI测试android.我想在设置中关闭位置的情况下运行测试,但由于其他测试未通过,因此我陷入了位置启动器对话框的困扰.我提到了我的观察以及到目前为止我所做的尝试

I am using Espresso for UI testing android. I wanted to run a test with location off in the settings and I got stuck with the location enabler dialogue as it fails other test. I have mentioned my observations and what I have tried so far

  • 使用了UiAutomator,它仅适用于单个测试用例,但无法使用测试套件的完整运行.
  • 使用了Grant权限规则,它给出了权限,但对话仍然存在.
  • 使用了Roboelectric,对问题没有影响.

  • Used UiAutomator, it works only on single test case but it fails on complete run of test suite.
  • Used Grant permission rule, it gave permission but the dialogue still exists.
  • Used Roboelectric, it had no effect on the problem.

使用了Shadow操作,它对问题没有影响.

Used Shadow operation, it had no effect on the problem.

我还附上了位置对话框的示例图片.

I have also attached a sample image of the location dialogue.

谢谢.

推荐答案

当前,我正在使用GrantPerms方法,该方法应添加到所有测试类中(all =需要访问android权限的测试类),所以我只是在适当的时候调用它(如果应用需要烫发).

Currently I'm using grantPerms method which should be added to all test classes (all = test-classes which require access to android permissions) so I'm just call it at the right time (if app need 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
        }
}

或者您是说一些特定的东西吗?

Or do you mean something specific?

更新我知道了,所以我将展示一个示例,说明如何解决这一问题,对我来说这是一个很好的解决方案.

Update I see, so I will show an example how I have resolved it on my side, for me this is good solution.

  • 示例:假设您有一些与电话联系人互动的应用程序(在应用程序中先按一下"Contact btn"应引起android权限警报"Deny/Allow" btns的升高)
  • 因此,当前您的测试将具有逐行结构(访问活动/屏幕->通过某些参数(标题,ID,程序包名称等...检查UI组件的存在)->点击此组件)
  • 现在,在第一次点击之后,您应该执行android权限警报(您知道此警报应该显示在哪个操作(点击等)之后)
  • 因此您只是需要在测试类中创建内部私有方法(目前需要访问android权限)方案

  • 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 :通常您不能以编程方式打开GPS服务,这是从android v.4.2开始不允许的,因此通常最好在测试开始之前手动打开GPS服务,但是看看这个

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

}

因此,应该在您的APP中所有可能引发GPS警报()的地方调用此方法

So this method should be call in all places IN YOUR APP which suppose to raise GPS alert ()

这篇关于如何在UI测试Espresso android中超越位置启动器对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆