gpt4 book ai didi

android-espresso - Espresso 2-如何测试多项事件?

转载 作者:行者123 更新时间:2023-12-04 15:54:32 26 4
gpt4 key购买 nike

我已经看到了一些关于它的问题。

例如。 Android Espresso testing app flow

但是上面的答案在espresso 2中不起作用。这是我的摘录

@Rule
public ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);


@Test
public void splashActivityTest() {
onView(withId(R.id.splash_container)).perform(swipeLeft());
onView(withId(R.id.splash_container)).perform(swipeLeft());

// launch the main activity
ViewInteraction appCompatButton = onView(
allOf(withId(R.id.introduction_goto_btn), withText("goToMainActivity"), isDisplayed()));
appCompatButton.perform(click());

// the hierarchy can't find HomeBtn , it still hold the Splash's View, so the code below will fail
onView(withId(R.id.home_btn)).check(ViewAssertions.matches(isDisplayed()));
}

如果在一个TestFile中不允许进行多事件测试,那么如何进行测试多个事件的流程?

最佳答案

我也有麻烦即使我使用了空闲状态,Espresso也不会等待新的事件运行。因此,这迫使我在检查有关新事件的 View 之前设置了延迟

创建界面:

/**
* Interface for expectations of compliance with the conditions.
*/
public interface Condition {
/**
* @return text description for log output when check failed.
*/
String getDescription();

/**
* @return true if the condition is met.
*/
boolean check();
}

并像这样使用它:
/**
* Wait while condition come true or timeout limit.
*
* @param condition condition for exit
* @param timeout limit in seconds
* @throws Exception exception
*/
public static void waitForCondition(Condition condition, int timeout) throws Exception {
final int CONDITION_NOT_MET = 0;
final int CONDITION_MET = 1;
final int TIMEOUT = 2;

final int INTERVAL = 250;

int status = CONDITION_NOT_MET;
int elapsedTime = 0;

do {
if (condition.check()) {
status = CONDITION_MET;
} else {
elapsedTime += INTERVAL;
delay(INTERVAL);
}

if (elapsedTime >= timeout * 1000) {
status = TIMEOUT;
break;
}
} while (status != CONDITION_MET);

if (status == TIMEOUT) {
String msg = condition.getDescription() + " - took more than " + timeout + " seconds. Test stopped.";
log(msg);
throw new Exception(msg);
}
}

例子:
public class MovieScreenVisible implements Condition {
@Override
public String getDescription() {
return "Movie screen should be on the top";
}

@Override
public boolean check() {
Activity activity = TestBase.getCurrentActivity();
if (activity == null || !(activity instanceof MovieActivity)) {
return false;
}

ViewGroup layout = activity.findViewById(R.id.movie_fragment);
return layout != null && layout.getVisibility() == View.VISIBLE;
}
}

// wait maximum 30 seconds until movie screen should be visible
waitForCondition(new MovieScreenVisible(), 30);

关于android-espresso - Espresso 2-如何测试多项事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39009906/

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