安卓Espresso黑盒测试

4
我正在尝试使用Android Espresso对第三方apk文件进行黑盒测试。我无法访问第三方apk文件的源代码。因此,我可以使用UIAutomatorViewer获取UI元素ID。然而,在Espresso文件中,我无法访问“R”。因此,当我调用onView(withId(R.id.<ui id>))时,它会返回一个错误:

package R does not exist

例如:
onView(withId(R.id.fragment_onboarding_skip_button)).perform(click());
1个回答

2
可以通过创建一个提取整数ID的方法来解决问题:
...    
public int getId(String id) {
    Context appContext = InstrumentationRegistry.getTargetContext();
    return appContext.getResources().getIdentifier(id, "id", "<applicationId>");
}

@Test
public void testSomething() {
    //here just pass the ID name
    onView(withId(getId("fragment_onboarding_skip_button"))).perform(click());
}
...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接