0

私はアンドロイドスタジオ2.2.2のテストコードを生成するために "レコードエスプレッソテスト機能"を使用しています 以下は自動生成テストクラスです。以下はAndroidのエスプレッソ例外: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を解決する方法はありますか?

答えて

1

私は、メインスレッド上で待機するように、次の手順を使用しています:

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 
+0

おかげで、私は意志あなたのアプローチを試し、それがうまくいくかどうかを教えてください。 –

0

あなたがあなた自身の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++; 
     } 

    } 
}