Espresso 如何在不使用 R.id.viewid 的情况下访问视图,就像我们在 robotium 中所做的那样?
Espresso How to access views without using R.id.viewid as we do in robotium?
我正在从 robotium 切换到 espresso,我正在使用 apk 编写测试,我无法访问代码。 在使用 solo.getView("view-id") 的 robotium 中,我们可以访问该视图,但我不知道如何在 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());
}
}
您可以使用 uiautomator from command line or by opening the Android Device Monitor's Hierarchy Viewer 转储应用程序的当前可见屏幕,并从每个屏幕视图中获取大量信息,包括资源 ID,这是您想要的视图 ID在你的测试中使用。
Genynmotion 出于某种原因不显示 ID,但使用真实设备或 Android 的模拟器应该可以。
您可以使用辅助函数来获取 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());
我正在从 robotium 切换到 espresso,我正在使用 apk 编写测试,我无法访问代码。 在使用 solo.getView("view-id") 的 robotium 中,我们可以访问该视图,但我不知道如何在 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());
}
}
您可以使用 uiautomator from command line or by opening the Android Device Monitor's Hierarchy Viewer 转储应用程序的当前可见屏幕,并从每个屏幕视图中获取大量信息,包括资源 ID,这是您想要的视图 ID在你的测试中使用。
Genynmotion 出于某种原因不显示 ID,但使用真实设备或 Android 的模拟器应该可以。
您可以使用辅助函数来获取 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());