gpt4 book ai didi

android - 简单地从espresso android中的自定义 View 获取 View

转载 作者:行者123 更新时间:2023-11-28 21:37:55 24 4
gpt4 key购买 nike

我在 android 中有一个具有这种布局的自定义组件。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>

在另一个布局中使用时,我通过这段代码找到了 editText。(Espresso)

val editText = onView(
allOf(withId(R.id.editText)
, isDescendantOfA(withId(R.id.mainLayout))
, isDescendantOfA(withId(R.id.mobileEdt))
)
)

我在所有应用程序和许多布局中都使用了这个自定义组件。我可以缩小或转换为在我的应用程序中运行,因为不会一次又一次地写入吗?

也许我改变了组件布局,所以我必须在所有测试中编辑所有 withId。

最佳答案

您的组件可能有一个类名。比方说 CustomEditText。在这种情况下,您可以实现一个基于 BoundedMatcher 的自定义匹配器,确保它只匹配您的 CustomEditText 的 View 实例。

简单的实现可能是这样的:

fun customEditWithId(idMatcher: Matcher<Int>): Matcher<View> {

return object : BoundedMatcher<View, CustomEditText>(CustomEditText::class.java!!) {

override fun describeTo(description: Description) {
description.appendText("with id: ")
idMatcher.describeTo(description)
}

override fun matchesSafely(textView: CustomEditText): Boolean {
return idMatcher.matches(textView.id)
}
}
}

那么你的断言看起来像这样:

onView(customEditWithId(0)).perform(click());

关于android - 简单地从espresso android中的自定义 View 获取 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56206131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com