我刚开始使用Espresso recorder
。我做了我的第一个测试,从我可以看到的函数onView
,假设等待对象继续不做工作。它总是回归:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching
。
是否有任何可以作为wait for
使用的功能?
package com.mytest;
import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingResource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import com.mytest.R;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static org.hamcrest.Matchers.allOf;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class Test1 {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
private IdlingResource mIdlingResource;
@Before
public void registerIdlingResource() {
mIdlingResource = mActivityRule.getActivity().getIdlingResource();
Espresso.registerIdlingResources(mIdlingResource);
}
@Test
public void test1() {
ViewInteraction recyclerView = onView(
allOf(withId(R.id.recycler_view), isDisplayed()));
recyclerView.perform(actionOnItemAtPosition(0, click()));
ViewInteraction relativeLayout = onView(
allOf(withId(R.id.capture_layout), isDisplayed()));
relativeLayout.perform(click());
ViewInteraction relativeLayout2 = onView(
allOf(withId(R.id.like_layout),
withParent(allOf(withId(R.id.cameraLayout),
withParent(withId(android.R.id.content)))),
isDisplayed()));
relativeLayout2.perform(click());
ViewInteraction relativeLayout3 = onView(
allOf(withId(R.id.exit_layout), isDisplayed()));
relativeLayout3.perform(click());
}
}
答案 0 :(得分:1)
一种方法是使用匹配器循环直到它发生。当我需要等待assync任务完成时,我使用以下帮助器。
public class ViewSynchronizer {
private static final String TAG = "MC_Synchronizer";
public static boolean viewExists(final Matcher<View> viewMatcher, final long millis) throws InterruptedException {
final Boolean[] found = new Boolean[1];
final CountDownLatch latch = new CountDownLatch(1);
ViewAction action = new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "wait for a specific view with id <" + viewMatcher.toString() + "> 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;
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
if (viewMatcher.matches(child)) {
Log.d(TAG, "perform: found match");
found[0] = true;
latch.countDown();
return;
}
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
found[0] = false;
latch.countDown();
}
};
onView(isRoot()).perform(action);
latch.await();
return found[0];
}
}
用法:
Assert.assertTrue(viewExists(allOf(withId(R.id.account_sign_out_btb),withEffectiveVisibility(ViewMatchers.Visibility.GONE)),2000)); //wait 2 seconds
答案 1 :(得分:0)
<强>更新强> 看起来您正在使用RecyclerView,请尝试以下代码。
此外,如果您进行任何网络呼叫,您需要实施RecyclerView / Network IdlingResource以告知Espresso在执行测试步骤之前等待数据填充。
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, true, true);
private MainActivity mMainActivity = null;
private IdlingResource mIdlingResource;
@Before
public void registerIdlingResource() {
mMainActivity = mActivityRule.getActivity();
}
@Test
public void test1() {
mIdlingResource = mActivityRule.getActivity().getIdlingResource();
Espresso.registerIdlingResources(mIdlingResource);
mActivityRule.launchActivity(MainActivity.createIntent(getTargetContext()));
ViewInteraction recyclerView = onView(
allOf(withId(R.id.recycler_view), isDisplayed()));
recyclerView.perform(actionOnItemAtPosition(0, click()));
ViewInteraction relativeLayout = onView(
allOf(withId(R.id.capture_layout), isDisplayed()));
relativeLayout.perform(click());
ViewInteraction relativeLayout2 = onView(
allOf(withId(R.id.like_layout),
withParent(allOf(withId(R.id.cameraLayout),
withParent(withId(android.R.id.content)))),
isDisplayed()));
relativeLayout2.perform(click());
ViewInteraction relativeLayout3 = onView(
allOf(withId(R.id.exit_layout), isDisplayed()));
relativeLayout3.perform(click());
Espresso.unregisterIdlingResources(mIdlingResource);
}
使用ActivityTestRule
,您需要设置initialTouchMode和launchActivity。
使用此
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, true, true);
也不要忘记在@After测试方法中取消注册IdlingResources。