square / spoon

Distributing instrumentation tests to all your Androids.
https://square.github.io/spoon/
Apache License 2.0
2.7k stars 477 forks source link

--clear-app-data interferes with permissions #562

Open ssaqua opened 5 years ago

ssaqua commented 5 years ago

The --clear-app-data option added in #535 interferes with any permissions setup by Spoon such as read/write external storage or those granted through --grant-all. Since these permissions are not granted again after the pm clear command, functionality that you might reasonably expect to work (screenshots, code coverage, files) will no longer work unless granted again in some other way.

stephane-ducornet-tagheuer commented 4 years ago

Hello, is there a solution or a workaround ?

stephane-ducornet-tagheuer commented 4 years ago

Finally, I found the solution:

  1. Set the clearAppDataBeforeEachTest in Gradle:

    spoon { ... clearAppDataBeforeEachTest = true }

  2. Set permissions into the Maniest

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
  1. And add permissions into the test (Kotlin):

class PurchaseTests : BaseTest() {

companion object {
    @ClassRule
    @JvmField
    val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
        android.Manifest.permission.READ_EXTERNAL_STORAGE
    )
}

@Test
fun userShouldAddItemToCart() {
    ...
}

}