Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso --> Java.lang.SecurityException: test fails due to Package does not belong to <process>

I have an Android Espresso code that attempts to click a button and it fails with a security error.

This is the Espresso command:

Espresso.onData(Matchers.allOf(Matchers.is(Matchers.instanceOf(Preference.class)), withKey(PreferenceKey.pref_custom_server_base_url.toString()), withSummaryText(Configurations.DEFAULT_SERVER_URL))).check(matches(isCompletelyDisplayed()));

This is the error I am receiving:

java.lang.SecurityException: Package xxx.xxx.test does not belong to 10095
at android.os.Parcel.readException(Parcel.java:2004)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:651)
at android.provider.Settings$NameValueCache.getStringForUser(Settings.java:1924)
at android.provider.Settings$Global.getStringForUser(Settings.java:10362)
at android.provider.Settings$Global.getString(Settings.java:10351)
at android.provider.Settings$Global.getFloat(Settings.java:10695)
at android.support.test.espresso.base.DefaultFailureHandler.getGlobalSetting(DefaultFailureHandler.java:178)
at android.support.test.espresso.base.DefaultFailureHandler.getSetting(DefaultFailureHandler.java:154)
at android.support.test.espresso.base.DefaultFailureHandler.getTransitionAnimationScale(DefaultFailureHandler.java:124)
at android.support.test.espresso.base.DefaultFailureHandler.isAnimationAndTransitionDisabled(DefaultFailureHandler.java:112)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:69)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at xxx.xxx.test.instruments.failure.ScreenshotFailureHandler.handle(ScreenshotFailureHandler.java:36)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher$1.apply(DataInteraction.java:206)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher$1.apply(DataInteraction.java:203)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:223)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:198)
at android.support.test.espresso.DataInteraction$DisplayDataMatcher.displayDataMatcher(DataInteraction.java:241)
at android.support.test.espresso.DataInteraction.makeTargetMatcher(DataInteraction.java:143)
at android.support.test.espresso.DataInteraction.check(DataInteraction.java:137)
at xxx.xxx.test.steps.ChangeServerUrlSteps.checkSettingsSaved(ChangeServerUrlSteps.java:112)
at xxx.xxx.test.steps.Prerequisites.serverConfigPrerequisites(Prerequisites.java:38)
at ✽.Given I am connected (features/001_login.feature:8)

Any idea why this may happen? Thanks! The code runs on Emulators and Cloud devices with the same error.

like image 922
Pavel Zagalsky Avatar asked Feb 25 '18 17:02

Pavel Zagalsky


1 Answers

I found a solution to the problem in the answer of this stackoverflow question: java.lang.SecurityException: Permission Denial: getIntentSender() when using UiAutomation in a test

When specifying the location of the database, use getTargetContext() instead of getContext() i.e.

File dbFolder = InstrumentationRegistry.getInstrumentation().getTargetContext().getExternalCacheDir();

instead of

File dbFolder = InstrumentationRegistry.getInstrumentation().getContext().getExternalCacheDir();
like image 130
Aldinjo Avatar answered Oct 11 '22 03:10

Aldinjo