第一次测试后,Espresso ActivityTestRule不会重启活动

时间:2017-04-20 08:34:38

标签: android android-espresso

我无法运行Espresso测试。在我的第一次测试运行后,它将失败启动下一个意图并达到超时。

java.lang.RuntimeException: Could not launch intent Intent { 
act=android.intent.action.MAIN flg=0x10000000 
cmp=se.liu.ida.sambandssystem/.Authentication } 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 
1492675509022 and now the last time the queue went idle was: 
1492675551675. If these numbers are the same your activity might be 
hogging the event queue...

我尝试过不同类型的TestRules,即使是使用显式finishActivity()relaunchActivity()方法的自定义TestRules,但它没有用。该应用程序正在进行网络呼叫,我试图在第一次测试后断开它,但这也没有用。任何人都知道如何在每次发布后强行退出应用程序?

以下是测试中的一些示例代码:

@RunWith(AndroidJUnit4.class)
public class AuthenticationTest {

    private String mNFC, mPassword;
    private int nrOfruns;
    private IdlingResource mIdlingResource;
    private AuthenticationUtil util;

    @Rule
    public IntentsTestRule<Authentication> mActivityTestRule =
            new IntentsTestRule<Authentication>(
                    Authentication.class,
                    false, //Inital touchmode
                    true  //LaunchActivity
            );

    @Before
    public void registerIdlingResource() {
        mIdlingResource = mActivityTestRule.getActivity().getIdlingResource();       
        Espresso.registerIdlingResources(mIdlingResource);
    }

    @After
    public void unregisterIdlingResource() throws Service.NotInitializedException {
        if (mIdlingResource != null) {
            Espresso.unregisterIdlingResources(mIdlingResource);
        }
        Log.d("TEST", "Unregistering Espresso Idling resource");
    }

    @Before
    public void initValidLogin() throws InterruptedException, Service.NotInitializedException {
        nrOfruns = 10;
        validNFC = "11111111";
        validPassword = "password";
        util = new AuthenticationUtil(); //Espresso code for clicking login buttons like a user would
        util.logOutIfLocked();
    }


    @Test
    public void validAuthenticationTest() throws Exception {
        Log.d("TEST", "Valid authentication test...");
        util.authenticate(validNFC, validPassword);

        //Log out after successful log in
        onView(withId(R.id.drawer_layout)).perform(actionOpenDrawer());
        onView(withId(R.id.nav_view))
                .perform(NavigationViewActions.navigateTo(R.id.nav_logout));
        onView(withText("Log out")).perform(click());

        Log.d("TEST", "Valid authentication test complete");

    }

    @Test
    public void invalidLogInTest() throws InterruptedException {
        Log.d("TEST","Invalid authentication test...");
        HashMap<String, String> invalidLogins = createInvalidLogins(nrOfruns);

        for (HashMap.Entry<String, String> invalidLogin : invalidLogins.entrySet()){
            util.authenticate(invalidLogin.getKey(), invalidLogin.getValue());
        }
    } 

如果单独启动测试,但我希望测试可以放在同一个类但不同的方法中。

编辑:问题是我们的应用程序创建了许多排队的Toast消息,禁止下一个启动意图。在移除所有吐司后,测试按预期工作。

0 个答案:

没有答案