gpt4 book ai didi

android - 在 Espresso 中断言 EditText 值

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:28 25 4
gpt4 key购买 nike

我们可以对 Edittext Value 执行断言并根据它的输出写下我们的测试用例吗?就像如果我们的 Edittext 值等于我们想要执行条件 A else B 的值。

 onView(withId(viewId)).check(matches(isEditTextValueEqualTo(viewId, value)));

Matcher<View> isEditTextValueEqualTo(final int viewId, final String content) {

return new TypeSafeMatcher<View>() {

@Override
public void describeTo(Description description) {
description.appendText("Match Edit Text Value with View ID Value : : " + content);
}

@Override
public boolean matchesSafely(View view) {
if (view != null) {
String editTextValue = ((EditText) view.findViewById(viewId)).getText().toString();

if (editTextValue.equalsIgnoreCase(content)) {
return true;
}
}
return false;
}
};
}

使用 try..Catch(Exception e) 无效

最佳答案

我认为您不应该在匹配器中执行 findViewById,我认为没有理由这样做。

我已经更新了你的匹配器:

Matcher<View> isEditTextValueEqualTo(final String content) {

return new TypeSafeMatcher<View>() {

@Override
public void describeTo(Description description) {
description.appendText("Match Edit Text Value with View ID Value : : " + content);
}

@Override
public boolean matchesSafely(View view) {
if (!(view instanceof TextView) && !(view instanceof EditText)) {
return false;
}
if (view != null) {
String text;
if (view instanceof TextView) {
text =((TextView) view).getText().toString();
} else {
text =((EditText) view).getText().toString();
}

return (text.equalsIgnoreCase(content));
}
return false;
}
};
}

这样调用它:

onView(withId(viewId)).check(matches(isEditTextValueEqualTo(value)));

关于android - 在 Espresso 中断言 EditText 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586977/

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