Espresso 会检测主线程何时处于空闲状态,以便可以在适当的时间运行测试命令,从而提高测试的可靠性。
先上示例代码:来自官网
@Test
fun greeterSaysHello() {
onView(withId(R.id.name_field)).perform(typeText("Steve"))
onView(withId(R.id.greet_button)).perform(click())
onView(withText("Hello Steve!")).check(matches(isDisplayed()))
}
很简单的代码。
场景:如果按钮 greet_button 的点击事件触发后,“Hello Steve!” 文本将在访问接口后才会显示,这个时间是不确定性的,所以给它添加 Thread.sleep() 也就不可行了。
空闲资源:
Espresso的空闲资源表示结果会影响界面测试中后续操作的异步操作。通过向 Espresso 注册空闲资源,您可以在测试应用时更可靠地验证这些异步操作。
⚠️ 这里我们虽然可以使用Espresso空闲资源来处理(使用 IdlingRegistry 类),但是 应用的生产代码中存在空闲资源逻辑,这是不推荐的。
处理方案:
根据如下匹配方法: ViewAssertions.matches()
public static ViewAssertion matches(final Matcher super View> viewMatcher) {
return new MatchesViewAssertion(checkNotNull(viewMatcher));
}
重写 MatchesViewAssertion() :
class WaitingNotMatchViewException(override val message: String) : RuntimeException() // 简便易使用 fun waitMatches(viewMatcher: Matcher): ViewAssertion { return WaitMatchesViewAssertion(checkNotNull(viewMatcher)) } @VisibleForTesting class WaitMatchesViewAssertion(private val viewMatcher: Matcher ) : ViewAssertion { private val TAG = WaitMatchesViewAssertion::class.java.simpleName override fun check(view: View?, noViewException: NoMatchingViewException?) { val description = StringDescription() description.appendText("'") viewMatcher.describeTo(description) if (noViewException != null) { description.appendText( String.format( Locale.ROOT, "' check could not be performed because view '%s' was not found.n", noViewException.viewMatcherDescription ) ) Log.e(TAG, description.toString()) throw noViewException } else { // 添加 try-catch try { description.appendText("' doesn't match the selected view.") ViewMatchers.assertThat(description.toString(), view, viewMatcher) }catch (e: AssertionFailedError){ // 抛出异常 WaitingNotMatchViewException throw WaitingNotMatchViewException("doesn't match the selected view.") } } } override fun toString(): String { return String.format(Locale.ROOT, "MatchesViewAssertion{viewMatcher=%s}", viewMatcher) } }
封装:
fun waitForElementWithMatcher(
elementToCheck: ViewInteraction, // 要检查的元素
viewMatcher: Matcher, // 匹配项
secondsToWait: Int = 30 // 超时时间
) {
var i = 0
var elementMatched = false
while (i <= secondsToWait) {
if (IntegrationUtil.waitElementToViewMatcher(elementToCheck, viewMatcher)) {
elementMatched = true
break
}
Thread.sleep(1000) // 这里的时间可以自己设定
i++
}
if (!elementMatched) {
throw Exception("Expected to find the element on Screen .Waited for $i seconds")
}
}
fun waitElementToViewMatcher(
elementToCheck: ViewInteraction,
viewMatcher: Matcher
): Boolean {
try {
val elementList = mutableListOf(elementToCheck.check(waitMatches(viewMatcher)))
if (elementList.count() == 1)
return true
} catch (e: NoMatchingViewException) {
return false
} catch (e: WaitingNotMatchViewException) {
return false
} catch (e: Exception) {
throw Exception(e.message)
}
return false
}
使用:
@Test
fun greeterSaysHello() {
onView(withId(R.id.name_field)).perform(typeText("Steve"))
onView(withId(R.id.greet_button)).perform(click())
// onView(withText("Hello Steve!")).check(matches(isDisplayed()))
waitForElementWithMatcher(onView(withText("Hello Steve!")), isDisplayed(), 30)
}
最后,欢迎讨论。



