- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的这个测试大约有一半的时间有效。
@Test
public void thirdSwipe() {
onView(withId(R.id.pager)).perform(swipeLeft());
onView(withId(R.id.pager)).perform(swipeLeft());
onView(withId(R.id.pager)).perform(swipeLeft());
onView(allOf(withId(R.id.hint_english_text), withText("dog 0a"))).check(matches(isDisplayed()));
}
我得到了这个失败:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.
Expected: is displayed on the screen to the user
Got: "TextView{id=2131427358, res-name=hint_english_text, visibility=VISIBLE, width=624, height=62, 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=20.0, y=20.0, text=dog 0a, input-type=0, ime-target=false, has-links=false}"
因此,看起来它找到了带有 "dog 0a"
的 TextView
,但它不会识别它。我看过其他问题,我确实用 String
设置了文本,这只是我类里面的几行:
private String englishText;
englishTextView.setText(englishText);
此外,我正在使用 allOf()
。任何帮助,将不胜感激。这个 View 在 ViewPager
的 View 内,所以我不确定测试是否在 ViewPager
是 idyl 之前发生,但它确实说它找到了 View .
最佳答案
对于空转问题,您肯定是在正确的轨道上。 Espresso 无法知道在执行滑动后 ViewPager 何时完全稳定下来。正如失败所暗示的那样,您正在寻找的 View 位于 View 层次结构中的某个位置,这意味着它已经被渲染,但 Espresso 检测到它还没有出现在屏幕上。这是因为 ViewPager 还没有完全定位到您感兴趣的页面上。
下一个问题是如何告诉 Espresso 等待 ViewPager 完成。这是一个有点难以解决的问题。
消除此问题可能性的一种方法是执行 Thread.sleep
,这将迫使 Espresso 等待一段时间,希望 ViewPager 在等待结束之前稳定下来。虽然这通常适用于足够长的 sleep 持续时间,但这并不理想,因为这会为您的测试增加不必要的时间。
解决此问题的最佳方法是让 ViewPager 告诉我们何时完全完成设置。
如果您看一下 ViewPager 可用的一些监听器,其中之一是 OnPageChangeListener
。 .这个类有几个回调方法:onPageScrolled
, onPageScrollStateChanged
, 和 onPageSelected
.
onPageSelected
听起来是个不错的选择,但如果您仔细查看此方法的文档,您会发现上面写着
/**
* This method will be invoked when a new page becomes selected. Animation is not
* necessarily complete.
*/
所以这行不通。
onPageScrollStateChanged
是另一个不错的候选人。这报告了 ViewPager 中的几个状态变化,即 SCROLL_STATE_DRAGGING
, SCROLL_STATE_SETTLING
, 和 SCROLL_STATE_IDLE
.所以使用这个回调,我们可以检测到 ViewPager 何时空闲。
至于如何将此监听器的实例挂接到 ViewPager 和 Espresso,您可以查看 this implementation由 Espresso 提供,用于在 DrawerLayouts 上执行操作并类似地等待它们完成。它的要点是您创建一个自定义 ViewAction 以使用将这些监听器之一附加到 ViewPager,然后执行滑动并等待 ViewPager 报告它进入了 SCROLL_STATE_IDLE
。 .
请注意,此实现使用反射来删除和包装 DrawerListener
从 DrawerLayout 如果在附加它自己之前存在,但在 ViewPager 的情况下这不是必需的,因为 ViewPager 可以有多个 OnPageChangeListeners
附在上面。
关于Android Espresso flaky withId withText 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36415746/
我的这个测试大约有一半的时间有效。 @Test public void thirdSwipe() { onView(withId(R.id.pager)).perform(swipeL
我正在尝试编写一个简单的测试,我只需单击主 Activity 中的 MenuItem: public class doTest extends ActivityInstrumentationTestC
我正在尝试使用 espresso 为我的应用程序编写单元测试,但每当我尝试使用 withId 时,都找不到它。我该如何解决这个问题? onView 工作正常,但是当我尝试 onView(withId)
我正在使用 Espresso 进行测试,首先我打开操作栏: openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetConte
有什么区别 1.ViewInteraction v = onView(allOf(withId(R.id.login_card_view), isDisplayed())) 和 2.v.check(匹
我是一名优秀的程序员,十分优秀!