I have onView(withId(R.id.check_box)).perform(click())
, but i only want to do this if the check box is not already checked. How can I do this in espresso?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You have something like this in your code:
Try
This seems to be part of your test, that the checkbox needs to be checked.
You can check this by:
If this fails, your test will fail, which may be good in your case. Then, you can perform the click you want after this case matches.
If this is not the situation you want, can you explain more on what you are trying to do in this Espresso Test?
I also wanted to toggle a checkbox/switch depending on it's previous state. At first, I tried this to toggle ON a checkbox that was OFF:
...and this to toggle OFF a checkbox that was ON:
However, this doesn't work, since Espresso will be looking for a specific toggle state before it performs the action. Sometimes, you don't know whether it's ON or OFF beforehand.
My solution is to use a custom ViewAction to turn OFF/ON any checkable object (Switch, Checkbox, etc.) that isn't dependent on previous state. So, if it's already ON, it'll stay ON. If it's OFF, it'll toggle ON. Here's the ViewAction:
And here's how you use it (in this example, when you want to toggle to ON):
I had the same problem an wrote an own ViewAction that always unchecks my SwitchCompat
and used it like this:
now i can be sure that the found switch is always unchecked, no matter if it was checked or unchecked before.