androidAndroid UI testing with Espresso
Basic Expresso cheet sheet
Lists
onData(withItemContent("item: 60"))
.onChildView(withId(R.id.item_size))
.perform(click());
onData(withItemContent("item: 60"))
.onChildView(withId(R.id.item_size))
.perform(click())
Click
ListView
onData(anything()).inAdapterView(withId(android.R.id.list)).atPosition(0)
.perform(click());
RecyclerView
onView(ViewMatchers.withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
Camera
Espresso is only able to interact with the single app under test. This means that if your app launches another app, your Espresso test code won't be able to interact with the other app at all. Launch the camera (i.e. another app) to take a photo and then return the resulting image to the original app, this becomes problematic.
Dependency
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
@Rule
public GrantPermissionRule externalStoragePermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
@Rule
public GrantPermissionRule cameraPermissionRule = GrantPermissionRule.grant(Manifest.permission.CAMERA);
@Rule
public IntentsTestRule<StopActivity> intentsRule = new IntentsTestRule<>(StopActivity.class);
Bitmap icon = BitmapFactory.decodeResource(InstrumentationRegistry.getTargetContext().getResources(), R.mipmap.ic_launcher);
Intent resultData = new Intent();
resultData.putExtra("data", icon);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
intending(toPackage("com.android.gallery")).respondWith(result);
Sources
Documentation
developer.android.com/training/testing/espresso