2014-09-23 13 views
29

sau đây là một trong các trường hợp thử nghiệm Espresso của tôi.Espresso - Làm cách nào để kiểm tra xem một hoạt động có được khởi chạy sau khi thực hiện một hành động nhất định không?

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()))); 

    } 

Hiện tại, tôi đã kiểm tra xem chế độ xem trong hoạt động mới (R.id.action_logout) có thể hiển thị hay không. Nếu có thể nhìn thấy, tôi sẽ giả định tha hoạt động đã mở thành công. Nhưng nó dường như không hoạt động như tôi mong đợi. Có cách nào tốt hơn để kiểm tra xem một hoạt động mới có được khởi chạy thành công thay vì kiểm tra chế độ xem trong hoạt động đó có hiển thị không? Cảm ơn

+0

Tại sao bạn không nhập ViewMatchers?'nhập static android.support.test.espresso.matcher.ViewMatchers. *' – Roel

+0

@ user2062024 Bạn có thể đăng mã hoạt động không? –

+0

Espresso mới nhất sẽ tự động chờ Asyntask. – WenChao

Trả lời

6

Sự cố là ứng dụng của bạn thực hiện thao tác mạng sau khi bạn nhấp vào nút đăng nhập. Espresso không xử lý (chờ) cuộc gọi mạng kết thúc theo mặc định. Bạn phải triển khai IdlingResource tùy chỉnh sẽ chặn Espresso tiếp tục thử nghiệm cho đến khi IdlingResource trở lại trạng thái Chờ, điều đó có nghĩa là yêu cầu mạng đã kết thúc. Hãy xem trang mẫu Espresso - https://google.github.io/android-testing-support-library/samples/index.html

+0

Cập nhật liên kết đã lỗi thời – denys

7

Hãy thử với

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

Nhìn vào response from @riwnodennyk

+1

Tại sao 'ComponentName' mới cần thiết? –

35

Bạn có thể sử dụng:

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

Yêu cầu nhập gradle này:

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

Việc nhập khẩu cho 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

Câu hỏi đặt ra là về Espresso chứ không phải Robolectric. – Allison

3

Bạn có thể làm điều đó như sau:

@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 được ném nếu Intents.init() không được gọi.

0

Hãy chắc chắn rằng thư viện ý định Expresso là trong các phụ thuộc gradle

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

Sau đó, nhập khẩu hai trong tập tin thử nghiệm của bạn

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

Sau đó thêm IntentsTestRule trong lớp thử nghiệm của bạn

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

Cuối cùng kiểm tra hoạt động đã đưa ra ý định

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

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

Hãy thử:

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

Ngoài ra, hãy nhớ

java.lang.NullPointerException được ném nếu Intents.init() không được gọi trước khi intended()

Các vấn đề liên quan