Android Espresso Exception: android.support.test.espresso.No



我正在使用"记录咖啡测试功能"来生成Android Studio中的测试代码2.2.2以下是自动生成的测试类。

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 com.mmi.drivemate.R;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
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.withText;
import static org.hamcrest.Matchers.allOf;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class SplashActivityTest {
    @Rule
    public ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);
    @Test
    public void splashActivityTest() {
    try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.login_button), withText("SIGN IN"), isDisplayed()));
        appCompatButton.perform(click());
        ViewInteraction appCompatEditText = onView(
                withId(R.id.fragment_login_email));
        appCompatEditText.perform(scrollTo(), click());
        ViewInteraction appCompatEditText2 = onView(
                withId(R.id.fragment_login_email));
        appCompatEditText2.perform(scrollTo(), replaceText("safemate2"), closeSoftKeyboard());
        ViewInteraction passwordView = onView(
                withId(R.id.fragment_login_password));
        passwordView.perform(scrollTo(), replaceText("safemate2"), closeSoftKeyboard());
        ViewInteraction appCompatButton2 = onView(
                allOf(withId(R.id.fragment_login_loginBtn), withText("Login")));
        appCompatButton2.perform(scrollTo(), click());
    }
}

下面是我的应用程序gradle:

defaultConfig {
    ..........
    ..........
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
dependencies {
.............
.............
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'design'
        exclude group: 'com.android.support', module: 'recyclerview-v7'
    }
}
apply plugin: 'com.google.gms.google-services'

我尝试在测试用例Java文件中使用thread.sleep(1000),该控件在前几个检查中运行正常,但是当随后执行视图操作时,它再次崩溃。有什么方法可以解决我的情况下的nomatchingViewException?

我正在使用以下过程在主线程上等待:

public static void waitAsync(long milliseconds) {
    try {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                return null;
            }
        }.get(milliseconds, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }
}

它不会冻结主线程,因此可以处理事件,但是代码正在等待超时。

用法非常简单:

waitAsync(1000); // sleep to one second

您可以尝试实现自己的Espresso Idling Resource

最终结果与以下类似:

import android.support.test.espresso.IdlingResource;
/**
 * Have functions to sleep the processor because assertions are not linked to
 * {@link IdlingResource} to do assertions, so should be used before asserts if there's an
 * idle process.
 */
public class IdlingResourceSleeper {
    private static final int SLEEPS_LIMIT = 50;
    private static final int SLEEPS_TIME = 10;
    /**
     * Used to sleep {@link IdlingResourceSleeper#SLEEPS_LIMIT} times and
     * {@link IdlingResourceSleeper#SLEEPS_TIME} ms until idlingResource.isIdleNow() is false.
     *
     * @param idlingResource
     */
    public static void sleep(IdlingResource idlingResource) {
        int sleeps = 0;
        while (!idlingResource.isIdleNow() || sleeps < SLEEPS_LIMIT) {
            android.os.SystemClock.sleep(SLEEPS_TIME);
            sleeps++;
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新