如何使用Espresso 2.2在Android中测试查看寻呼机滑动手势

时间:2015-10-01 03:57:25

标签: android automation android-viewpager android-espresso

我正在使用Espresso 2.2为View pager编写自动化测试,我需要在其中测试滑动功能。

我写了以下代码:

 @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)));}}

然而,方法swipeLeft()没有得到解决。请告诉我我在哪里做错了?我们将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:11)

您必须导入swipeLeft(),如:

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

旁注: 示例代码具有swipLeft()而不是swipeLeft()。

答案 1 :(得分:2)

即使你能够滑动,你也会看到动作,但测试仍然会失败。默认情况下,Espresso会验证90%的可见性,否则会失败。您需要自己定制可见性,基本上降低它。请参考此解决方案: Espresso: How to test SwipeRefreshLayout? 希望这有帮助!