Espresso在相当基本的测试中产生的模糊结果

时间:2016-05-10 21:21:15

标签: android unit-testing android-espresso

我们有一个页面,在某种意义上就像电子邮件一样。所以我们有一堆带有TextView的Recyclerview,其中一个是包含所有电子邮件内容的大型TextView。

所以我们只是试图测试电子邮件的整个文本是否已加载并显示(我们在最后附加一个特殊的字符串,我们将测试它是否显示出来)。

Espresso.onView( Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withId(R.id.email_content)) )
            .perform(ViewActions.scrollTo(), ViewActions.swipeUp());

执行此操作时,我们会收到此错误:

Caused by: android.support.test.espresso.PerformException: Error performing 'click (after 3 attempts)' on view 'unknown'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.action.MotionEvents.sendDown(MotionEvents.java:111)
at android.support.test.espresso.action.Swipe.sendLinearSwipe(Swipe.java:88)
at android.support.test.espresso.action.Swipe.access$100(Swipe.java:31)
at android.support.test.espresso.action.Swipe$1.sendSwipe(Swipe.java:38)
at android.support.test.espresso.action.GeneralSwipeAction.perform(GeneralSwipeAction.java:70)

所以当我们改变它时:

    Espresso.onView( Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.withId(R.id.email_content)) )
            .perform(ViewActions.swipeUp());

我们收到此错误:

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: 
at least 90 percent of the view's area is displayed to the user.
Target view: "AppCompatTextView{id=2131558822, res-name=email_content, visibility=VISIBLE, width=1048, height=1513896, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=16.0, y=175.0, text=

为什么在第一种情况下它表示无法滚动视图?在第二种情况下,它表示它不够完全可见?

2 个答案:

答案 0 :(得分:1)

ViewActions.scrollTo()不能与RecyclerView一起使用,因为scrollTo只需要处理从ScrollView继承的对象。要滚动到RecyclerView中的必要项,您应该编写自己的ViewAction。您可以查看如何制作此here方法scrollToPosition

答案 1 :(得分:0)

当我使用swipeDown()时没关系,但是swipeUp()产生了完全相同的错误:

Caused by: android.support.test.espresso.PerformException: Error performing 'click (after 3 attempts)' on view 'unknown'.

我试图模拟/测试片段中的回收器视图。它将从服务器获取新数据,因为它在回收站视图中传递了60%的项目。

Android Studio 2.3.1

Windows 7

Genymotion三星S6

我在下面完成了模拟行为的临时解决方案。希望这能帮助完全相同的行为和错误的人。

MyFragment.java

        ...
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            ...
            else if(mainActivity.isAutomatedTesting && dy < 0) //check for scroll up. isAutomatedTesting is set to true in MyTest.java
            {
                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
                lastVisibleItemPos = mLayoutManager.findLastCompletelyVisibleItemPosition();

                if (loading)
                {
                    if ( (lastVisibleItemPos / totalItemCount) >= listTolerance)
                    {                            
                        //fetch new data
                    }
                }
            }
            ...

MyTest.java

        //wait for first lot of data to finish loading
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        int count = 1;
        int maxCount = 4;
        while(count < maxCount) {

            onView(withId(R.id.recycler_view))
                    .perform(RecyclerViewActions.scrollToPosition(count * 9));
            onView(withId(R.id.recycler_view))
                    .perform(swipeDown());

            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            count++;
        }