• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

安卓 Espresso : How to match text on custom spinner selected item layout?

[复制链接]
菜鸟教程小白 发表于 2022-12-9 06:19:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有自定义布局的微调器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    androidrientation="horizontal">
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="60dp"
        android:layout_height="60dp" />
    <TextView
        android:id="@+id/tv_text"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

我需要检查所选项目的 R.id.tv_text 中的值是否与特定文本匹配。 是否可以在不实现自定义 Matcher 类的情况下做到这一点?

我的简单测试:

@SmallTest
public class CategoryTest {

    public static final String TEXT_TO_MATCH = "Spinner item text";

    @Test
    public void testCreate() {
        ...
        onView(withId(R.id.spn_tt)).perform(click()); // open spinner
        onView(allOf(withId(R.id.tv_text), withText(TEXT_TO_MATCH))).perform(click()); // find item by text and click
        onView(withId(R.id.spn_tt)).check(matches(withTextInSpinnerSelectedView(TEXT_TO_MATCH))); // check if right item selected by text
    }

}



Best Answer-推荐答案


我也使用自定义匹配器进行了归档。但我使用了一个不那么冗长的匹配器,也许它可以帮助某人:

 onView(withId(R.id.spinner)).perform(click());
 onData(allOf(is(instanceOf(YourCustomClass.class)), withMyValue("Open"))).perform(click());


public static <T> Matcher<T> withMyValue(final String name) {
    return new BaseMatcher<T>() {
        @Override
        public boolean matches(Object item) {
            return item.toString().equals(name);
        }

        @Override
        public void describeTo(Description description) {

        }
    };
}

那么你必须在你的自定义类上重写 toString() 方法。

关于安卓 Espresso : How to match text on custom spinner selected item layout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581576/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap