How to surpass location enabler dialogue in UI testing Espresso android?

[亡魂溺海] 提交于 2021-01-27 06:46:08

问题


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

  • 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.

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

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

Thank you.


回答1:


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).

Here is scheme:

     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.

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

    }

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



来源:https://stackoverflow.com/questions/49129710/how-to-surpass-location-enabler-dialogue-in-ui-testing-espresso-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!