android - 您如何使用 espresso 测试操作栏后退按钮?
问题描述
我正在尝试学习如何测试我的 Android 项目,包括我的导航。我已经实现了一个 navHost 片段作为 NavController,并使用它来设置我的操作栏后退按钮以在我的片段中移动。我不知道如何访问这个后退按钮,以便我可以通过 espresso 对其进行测试。
以下是主要活动中 onCreate 函数的相关代码:
val navController = this.findNavController(R.id.myNavHostFragment)
NavigationUI.setupActionBarWithNavController(this, navController)
appBarConfiguration = AppBarConfiguration(navController.graph)
这是一个相关的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()))
}
我对 Android 和测试都是新手,因此将不胜感激。
解决方案
这是我在检查自动浓缩咖啡测试后发现的如果语言环境是英语的工作答案:
val imageButton = onView(
Matchers.allOf(
withContentDescription("Navigate up"),
isDisplayed()
)
)
imageButton.perform(click())
如果应用程序是多语言的,则使用:
val imageButton = onView(
Matchers.allOf(
withContentDescription(R.string.abc_action_bar_up_description),
isDisplayed()
)
)
imageButton.perform(click())
推荐阅读
- php - 如果记录存在则查询数据库,如果不存在则回显答案
- function - 如何在 setState 之后保留先例小部件
- java - 为什么 MockMvc 测试建议注入 WebApplicationContext @BeforeEach 测试?
- c++ - 模板化省略号构造函数 C++
- python - 将DataFrame的每一行重复N次以创建一个新的DataFrame(N由另一列定义)
- python - 如何在条形图中从一列列表中绘制词频
- amazon-web-services - COPY 命令从“iam_role”处或附近的 Amazon S3 语法错误加载
- c++ - 排序函数不将参数传递给比较函数
- c++ - 当我尝试在运行时从 psapi.dll (windows) 链接函数时,GetModuleFileNameExA 将无法正常工作
- cmake - cmake rpath没有加载所有库