Sta*_*rrr 6 testing android android-espresso
我想检查
adFrame单击后更改的可见性和大小是否发生变化buttonShow
onView(withId(buttonShow)).perform(click());
onView(withId(adFrame)).check(matches(isDisplayed()));
onView(withId(adFrame)).check(?);
Run Code Online (Sandbox Code Playgroud)
我正在寻找这个问题的解决方案?。
adFrame我们是否有一个断言可以测试使用 espresso 进行此Android Instrumentation 测试的大小?
给定以下匹配器:
class WidthMatcher(private val width: Int = ViewGroup.LayoutParams.MATCH_PARENT) : TypeSafeMatcher<View>() {
companion object {
fun withWidth(width: Int) = WidthMatcher(width)
}
override fun describeTo(description: Description?) {
description?.appendText("View with width: ${width}px")
}
override fun matchesSafely(item: View): Boolean {
return item.layoutParams?.width == width
}
}
class HeightMatcher(private val height: Int = ViewGroup.LayoutParams.MATCH_PARENT) : TypeSafeMatcher<View>() {
companion object {
fun withHeight(height: Int) = HeightMatcher(height)
}
override fun describeTo(description: Description?) {
description?.appendText("View with height: ${height}px")
}
override fun matchesSafely(item: View): Boolean {
return item.layoutParams?.height == height
}
}
Run Code Online (Sandbox Code Playgroud)
您只需检查如下:
onView(withId(adFrame)).check(matches(withHeight(ViewGroup.LayoutParams.MATCH_PARENT)))
Run Code Online (Sandbox Code Playgroud)
您还可以检查任何尺寸(以 px 为单位)。您可以使用以下方法从 dp 转换为 px:
/**
* Converts dp unit to its equivalent in pixels, based on device density.
*
* @param dp The value in dp (density independent pixels) unit aim to convert into pixels
* @return The equivalent int value in px of the provided dp value depending on device density
*/
fun dpToPx(dp: Int): Int {
(dp * Resources.getSystem().displayMetrics.density).roundToInt()
}
/**
* This method converts device specific pixels to density independent pixels.
*
* @param px A value in px (pixels) unit. Which we need to convert into db
* @return The equivalent int value in dp of the provided px value depending on device density
*/
fun pxToDp(px: Int): Int {
(px / Resources.getSystem().displayMetrics.density).roundToInt()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1577 次 |
| 最近记录: |