Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to surpass location enabler dialogue in UI testing Espresso 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

  • 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. enter image description here

Thank you.

like image 760
SURYA N Avatar asked Mar 06 '18 11:03

SURYA N


People also ask

Which is the class used in espresso framework to identify mobile test objects?

Latest development (Android 9.0, API level 28 or higher) of espresso testing framework will be done in AndroidX library. testInstrumentationRunner in the android/defaultConfig sets AndroidJUnitRunner class to run the instrumentation tests.

What tool is used for UI testing in Android Studio?

UI Automator (Android 4.3, API level 18 or higher) is a UI testing framework suitable for cross-app functional UI testing across system and installed apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher on a test device.

In which file we add espresso dependencies lines?

Add Espresso dependencies Open your app's build. gradle file. This is usually not the top-level build. gradle file but app/build.

How do you assert espresso?

check is a method which accepts an argument of type ViewAssertion and do assertion using passed in ViewAssertion object. matches(withText(“Hello”)) returns a view assertion, which will do the real job of asserting that both actual view (found using withId) and expected view (found using withText) are one and the same.


1 Answers

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

like image 113
Boris Bobrov Avatar answered Sep 28 '22 07:09

Boris Bobrov