我试图通过以下方式点击一些Espresso测试中的主页图标:
onView(withId(android.R.id.home)).perform(click());
Run Code Online (Sandbox Code Playgroud)
这适用于Android> 3.0 - 但旧版本失败,appcompat因为这个元素似乎没有使用此ID.做我想做的事情的好方法是什么?
我正在尝试学习如何测试我的 Android 项目,包括我的导航。我已经实现了一个 navHost 片段作为 NavController,并使用它来设置我的操作栏后退按钮以在我的片段中移动。我不确定如何访问此后退按钮,以便我可以通过 espresso 对其进行测试。
以下是来自主要活动的 onCreate 函数的相关代码:
val navController = this.findNavController(R.id.myNavHostFragment)
NavigationUI.setupActionBarWithNavController(this, navController)
appBarConfiguration = AppBarConfiguration(navController.graph)
Run Code Online (Sandbox Code Playgroud)
这是一个相关的 stackoverflow 页面,但他们询问主页按钮,而我正在尝试使用后退按钮:如何使用 Espresso 测试操作栏上的主页按钮?
到目前为止,这是我的测试(我的 android 应用程序用于订购三明治):
@Test
fun testNavigation() {
//check starting screen
onView(withId(R.id.welcomeConstraint)).check(matches(isDisplayed()))
//push button
onView(withText(R.string.order_now)).perform(click())
//check next screen
onView(withId(R.id.order_constraint)).check(matches(isDisplayed()))
//TO-DO: check action bar back button
//click bread
onView(withText(R.string.wheat)).perform(click())
//click sandwich
onView(withText(R.string.panini)).perform(click())
//click submit
onView(withText(R.string.submit)).perform(click())
//this is an issue: need to click submit twice
onView(withText(R.string.submit)).perform(click())
//check next screen
onView(withId(R.id.recieptConstraint)).check(matches(isDisplayed()))
}
Run Code Online (Sandbox Code Playgroud)
我是 Android 和测试的新手,所以任何帮助将不胜感激。
android android-fragments kotlin android-studio android-espresso