2014-09-23 31 views
29

以下是我的Espresso測試用例之一。特濃咖啡 - 如何在執行特定操作後檢查活動是否啓動?

public void testLoginAttempt() { 
     Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("[email protected]")); 
     Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword")); 

     Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click()); 
     // AFTER CLICKING THE BUTTON, A NEW ACTIVITY WILL POP UP. 
     // Clicking launches a new activity that shows the text entered above. You don't need to do 
     // anything special to handle the activity transitions. Espresso takes care of waiting for the 
     // new activity to be resumed and its view hierarchy to be laid out. 
     Espresso.onView(ViewMatchers.withId(R.id.action_logout)) 
       .check(ViewAssertions.matches(not(ViewMatchers.isDisplayed()))); 

    } 

目前我所做的是檢查新活動(R.id.action_logout)中的視圖是否可見或不可見。如果可見,我會假定活動已成功打開。 但它似乎沒有按照我的預期工作。 有沒有更好的方法來檢查新活動是否成功啓動,而不是檢查該活動中的視圖是否可見? 謝謝

+0

你爲什麼不導入ViewMatchers之前叫什麼名字?'import static android.support.test.espresso.matcher.ViewMatchers。*' – Roel 2015-02-02 16:11:20

+0

@ user2062024你可以發佈工作代碼嗎? – 2016-02-16 09:45:11

+0

最新的Espresso將自動等待Asyntask。 – WenChao 2016-05-29 09:46:05

回答

6

問題是,您的應用程序在單擊登錄按鈕後執行網絡操作。 Espresso不處理(等待)網絡呼叫以默認完成。您必須實現您的自定義IdlingResource,它將阻止Espresso繼續進行測試,直到IdlingResource返回到空閒狀態,這意味着網絡請求已完成。看看咖啡樣頁 - https://google.github.io/android-testing-support-library/samples/index.html

+0

更新了過時的鏈接 – denys 2017-06-22 21:06:45

7

嘗試

intended(hasComponent(new ComponentName(getTargetContext(), ExpectedActivity.class))); 

response from @riwnodennyk

+1

爲什麼'new ComponentName'是必需的? – 2017-06-22 16:41:33

35

您可以使用:

intended(hasComponent(YourExpectedActivity.class.getName())); 

需要此gradle這個條目:

androidTestCompile ("com.android.support.test.espresso:espresso-intents:$espressoVersion") 

intended()hasComponent()

import static android.support.test.espresso.intent.Intents.intended; 
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; 
-3
@RunWith(RobolectricTestRunner.class) 
public class WelcomeActivityTest { 

    @Test 
    public void clickingLogin_shouldStartLoginActivity() { 
     WelcomeActivity activity = Robolectric.setupActivity(WelcomeActivity.class); 
     activity.findViewById(R.id.login).performClick(); 

     Intent expectedIntent = new Intent(activity, LoginActivity.class); 
     assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent); 
    } 
} 
+1

問題是關於濃咖啡,而不是Robolectric。 – Allison 2017-06-20 23:27:08

3

你可以按如下做進口:如果Intents.init()

@Test 
public void testLoginAttempt() { 
    Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("[email protected]")); 
    Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword")); 

    Intents.init(); 
    Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click()); 
    Intents.release(); 
} 

java.lang.NullPointerException被拋出不叫。

0

確保Expresso意圖庫在gradle這個依賴

androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.1" 

然後在您的測試文件中導入這兩個

import android.support.test.espresso.intent.Intents.intended 
import android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent 

然後在您的測試類添加IntentsTestRule

@Rule 
@JvmField 
val mainActivityRule = IntentsTestRule(MainActivity::class.java) 

最後檢查活動已啓動意向

@Test 
fun launchActivityTest() { 
    onView(ViewMatchers.withId(R.id.nav_wonderful_activity)) 
      .perform(click()) 

    intended(hasComponent(WonderfulActivity::class.java!!.getName())) 
} 
3

嘗試:

intended(hasComponent(YourActivity.class.getName()));

而且,記住

java.lang.NullPointerException是,如果Intents.init()拋出不intended()