我正在从 robotium 切换到 espresso,我正在使用 apk 编写测试,我无法访问代码。 在 robotium 中使用 solo.getView("view-id") 我们可以访问 View ,但我不知道如何在 espresso 中执行此操作? espresso witId() 方法需要我无权访问的 R.id.viewid。
public class AaEspressoTest {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.tri.re.CordActivity";
private static Class<?> launcherActivityClass;
static {
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Rule
public ActivityTestRule<?> mActivityRule = new ActivityTestRule(launcherActivityClass);
@Test public void testHello() throws Exception{
onView(withText("Browse older recordings")).perform(click());
//Id is not accessible shows red
onView(withId(R.id.button)).perform(click());
}
}
最佳答案
您可以使用辅助函数来获取 id:
private static int getId(String id) {
Context targetContext = InstrumentationRegistry.getTargetContext();
String packageName = targetContext.getPackageName();
return targetContext.getResources().getIdentifier(id, "id", packageName);
}
然后你可以在 Espresso 中使用这个 id:
onView(withId(getId("button"))).perform(click());
关于android - Espresso 如何像在 robotium 中那样不使用 R.id.viewid 访问 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29946970/