gpt4 book ai didi

java - Espresso : Getting a text value from a textview and storing in a String?

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

我想断言从 TextView 获得的“文本”的一部分,然后将其存储在字符串中,但不确定我将如何执行此操作。

以下是供引用的代码片段:

private void validateFlightOverviewWidgetDate(int resId, String value, boolean outBound) throws Throwable {
if (ProductFlavorFeatureConfiguration.getInstance().getDefaultPOS() == PointOfSaleId.UNITED_STATES) {
onView(allOf(outBound ? isDescendantOfA(withId(R.id.package_outbound_flight_widget))
: isDescendantOfA(withId(R.id.package_inbound_flight_widget)),
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),
withId(resId)))
.check(matches(withText(containsString("Dec 22 "))));

我想将“Dec 22”的值存储在一个字符串中,以便稍后我可以使用它进行断言。

最佳答案

您可能需要创建一个自定义ViewAction来帮助您从TextView获取文本:

public class GetTextAction implements ViewAction {

private CharSequence text;

@Override public Matcher<View> getConstraints() {
return isAssignableFrom(TextView.class);
}

@Override public String getDescription() {
return "get text";
}

@Override public void perform(UiController uiController, View view) {
TextView textView = (TextView) view;
text = textView.getText();
}

@Nullable
public CharSequence getText() {
return text;
}
}

然后您可以通过以下方式获取文本:

GetTextAction action = new GetTextAction();
onView(allOf(isDescendantOf(...), withId(...), withEffectiveVisibility(...)))
.perform(action);

CharSequence text = action.getText();

虽然我不建议使用这种方式进行测试断言,但它似乎非常规且笨拙。另外,由于 withId,您实际上不需要在 allOf 组合中包含 isDescendantOf(...),除非 id 不唯一。

关于java - Espresso : Getting a text value from a textview and storing in a String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53268952/

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