2017-01-18 6 views
0

私はちょうどEspresso recorderと仕事を始めました。私は最初のテストを行いましたが、私が見ることのできるものから、機能を続行するのを待つことを想定しています。onView常に返します:エスプレッソonViewは要素を待っていません

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching

wait forとして機能する機能はありますか?

package com.mytest; 


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 android.support.test.espresso.Espresso; 
import android.support.test.espresso.IdlingResource; 



import org.junit.Rule; 
import org.junit.Test; 
import org.junit.Before; 
import org.junit.runner.RunWith; 

import com.mytest.R; 



import static android.support.test.espresso.Espresso.onView; 
import static android.support.test.espresso.action.ViewActions.click; 
import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition; 
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.withParent; 
import static org.hamcrest.Matchers.allOf; 

@LargeTest 
@RunWith(AndroidJUnit4.class) 
public class Test1 { 

    @Rule 
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class); 

    private IdlingResource mIdlingResource; 

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

    @Test 
    public void test1() { 
     ViewInteraction recyclerView = onView(
       allOf(withId(R.id.recycler_view), isDisplayed())); 
     recyclerView.perform(actionOnItemAtPosition(0, click())); 

     ViewInteraction relativeLayout = onView(
       allOf(withId(R.id.capture_layout), isDisplayed())); 
     relativeLayout.perform(click()); 

     ViewInteraction relativeLayout2 = onView(
       allOf(withId(R.id.like_layout), 
         withParent(allOf(withId(R.id.cameraLayout), 
           withParent(withId(android.R.id.content)))), 
         isDisplayed())); 
     relativeLayout2.perform(click()); 

     ViewInteraction relativeLayout3 = onView(
       allOf(withId(R.id.exit_layout), isDisplayed())); 
     relativeLayout3.perform(click()); 

    } 
} 
+0

アイドリングリソースを使用すると、UIスレッドがビューを更新中に忙しくなっている可能性があります。ここに例があります。 https://github.com/googlesamples/android-testing/blob/master/ui/espresso/IdlingResourceSample/app/src/androidTest/java/com/example/android/testing/espresso/IdlingResourceSample/ChangeTextBehaviorTest.java –

+0

@vk .4884 私が理解しているように、私はテストを開始するイベントをキャッチすると思います。私は '@ Before'でそれをすることができます。そして、 '@Rule'に '@Before'を使用しています。私はそれがIdlingResourceであるので、MainActivityでそのイベントを捕まえることができます。私はチェックし、私のアプリでは、テストの最初の機能は 'onResume'が呼び出されたときに開始する必要があります。問題は私が正確にこれを登録する方法を知っていませんです。 'mIdlingResource = mActivityRule.getActivity()。onResume(); Espresso.registerIdlingResources(mIdlingResource); ' 私はこれを試しましたが、それは動作しません。互換性のないタイプのエラーが発生します。 – Megami

+0

'onResume() 'の代わりに' mIdlingResource = mActivityRule.getActivity()。getIdlingResource(); 'を使用してください。 –

答えて

0

更新: あなたがRecyclerViewを使用しているように、コードの下に試してみてくださいに見えます。

また、ネットワークコールを行っている場合、テストステップを実行する前にデータが入力されるのを待つように、RecyclerView/Network IdlingResourceを実装する必要があります。あなたはinitialTouchModeとlaunchActivityを設定する必要がActivityTestRuleを使用し

@Rule 
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, true, true); 

    private MainActivity mMainActivity = null; 
    private IdlingResource mIdlingResource; 

    @Before 
    public void registerIdlingResource() { 
     mMainActivity = mActivityRule.getActivity(); 
    } 

    @Test 
    public void test1() { 

     mIdlingResource = mActivityRule.getActivity().getIdlingResource(); 
     Espresso.registerIdlingResources(mIdlingResource); 

     mActivityRule.launchActivity(MainActivity.createIntent(getTargetContext())); 

     ViewInteraction recyclerView = onView(
       allOf(withId(R.id.recycler_view), isDisplayed())); 
     recyclerView.perform(actionOnItemAtPosition(0, click())); 

     ViewInteraction relativeLayout = onView(
       allOf(withId(R.id.capture_layout), isDisplayed())); 
     relativeLayout.perform(click()); 

     ViewInteraction relativeLayout2 = onView(
       allOf(withId(R.id.like_layout), 
         withParent(allOf(withId(R.id.cameraLayout), 
           withParent(withId(android.R.id.content)))), 
         isDisplayed())); 
     relativeLayout2.perform(click()); 

     ViewInteraction relativeLayout3 = onView(
       allOf(withId(R.id.exit_layout), isDisplayed())); 
     relativeLayout3.perform(click()); 

     Espresso.unregisterIdlingResources(mIdlingResource); 

    } 

この

@Rule 
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, true, true); 

も@After試験方法でIdlingResourcesの登録を解除することを忘れないでください使用してください。

関連する問題