Testing 如何使浓咖啡测试等待页面加载
我将登录。之后,请求显示卡片列表。每次这个请求发生在不同的时间,使用睡眠不是很好。如何使测试在页面加载时一直等待?最好与样本代码Testing 如何使浓咖啡测试等待页面加载,testing,automated-tests,android-espresso,Testing,Automated Tests,Android Espresso,我将登录。之后,请求显示卡片列表。每次这个请求发生在不同的时间,使用睡眠不是很好。如何使测试在页面加载时一直等待?最好与样本代码 @RunWith(AndroidJUnit4.class) @LargeTest public class AuthorizationTest { private IdlingResource mIdlingResource; @Rule public ActivityTestRule<AuthorizationActivity> mActivityR
@RunWith(AndroidJUnit4.class)
@LargeTest
public class AuthorizationTest {
private IdlingResource mIdlingResource;
@Rule
public ActivityTestRule<AuthorizationActivity> mActivityRule = new ActivityTestRule(AuthorizationActivity.class);
@Before
public void SetUp() throws Exception {
Thread.sleep(4000);
try {
onView(allOf(withId(R.id.tiAuthLogin), (isDisplayed()))).check(matches(isDisplayed()));
} catch (NoMatchingViewException e) {
pressBack();
}
}
@Test
public void checkMoneyBackButton() throws Exception {
onView(withId(R.id.btnAuthLogin)).perform(click());
Thread.sleep(10000);
onView(withId(R.id.etSessionKey1)).perform(typeText("1234"));
closeSoftKeyboard();
onView(isRoot()).perform(waitFor(40000));
ViewInteraction viewInteraction = Espresso.onView(withText("**** 0431"));
viewInteraction.perform(click());
Thread.sleep(3000);
Espresso.onView(ViewMatchers.withId(R.id.vpCard)).perform(ViewActions.swipeUp());
onView(withId(R.id.statementMoneyBack)).check(matches(isDisplayed()));
}
}您可以创建自定义操作以等待 请检查此链接:
我不知道为什么,但这对我不起作用
@RunWith(AndroidJUnit4.class)
@LargeTest
public class AuthorizationTest {
@Rule
public ActivityTestRule<AuthorizationActivity> mActivityRule = new ActivityTestRule(AuthorizationActivity.class);
@Before
public void SetUp() throws Exception {
Thread.sleep(4000);
try {
onView(allOf(withId(R.id.tiAuthLogin), (isDisplayed()))).check(matches(isDisplayed()));
} catch (NoMatchingViewException e) {
pressBack();
}
}
public static ViewAction waitId(final int viewId, final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
final Matcher<View> viewMatcher = withId(viewId);
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
// found view with required ID
if (viewMatcher.matches(child)) {
return;
}
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
// timeout happens
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
};
}
@Test
public void checkMoneyBackButton() throws Exception {
onView(withId(R.id.btnAuthLogin)).perform(click());
Thread.sleep(10000);
onView(withId(R.id.etSessionKey1)).perform(typeText("1234"));
closeSoftKeyboard();
onView(isRoot()).perform(waitId(R.id.swipeRefreshLayout, 45000));
ViewInteraction viewInteraction = Espresso.onView(withText("**** 0431"));
viewInteraction.perform(click());
Thread.sleep(3000);
onView(withId(R.id.btnTransferToCard)).check(matches(isDisplayed()));
}
}我认为您应该将登录和查看卡列表之间的测试场景分开。 登录的第一个场景:
public class AuthorizationTest {
@Rule
public ActivityTestRule<AuthorizationActivity> mActivityRule = new ActivityTestRule(AuthorizationActivity.class);
private Activity activity;
@Before()
public void setup() {
activityTestRule.launchActivity(new Intent());
activity = activityTestRule.getActivity();
}
@After()
public void tearDown() {
activityTestRule.finishActivity();
}
@Test
public void login() throws Exception {
onView(withId(R.id.btnAuthLogin)).perform(click());
onView(withId(R.id.etSessionKey1)).perform(typeText("1234"));
closeSoftKeyboard();
onView(isRoot()).perform(waitId(R.id.swipeRefreshLayout, 45000));
ViewInteraction viewInteraction = Espresso.onView(withText("**** 0431"));
viewInteraction.perform(click());
//asssert login success
onView(withId(R.id.toastloginsuccess)).check(matches(isDisplayed()));
}
第二个场景直接进入卡片列表活动
public class CardListTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
private Activity activity;
@Before()
public void setup() {
activityTestRule.launchActivity(new Intent());
activity = activityTestRule.getActivity();
}
@After()
public void tearDown() {
activityTestRule.finishActivity();
}
@Test
public void checkMoneyBackButton() throws Exception {
//assertMainActivity
onView(withId(R.id.menuTitle)).check(matches(isDisplayed()));
//Assert what you want
onView(withId(R.id.btnTransferToCard)).check(matches(isDisplayed()));
// Do what you want
}
希望能有所帮助。你应该看看这个:我知道有这样的事情。但我不知道如何将此应用于我的代码。密码