我在YT Advanced Android Espresso上發現了很棒的器械測試教程。我從那裏進行了代碼調整,以適應我的需求。如何查看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)));
}
不幸的是,它不適合我。測試失敗:
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)
它有什麼問題?我如何以其他方式測試它?
這並不適用於我。這是Grzegorz Bielanski發佈的進口產品。 instanceOf無法爲我解決。 –
只是想知道你爲什麼會投票?它適用於大多數情況(請參閱票數),並且它不適用於您的事實不會使此答案不正確。 – denys
我如何知道它適用於「大多數情況下」。我的經驗是,Android上的各種版本在不同版本之間發生了變化,今天的相關答案不一定明確。 –