android / testing-samples

A collection of samples demonstrating different frameworks and techniques for automated testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Espresso]Test instrumentation process crashed. After click action app should restarted but with espresso is crashed

MohamedGaberM opened this issue · comments

Hi all
Steps to reproduce :
1-I just create a simple test case (Just click on the button)
2-After clicking on this button, the app should be restarted (Expected Result)
3-Testcase will fail and I get this error (Actual Result)
(Test instrumentation process crashed. Check tests.SplashActivityTest#splashActivityTest.txt for details)
The data on file mentioned before :
INSTRUMENTATION_RESULT: shortMsg=Process crashed.
INSTRUMENTATION_CODE: 0

Prerequisites:

android {
defaultConfig {
...
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        testInstrumentationRunnerArguments clearPackageData:'true'

} }
...
    testOptions {
        unitTests.returnDefaultValues = true
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }
    .....
    dependencies {
     // Espresso Dependencies
    androidTestImplementation 'androidx.test:core:1.5.0-beta01'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4-beta01'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-beta01'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'androidx.test:rules:1.4.1-beta01'
    androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.0-beta01'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0'
    androidTestImplementation 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0'
    androidTestImplementation('com.schibsted.spain:barista:3.6.0') {
        exclude group: 'org.jetbrains.kotlin'
    } 

    //   orchestrator
    androidTestUtil 'androidx.test:orchestrator:1.4.1'
    }

I try with API 31 and 33 with Emulator and real devices too

Can any one here help me on this issue?

Thanks

Can you provide a complete sample and/or a logcat? The list of dependencies look a bit strange with a mixture of very old and new dependencies. eg The ' androidTestImplementation 'com.android.support.test:rules:1.0.2'' especially seems wrong

Can you provide a complete sample and/or a logcat? The list of dependencies look a bit strange with a mixture of very old and new dependencies. eg The ' androidTestImplementation 'com.android.support.test:rules:1.0.2'' especially seems wrong

This is the complete very simple test case
and no problem with ' androidTestImplementation 'com.android.support.test:rules:1.0.2'' especially seems wrong
i remove it and the same error happen

@RunWith(AndroidJUnit4.class)
public class SplashActivityTest{

    @Rule
    public ActivityScenarioRule<SplashActivity> mActivityScenarioRule =
            new ActivityScenarioRule<>(SplashActivity.class);

    @Test
    public void splashActivityTest(){

        onView(withId(R.id.preliveButton)).perform(click());

Sorry but without a full sample or logcat our ability to help is going to be limited. I tried copying over your list of dependencies into a simple sample project and it worked fine. You can use one of http://github/android/testing-samples as a base

Oh I just noticed in your description "2-After clicking on this button, the app should be restarted (Expected Result)'. You cannot restart the app from within an instrumented test, because that will kill the process the test is running in.

If you need to do this you might want to look into using UiAutomator, and setting up your tests so they run in a different process than the application under test.

Collaborator

but this is a hard restart, this button should be clicked at the start of the process to can select the environment
and after clicking, the app will hard restart.
so can't handle this case with espresso?

Oh I just noticed in your description "2-After clicking on this button, the app should be restarted (Expected Result)'. You cannot restart the app from within an instrumented test, because that will kill the process the test is running in.

If you need to do this you might want to look into using UiAutomator, and setting up your tests so they run in a different process than the application under test.

and how can i use UiAutomator to can continue my test cases after restarting app?

Espresso will wait for the app to be idle after the click is sent, so I strongly suspect this case cannot be handled in espresso. How does the code under test restart the app?

For UiAutomator, check out https://github.com/android/testing-samples/tree/main/ui/uiautomator/BasicSample. You'll likely need to adjust the targetProcess here

<instrumentation android:targetPackage="com.example.android.testing.uiautomator.BasicSample"
to com.example.android.testing.uiautomator.BasicSample.test and perhaps make other changes

Espresso will wait for the app to be idle after the click is sent, so I strongly suspect this case cannot be handled in espresso. How does the code under test restart the app?

For UiAutomator, check out https://github.com/android/testing-samples/tree/main/ui/uiautomator/BasicSample. You'll likely need to adjust the targetProcess here

<instrumentation android:targetPackage="com.example.android.testing.uiautomator.BasicSample"

to com.example.android.testing.uiautomator.BasicSample.test and perhaps make other changes

The screen asks you to choose the required data then the app restarts with the data you choose before.

Intent i = activity.getBaseContext().getPackageManager().getLaunchIntentForPackage(activity.getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(i);
activity.finishAffinity();
System.exit(0);
```

> and i will check uiautomator, Thanks

Yeah there isn't a way to handle a System.exit(0) from an instrumentation test that runs in the same process

Yeah there isn't a way to handle a System.exit(0) from an instrumentation test that runs in the same process

yea I tried UI Automator and instrumentation crushed also, so is there any way to handle this case?

Did you configure the UI Automator instrumentation to run in a different process than the app under test? See the manifest link I gave above