2016-02-26 113 views
1

我想要測試基於濃咖啡的快樂路徑。我的流程是這樣的: SplashActivity -> Activity_1 -> Activity_2 -> Activity_3 -> Activity_4. Activity_1中有一個按鈕,如果用戶還沒有以其他方式登錄Activity_3,則會將用戶導向Activity_2濃縮咖啡,測試登錄屏幕的快樂路徑

我的測試通過,如果應用程序在這個方向去SplashActivity -> Activity_1 -> Activity_3 ->...。然而,我得到例外,android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching:當用戶還沒有登錄,因此應用程序以這種方式SplashActivity -> Activity_1 -> Activity_2 ->...

這很明顯,因爲我的測試期望Activity_3,而Activity_2是可見的。

這是我的測試:

@RunWith(AndroidJUnit4.class) 
public class MinHappyPathTest 
{ 
    @Rule 
    public ActivityTestRule<Activity_1> mActivityTestRule = new ActivityTestRule<>(Activity_1.class); 

    private Activity_1 mActivity_1; 

    @Before 
    public void setup() 
    { 
     mActivity_1 = mActivityTestRule.getActivity(); 
    } 

    @Test 
    public void HappyPathMinimumTest() throws InterruptedException 
    {  
     // Wait to everything settles down (few animations there) 
     Thread.sleep(2000); 

     // On mActivity_1 press the button 
     onView(withId(R.id.btnNext)).perform(click()); 

     // On mActivity_3  onView(withId(R.id.editText)).perform(typeText(destination_short_name), ViewActions.closeSoftKeyboard()); 
     Thread.sleep(1000); // to results displays 
     onView(allOf(withId(R.id.recycler_view), isDisplayed())) 
       .perform(RecyclerViewActions.actionOnItemAtPosition(2, click())); 

     // Other tests... 
    } 
} 

兩個問題,我有:

  1. 如何把如果基於活動的聲明Activity_1後是可見的?
  2. 根據我發現的與單元測試不同的是,您可以選擇課程並對其進行測試,因此在Espresso中不可能做同樣的事情。例如,我直接運行mActivity_4並進行測試,因爲默認情況下應用啓動mActivity_1顯示,並且我得到NoMatchingViewException。我對嗎?我實際上測試過,看起來像那樣。

回答

0

我沒有找到一個方式,我可以檢查正在顯示什麼活動,所以我決定用if聲明我正在使用的活動是這樣的方式:

@Test 
public void HappyPathMinimumTest() throws InterruptedException 
{  
    // Wait to everything settles down (few animations there) 
    Thread.sleep(2000); 

    // On Activity_1 press the button 
    onView(withId(R.id.btnNext)).perform(click()); 

    // Display login screen if required (Activity_2) 
    if (myCondition) 
    { 
     // tests of Login activity... 
    } 

    // On mActivity_3  onView(withId(R.id.editText)).perform(typeText(destination_short_name), ViewActions.closeSoftKeyboard()); 
    Thread.sleep(1000); // to results displays 
    onView(allOf(withId(R.id.recycler_view), isDisplayed())) 
      .perform(RecyclerViewActions.actionOnItemAtPosition(2, click())); 

    // Other tests... 
} 
1

我想您正在接收NoMatchingViewException,因爲您正在使用Thread.sleep。它不適用於Espresso。您應該使用IdlingResources在可以繼續時通知Espresso。請參閱此處的IdlingResource實施示例 - http://droidtestlab.com/delay.html

+0

感謝您的信息。我之前讀過關於islingResource的鏈接,感謝鏈接,但是'Thread.sleep()'對我很好。我有一個短信確認屏幕,等待30S通過短信接收代碼。即使在這個屏幕上我的Thread.sleep(31000);效果很好。我在我的快樂道路上廣泛使用了這種等待方法,並沒有看到問題。再次感謝。 – Hesam