So far we can do:
- swipeLeft
- swipeRight
- swipeUp
- swipeDown
How can we swipeTop(all the way to the Top) or swipeBottom(all the way to the bottom) is expresso. Please give me an example if these methods already exists.
So far we can do:
How can we swipeTop(all the way to the Top) or swipeBottom(all the way to the bottom) is expresso. Please give me an example if these methods already exists.
Have you tried a GeneralSwipeAction
like that?
private static ViewAction swipeFromTopToBottom() {
return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER,
GeneralLocation.BOTTOM_CENTER, Press.FINGER);
}
Maybe you have to provide a custom implementation for the second and/or third parameter, as Anna already mentioned:
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view);
coordinates[1] = 0;
return coordinates;
}
}
For example if you use recyclerView in yours application, you can use something like this:
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp())
or
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeDown())
@testsingh i think there's no out-off-box method to do the "swipeTop" or "swipeBottom" following looping maybe the easiest way (dumbest) that worked for me XDDD
//swipeUp 100 times, vice versa for swipeDown
for(int i=0;i<=100;i++){
onView(withText("label")).perform(swipeUp());
}
I know I am late to the party but after quite a bit of finagling I finally found something that can swipe top to bottom, left to right, etc. - without needing any resource id's.
The reason I needed it was for a dynamically populated view where everything was completely ambiguous. With the method below I can scroll all the way to the bottom and or even change the delay to scroll down just a single page.
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
}
I do not need left to right, etc. so I hardcoded the 500 in there (500 being the x axis).
and to call them I did it I did this -
// This swipes all the way to the bottom of the screen
public static void swipeToBottom(){
swiper(1000, 100, 0);
}
// This scrolls down one page at a time
public static void scrollSlowlyDown(){
swiper(775, 100, 100);
}
// This swipes to the top
public static void swipeToTop(){
swiper(100, 1000, 0);
}
// This scrolls up one page at a time
public static void scrollSlowlyUp(){
swiper(100, 775, 100);
}
I hope this helps anyone that stumbles up this.
I think it could be done by writing complex ViewActions
by performing swipe.
public static ViewAction swipeToTop() {
return new MySwipeAction(Swipe.FAST,
GeneralLocation.CENTER,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view);
coordinates[1] = 0;
return coordinates;
}
}, Press.FINGER);
}
public static ViewAction swipeToBottom() {
return new MySwipeAction(Swipe.FAST,
GeneralLocation.CENTER,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
float[] coordinates = GeneralLocation.CENTER.calculateCoordinates(view);
coordinates[1] = view.getContext().getResources().getDisplayMetrics().heightPixels;
return coordinates;
}
}, Press.FINGER);
}
where MySwipeAction could be something like this:
public class MySwipeAction implements ViewAction {
public MySwipeAction(Swiper swiper, CoordinatesProvider startCoordProvide, CoordinatesProvider endCoordProvide, PrecisionDescriber precDesc) {
// store here in class variables to use in perform
...
}
@Override public Matcher<View> getConstraints() {...}
@Override public String getDescription() {...}
@Override
public void perform(UiController uiController, View view) {
...
float[] startCoord = startCoordProvide.calculateCoordinates(view);
float[] finalCoord = endCoordProvide.calculateCoordinates(view);
float[] precision = precDesc.describePrecision();
Swiper.Status status = Swiper.Status.FAILURE;
// you could try this for several times until Swiper.Status is achieved or try count is reached
try {
status = m_swiper.sendSwipe(uiController, startCoord, finalCoord, precision);
} catch (RuntimeException re) {
...
}
// ensures that the swipe has been run.
uiController.loopMainThreadForAtLeast(ViewConfiguration.getPressedStateDuration());
}
}
I hope this could help you.
// swipe up
for(int i=0;i<=3;i++){
onView(allOf(withId(R.id.recycler_view),isDisplayed())).perform(actionOnItemAtPosition(4, swipeUp()));
}
// swipe Down
for(int i=0;i<=3;i++){
onView(allOf(withId(R.id.recycler_view),isDisplayed())).perform(actionOnItemAtPosition(7, swipeDown()));
}