Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test view pager swipe gesture in Android using Espresso 2.2

I am writing automation test for View pager using Espresso 2.2 in which I need to test the swipe functionality.

I have written the below code:

 @LargeTest
 public class FirstActivityTest {

 @Rule
 public ActivityTestRule<FirstActivity> firstActivityTestRule =
        new ActivityTestRule<>( FirstActivity.class);

@Test
public void testViewPagerSwipeFunctionality() throws InterruptedException{


   onView(withId(R.id.tv_commu)).check(matches(withText(R.string.first_screen_text)));

   onView(withId(R.id.tv_skip)).check(matches(withText(R.string.skip))) ;

   onView(withId(R.id.radio_button_first)).check(matches(isChecked()));
   onView(withId(R.id.view_pager)).perform(swipLeft());

   onView(withId(R.id.radio_button_second))
            .check(matches(isChecked()));
   onView(withId(R.id.tv_comp)).check(matches(withText(R.string.second_screen_text)));

   onView(withId(R.id.tv_skip)).check(matches(withText(R.string.skip))) ;

   onView(withId(R.id.view_pager)).perform(swipeLeft());

   onView(withId(R.id.radio_button_third))
            .check(matches(isChecked()));
   onView(withId(R.id.tv_skip)).check(matches(withText(R.string.skip))) ;
    onView(withId(R.id.tv_person)).check(matches(withText(R.string.third_screen_text)));}}

However the method swipeLeft() is not getting resolved. Please let me know where I am doing wrong? Your help will be highly appreciated.

like image 549
user1740281 Avatar asked Oct 01 '15 03:10

user1740281


2 Answers

You have to import swipeLeft() like:

import static android.support.test.espresso.action.ViewActions.swipeLeft;

Side note: The example code has swipLeft() rather than swipeLeft().

like image 180
Ryan Ford Avatar answered Oct 28 '22 00:10

Ryan Ford


Even if you are able to swipe, you will see the action but the test will still fail. By default, Espresso validate 90% visibility and it fails otherwise. You need to customized the visibility yourself, basically lower it. Please refer to this solution: Espresso: How to test SwipeRefreshLayout? Hope this helps!

like image 22
Sean Das Avatar answered Oct 28 '22 00:10

Sean Das