使用浓缩咖啡,我如何根据显示的视图决定使用哪个视图操作?

Using espresso, how can I decide which view action to use according to displayed view?

我有一个一次显示一个组件的窗体,它可能是:EditText、Spinner、ListView,或者根据某些规则的其他东西。

并且每个组件都有不同的测试代码。

使用 Espresso,如何检查显示的组件并执行其操作?

伪代码

if( ask to enter the number ){
    onView(withId(R.id.et_number)).perform(replaceText("12345"));
}else if(ask to select my country){
     onView(withRecyclerView(R.id.rv_country).atPosition(0)).perform(click());
}

实施此案例的最佳方法是什么?如果我必须创建我的自定义匹配器,这种情况的实施情况如何?

如果有人能给我一个简单的例子,我将不胜感激。

我以前做过类似的事情。我所做的是检查元素是否显示在 try 中并捕获异常。 试试这个:

try { // supposing et_number is displayed
  onView(withId(R.id.et_number)).check.matches(isCompletelyDisplayed())); // will throw an exception if its not displayed
    onView(withId(R.id.et_number)).perform(replaceText("12345"));
} catch (Exception e) { // et_number is not displayed
    onView(withRecyclerView(R.id.rv_country).atPosition(0)).perform(click());
}