Espresso 在相当基本的测试中得出的结果不明确
Ambiguous results from Espresso on a fairly basic test
我们有一个页面,在某种意义上就像电子邮件一样。所以我们有一个带有一堆 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.access0(Swipe.java:31)
at android.support.test.espresso.action.Swipe.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=
为什么在第一种情况下它说无法滚动视图,而在第二种情况下它说它不够完全可见?
ViewActions.scrollTo()
不能与 RecyclerView
一起使用,因为 scrollTo 只需要使用从 ScrollView 继承的对象。要滚动到 RecyclerView
中的必要项,您应该编写自己的 ViewAction。你可以看看如何制作这个 here 见方法 scrollToPosition
当我使用 swipeDown() 时没问题,但 swipeUp() 产生了完全相同的错误:
Caused by: android.support.test.espresso.PerformException: Error performing 'click (after 3 attempts)' on view 'unknown'.
我试图 simulate/test 在片段中的回收站视图上向上滑动。当它通过回收站视图中项目数量的 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++;
}
我们有一个页面,在某种意义上就像电子邮件一样。所以我们有一个带有一堆 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.access0(Swipe.java:31)
at android.support.test.espresso.action.Swipe.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=
为什么在第一种情况下它说无法滚动视图,而在第二种情况下它说它不够完全可见?
ViewActions.scrollTo()
不能与 RecyclerView
一起使用,因为 scrollTo 只需要使用从 ScrollView 继承的对象。要滚动到 RecyclerView
中的必要项,您应该编写自己的 ViewAction。你可以看看如何制作这个 here 见方法 scrollToPosition
当我使用 swipeDown() 时没问题,但 swipeUp() 产生了完全相同的错误:
Caused by: android.support.test.espresso.PerformException: Error performing 'click (after 3 attempts)' on view 'unknown'.
我试图 simulate/test 在片段中的回收站视图上向上滑动。当它通过回收站视图中项目数量的 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++;
}