无法启动意图Intent

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (121)

在这里我的Espresso测试:

import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import java.util.concurrent.TimeUnit

@RunWith(AndroidJUnit4::class)
class TradersActivityTest {
    private lateinit var mockServer: MockWebServer

    @Rule
    @JvmField
    var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java, false, false)

    @Before
    fun setup() {
        mockServer = MockWebServer()
        mockServer.start(8081)
    }

    @Test
fun hasTraders_noTradersTextView_isNotDisplayed() {
    mockServer.enqueue(MockResponse()
            .setResponseCode(200)
            .setBody(FileUtil.getStringFromFile(context, ONE_TRADER_NO_WALLETS_LIST)));
    tradersIntentTestRule.launchActivity(Intent())
    onView(withId(R.id.noTradersTextView))
            .check(matches(not(isDisplayed())))
}

    @Test
    fun toolBar_height() {
        onView(withId(R.id.toolBar))
                .check(matches(withHeightResId(R.dimen.tool_bar_height)))
    }

测试hasTraders_noTradersTextView_isNotDisplayed成功通过。

但测试toolBar_height失败并显示消息:

java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:169)
at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:83)

所以我改变setup方法:

 @Before
    fun setup() {
        mockServer = MockWebServer()
        mockServer.start(8081)

        mockServer.enqueue(MockResponse()
                .setResponseCode(200)
                .setBody(FileUtil.getStringFromFile(context, ONE_TRADER_NO_WALLETS_LIST)));
        tradersIntentTestRule.launchActivity(Intent())

        Debug.d(TAG, "SUCCCESS_START_MOCKWEBSRVER")
    }

现在测试toolBar_height通过,但hasTraders_noTradersTextView_isNotDisplayed失败并显示消息:

java.lang.RuntimeException: Could not launch intent Intent { flg=0x10000000 com.myproject.android.ui.activity.TradersActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1556373134135 and now the last time the queue went idle was: 1556373143180. If these numbers are the same your activity might be hogging the event queue.
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:459)

PS我需要在测试中启动活动,hasTraders_noTradersTextView_isNotDisplayed因为它是一个特定的测试。但是我不想在测试中开始活动toolBar_height

提问于
用户回答回答于

在我的UI自动化测试中,我有这些类:

public class UiTestHelper {

    public static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }

    public static void clickOn(View view) {
        waitUntilVisible(view);
        onView(withId(view.getId())).perform(click());
    }

    public static void waitUntilVisible(View view) {
        ViewVisibilityIdlingResource idlingResource = new ViewVisibilityIdlingResource(view, VISIBLE);
        IdlingRegistry.getInstance().register(idlingResource);
    }

    public static void unregisterAllIdlingResources() {
        IdlingRegistry idlingRegistry = IdlingRegistry.getInstance();
        for (IdlingResource resource: idlingRegistry.getResources()) {
            idlingRegistry.unregister(resource);
        }
    }
}

public class ViewVisibilityIdlingResource implements IdlingResource {

    private final View view;
    private final int expectedVisibility;
    private boolean idle;
    private ResourceCallback resourceCallback;

    public ViewVisibilityIdlingResource(final View view, final int expectedVisibility) {
        this.view = view;
        this.expectedVisibility = expectedVisibility;
        this.idle = false;
        this.resourceCallback = null;
    }

    @Override
    public final String getName() {
        return ViewVisibilityIdlingResource.class.getSimpleName();
    }

    @Override
    public final boolean isIdleNow() {
        idle = idle || (view.getVisibility() == expectedVisibility && view.getWidth() > 0 && view.getHeight() > 0);

        if (idle) {
            if (resourceCallback != null) {
                resourceCallback.onTransitionToIdle();
            }
        }

        return idle;
    }

    @Override
    public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
        this.resourceCallback = resourceCallback;
    }

}

然后,当我想检查一个视图在那里时,调用

UiTestHelper.waitUntilVisible(view);

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励