我正在使用「記錄espresso測試功能」在android studio 2.2.2中生成測試代碼 以下是自動生成的測試類。Android Espresso異常:android.support.test.espresso.NoMatchingViewException
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?
感謝您的回答阿爾喬姆,我會試試你的方法,並讓你知道它是否有效。 –