android - 如何通过类名访问具有 R.id 的父元素中的元素
问题描述
我正在编写一个用于登录 Android 应用程序的自动化测试。我正在使用 Record Espresso Test 记录测试,然后编辑代码,因为它通常充满错误。我正在使用浓缩咖啡
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2',
并uiAutomatorViewer
仔细检查R.id's~
和class
名称。
我在尝试编辑元素中没有R.id
但使用类名的文本时遇到了问题android.widget.EditText
:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: com.mydosesmart:id/til_name and an instance of android.widget.FrameLayout and an instance of android.widget.EditText)
问题是具有类名的元素android.widget.EditText
没有R.id.
. 这个类名对于这个视图不是唯一的,这个具有类名android.widget.EditText
的元素有一个具有唯一性的父元素R.id.
。
在应用程序的登录视图中,两个元素具有类名,android.widget.EditText
因此我不能仅通过类名来调用此元素。我想这样称呼它:在元素中R.id.til_name
找到具有类名的元素android.widget.EditText
。下面是我现在使用的代码,但它失败了。
ViewInteraction textInputEditText2 = onView(
allOf(withId(R.id.til_name), instanceOf(Class.forName("android.widget.FrameLayout")), instanceOf(Class.forName("android.widget.EditText"))));
textInputEditText2.perform(replaceText("testespresso"), closeSoftKeyboard());
这也失败了:
ViewInteraction textInputEditText2 = onView(
allOf(withId(R.id.til_name), instanceOf(Class.forName("android.widget.EditText"))));
textInputEditText2.perform(replaceText("testespresso"), closeSoftKeyboard());
由于我正在测试的应用程序中的许多元素没有指定的 R.id,我想找到一种简单的方法来调用它们以进行测试。
解决方案
我认为你应该试试这个(找到你的 EditText 没有 ID 但有唯一已知 id 的父级):
allOf(withParent(withId(R.id...)), withClassName(containsString(EditText.class.getName())))
根据新信息更新:与间接父级匹配(R.id.... 是放置间接父级 ID 的位置):
allOf(isDescendantOfA(withId(R.id...)), withClassName(containsString(EditText.class.getName())))
推荐阅读
- flutter - SnackBar 未在颤动中显示
- c - 为什么变量'k'不递增?(C)
- roles - 选择角色的不同方式
- c# - 当页面视频全屏时,将 Webview2 切换到全屏
- r - 如何正确使用 GET 和 httr 结构相同的两个 cookie?
- javascript - Javascript 中的过滤器方法不起作用。但其他过滤方法正在工作
- python - 如何在从串口接收的文本浏览器 pyqt5 上附加串行数据 python pyqt
- javascript - 有没有办法根据对象内部已经存在的两个值在对象内部创建新的键值对?- JavaScript
- php - 无法在 Dockerized 应用程序中使用 DBGp 从远程服务器将 Xdebug 重定向到 localhost
- google-analytics - 如何使用 gtag 脚本手动设置 Google Analytics(分析)引荐来源网址?