2016-03-31 31 views
5

Tôi đã tìm thấy hướng dẫn thử nghiệm công cụ tuyệt vời trên YT Advanced Android Espresso. Tôi lấy mã từ đó với sự điều chỉnh nhỏ theo nhu cầu của tôi.Cách kiểm tra tiêu đề Thanh công cụ trong thử nghiệm nhạc cụ Android?

import static android.support.test.InstrumentationRegistry.getInstrumentation; 
import static android.support.test.espresso.Espresso.onView; 
import static android.support.test.espresso.action.ViewActions.click; 
import static android.support.test.espresso.assertion.ViewAssertions.matches; 
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 
import static android.support.test.espresso.matcher.ViewMatchers.withChild; 
import static android.support.test.espresso.matcher.ViewMatchers.withId; 
import static android.support.test.espresso.matcher.ViewMatchers.withParent; 
import static android.support.test.espresso.matcher.ViewMatchers.withText; 
import static org.hamcrest.core.AllOf.allOf; 

... 

@Test 
public void checkToolbarTitle() { 
    String toolbarTitile = getInstrumentation().getTargetContext().getString(R.string.my_bus_stops); 
    onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))).check(matches(withText(toolbarTitile))); 
} 

Unfortunatelly nó không hiệu quả với tôi. Thử nghiệm không thành công với:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (is assignable from class: class android.widget.TextView and has parent matching: is assignable from class: class android.widget.Toolbar) 

Điều gì xảy ra với nó? Làm thế nào tôi có thể kiểm tra nó theo cách khác?

Trả lời

3

SOLUTION

Phương pháp này là tốt. Như Chiu-Ki Chan đã viết trong hướng dẫn của cô, bạn có thể "xác định rằng một cái nhìn cụ thể". NHƯNG bạn phải chắc chắn rằng bạn nhập khẩu Thanh công cụ thích hợp:

import android.support.v7.widget.Toolbar; 

thay vì:

import android.widget.Toolbar; 
15

này làm việc cho tôi:

onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar)))) 
    .check(matches(withText("toolbarTitile"))); 
+2

Điều này không hiệu quả đối với tôi. Nó đã được nhập khẩu như Grzegorz Bielanski đăng. instanceOf không thể được giải quyết cho tôi. –

+0

Chỉ cần tự hỏi tại sao bạn lại bỏ phiếu? Nó hoạt động cho hầu hết các trường hợp (xem số phiếu bầu) và thực tế là nó không hoạt động cho bạn không làm cho câu trả lời này không chính xác. – denys

+0

Làm thế nào để tôi biết nó hoạt động cho "hầu hết các trường hợp". Trải nghiệm của tôi là những gì hoạt động trên các thay đổi của Android từ phiên bản này sang phiên bản khác và một câu trả lời có liên quan hôm nay không chắc chắn sẽ có liên quan vào ngày mai. –

1

Tôi không nhớ nếu tôi đã tự mình viết, hoặc nếu tôi tìm thấy nó ở đâu đó, nhưng đây là cách tôi kiểm tra tiêu đề thanh công cụ:

public static Matcher<View> withToolbarTitle(CharSequence title) { 
    return withToolbarTitle(is(title)); 
} 

public static Matcher<View> withToolbarTitle(final Matcher<CharSequence> textMatcher) { 
    return new BoundedMatcher<View, Toolbar>(Toolbar.class) { 
     @Override 
     public boolean matchesSafely(Toolbar toolbar) { 
      return textMatcher.matches(toolbar.getTitle()); 
     } 

     @Override 
     public void describeTo(Description description) { 
      description.appendText("with toolbar title: "); 
      textMatcher.describeTo(description); 
     } 
    }; 
} 

Tính năng này hoạt động với mọi trường hợp. Xác nhận ví dụ: onView(withId(R.id.toolbar)).check(matches(withToolbarTitle("title")));

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