11 April 2024 Leave a comment Tech-Help
Automated UI testing on Android can be significantly enhanced with Espresso, which provides developers with a variety of methods to simulate user interactions. Common gestures like swiping left, right, up, and down are straightforward to implement. However, certain scenarios require custom swipe actions such as swiping all the way to the top or bottom of a view. In this guide, we’ll explore how to perform these custom swipe gestures using Espresso.
Custom Swipe from Top to Bottom
To perform a swipe from the top to the bottom of a view, you can utilize the GeneralSwipeAction
class with specified start and end locations. Here’s an example method to achieve a top-to-bottom swipe:
private static ViewAction swipeFromTopToBottom() {
return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER,
GeneralLocation.BOTTOM_CENTER, Press.FINGER);
}
This action will simulate a fast swipe from the top-center to the bottom-center of the view. You can call this method within your test case to perform the action on a specific view.
Customizing Start and End Points
For more control over the start and end points of your swipe, you can provide custom implementations for the coordinates provider. Here’s an example of how to override the starting Y coordinate:
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view);
coordinates[1] = 0; // Override the Y coordinate to start from the top
return coordinates;
}
}
By customizing the CoordinatesProvider
, you can adjust where the swipe begins and ends. This is particularly useful when you need to perform swipes in views with dynamic content or varying sizes.
Performing Swipes in RecyclerView
If you’re working with a RecyclerView
, you can perform swipes using the view’s ID:
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp());
This will swipe up within the RecyclerView
. Similarly, you can use swipeDown()
to swipe in the opposite direction.
Advanced Custom Swipes Without Resource IDs
For more complex scenarios where you don’t have resource IDs, such as with dynamically populated views, you can use the following method to perform a swipe:
static void swiper(int start, int end, int delay) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
Instrumentation inst = getInstrumentation();
MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 500, start, 0);
inst.sendPointerSync(event);
eventTime = SystemClock.uptimeMillis() + delay;
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 500, end, 0);
inst.sendPointerSync(event);
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 500, end, 0);
inst.sendPointerSync(event);
SystemClock.sleep(2000); // The wait is important to scroll
}
This custom method sends a series of touch events to simulate swiping on the screen. You can adjust the start and end positions along with the delay to control the swipe behavior.
Conclusion
Custom swipes in Espresso tests allow for a broader range of interactions within your automated UI testing suite. By using the provided methods and examples, you can tailor swipes to fit the unique requirements of your application, ensuring a thorough and robust testing process.