3

I have an android app. I am doing automated testing of this app using uiautomator. Before doing any processing I need to login the app. But at first time it store the data in cache and login automatically every time when I launch.

I want application should be logged in every time with filled credential.

Is there any way to stop this caching by using uiautomator api.

Rahul Jain
  • 658
  • 5
  • 20
  • Possible duplicate of [Android Studio -- clear application data for Instrumentation Test](http://stackoverflow.com/questions/31713579/android-studio-clear-application-data-for-instrumentation-test) – WindRider Sep 14 '16 at 15:44

3 Answers3

2

For those of you who wants to clear your target app under test, while running UiAutomator, you could probably try the method I wrote below. This only works with API level 18 or greater.

http://developer.android.com/reference/android/app/UiAutomation.html#executeShellCommand(java.lang.String)

public static void clearData (String packageName) {

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            InstrumentationRegistry.getInstrumentation().getUiAutomation()
                    .executeShellCommand("pm clear " + packageName)
                    .close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
David Kim
  • 164
  • 1
  • 9
  • 1
    when i try this with the latest Uiautomator 2.0 i get: INSTRUMENTATION_RESULT: shortMsg=Process crashed. – Tim Boland Sep 13 '16 at 21:28
  • This could help you: http://stackoverflow.com/questions/31713579/android-studio-clear-application-data-for-instrumentation-test – WindRider Sep 14 '16 at 15:50
0

You can give below command before you start your tests, this would clear you application data. Then start your tests !

adb shell pm clear yourPackageName
Anvesh Yalamarthy
  • 1,625
  • 20
  • 36
  • Can we run more than one test cases by a single command using uiautomator ? – Rahul Jain Nov 27 '14 at 05:27
  • Unfortunately NO... but can check below framework, Ive not tested it personally though.. http://sourceforge.net/projects/uiautomator/ – Anvesh Yalamarthy Nov 27 '14 at 11:39
  • This is not the correct answer. Even on a basic note, someone can see that any shell script can be automated. Typing a command in every time you need to cache bust is a bad suggestion. – Joseph Casey Mar 21 '17 at 16:36
  • Yes, you are correct.. Did I say we can't automate it ? :).. I just gave a pointer to address the problem :) – Anvesh Yalamarthy Mar 21 '17 at 18:22
0

In your app you can manually first clear the data or disable the "Save my credential" option in the app. So every time when starting the app it will ask for the credentials again.

Or else you can directly write it in command prompt before running the test case.

adb shell pm clear yourPackageName
harsh pamnani
  • 1,191
  • 1
  • 10
  • 14