Android UIAutomator: How to reset value of Companion Object after closing and restarting App in TestCase?

24 Views Asked by At

I need some help performing an AndroidTest.

The normal behavior of the App is: I start the App, then I'll be ask to enter a name which is saved as an CompanionOpbject. During using the App the name will remain. Then I close the App by pressing Home Button, Swiping and pressing "CLEAR ALL" to shut down App. Pressing Home again I can start the App again and it asked again for the name because the Companion Object was reset.

I also want to do the same in the test case, it all works but unfortunately the companion Object is not reset after restarting the App. It is kept. The purpose of the test should be that is reset at the start of the App. I'm not sure if I have to reset the Companion Name before restarting the App. But it feels like manipulating the test so it will always work.

Here is my code that I use for starting the App at beginning:

    val context = ApplicationProvider.getApplicationContext<Context>()
    val intent = context.packageManager.getLaunchIntentForPackage(
        "package.name")?.apply {
        addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK )
    }
    context.startActivity(intent)


    val LAUNCH_TIMEOUT = 10L
    device.wait(Until.hasObject(By.pkg("package.name").depth(0)),
        LAUNCH_TIMEOUT
    )

Then killing the Application with Clear all:

var keepSwiping = true
    val maxSwipeAttempts = 10
    device.pressRecentApps()
    var swipeAttempt = 0
    while (swipeAttempt < maxSwipeAttempts && keepSwiping) {
        val height: Int = device.displayHeight
        val width: Int = device.displayWidth
        device.swipe(width / 2, height / 2, width, height / 2, 50)
        val clearAll1: UiObject = device.findObject(UiSelector().text("Clear all"))
        val clearAll2: UiObject =  device.findObject(UiSelector().text("CLEAR ALL"))
        val clearAll3: UiObject = device.findObject(UiSelector().textContains("Clear all"))
        val clear =
            if (clearAll1.exists()) clearAll1 else if (clearAll2.exists()) clearAll2 else clearAll3
        keepSwiping = if (clear.exists()) {
            clear.click()
            false
        } else {
            true
        }
        swipeAttempt++
    }

And restart App from home screen:

device.pressHome()
   
val appToTest: UiObject = device.findObject(UiSelector().text("App Launcher"))
appToTest.clickAndWaitForNewWindow()
   

Do I miss something? is the app not shut down correctly so that the Companion stays? Any help is appreciated.

0

There are 0 best solutions below