简体   繁体   中英

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.

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().

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM