Alex Kolo
Alex Kolo

Reputation: 273

AndroidManifest.xml for Espresso testing

While running Espresso test with Android studio, I receive an exception while writing to internal storage: java.io.FileNotFoundException: /storage/emulated/0/MyCache: open failed: EACCES (Permission denied)

I do understand that this issue caused by lack of permissions.

My question is, do I have to create additional AndroidManifest file for Espresso test or should I use an existing AndroidManifest file from the app itself? I've tried the 2nd option, but it seems that MainActivityTest is not visible by my app's AndroidManifest.xml

Please refer to attached print screen:

project print screen

Upvotes: 4

Views: 1555

Answers (3)

denys
denys

Reputation: 6910

It is possible to access application under test (AUT) context by InstrumentationRegistry.getTargetContext(). So if you have declared needed permission in your AUT then you can use it's context.

Other possibility is to save your cache file inside AUT or test application package storage - data/data/com.your.package/files/cache_file. Then you don't need a permission at all.

Upvotes: 1

Ryan
Ryan

Reputation: 1883

You should already have that permission defined in your AndroidManifest.xml. If you are running on M+ you might be looking for something similar to this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getInstrumentation().getUiAutomation().executeShellCommand(
            "pm grant " + getTargetContext().getPackageName()
                    + " android.permission.WRITE_EXTERNAL_STORAGE");
}

this will grant you the appropriate permission for external storage access

Upvotes: 0

user7120361
user7120361

Reputation:

could not understand your question. If your target sdk is marshmallow (sdk 24) so you have to define runtime permissions. Check this out https://developer.android.com/training/permissions/requesting.html

Another solution would be to downgrade the target sdk (try to do this in your build.gradle) or create a new project with target = 23

Share some code so that we could understand the reason of the problem

Upvotes: 1

Related Questions