gpt4 book ai didi

android - 如何使用android espresso test设置textview的可见性

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:56:35 25 4
gpt4 key购买 nike

我想设置测试用例中 TextView 的可见性。我正在使用 Espresso 来测试 UI。我使用 viewAction 将文本设置为 TextView 。但我想为 TextView 设置可见性。请任何人帮助我解决这个问题。这是我将文本设置为 TextView 的代码。

public ViewAction setTextInTextView(final String value){
return new ViewAction() {
@SuppressWarnings("unchecked")
@Override
public void perform(UiController uiController, View view) {
((TextView) view).setText(value);
}

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

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

最佳答案

试试这个,

public class MainActivityInstrumentationTest {

@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void validateEditText() {

onView(withId(R.id.out)).perform(setTextViewVisibitity(true));

// Just for viewing the results. Remove after use.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

onView(withId(R.id.out)).perform(setTextViewVisibitity(false));

// Just for viewing the results. Remove after use.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private static ViewAction setTextViewVisibitity(final boolean value) {
return new ViewAction() {

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

@Override
public void perform(UiController uiController, View view) {
view.setVisibility(value ? View.VISIBLE : View.GONE);
}

@Override
public String getDescription() {
return "Show / Hide View";
}
};
}
}

关于android - 如何使用android espresso test设置textview的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47427166/

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