ES preSSO如何访问视图,而无需使用R.id.viewid正如我们在robotium呢? [英] Espresso How to access views without using R.id.viewid as we do in robotium?

查看:412
本文介绍了ES preSSO如何访问视图,而无需使用R.id.viewid正如我们在robotium呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从robotium切换到上课preSSO,我写的apk使用 的测试中,我没有获得code。 在使用solo.getView robotium(查看-ID)我们可以访问的看法,但我不是歌厅如何做到这一点的ES preSSO? ES preSSO witId()方法需要R.id.viewid这我没有访问权限。

 公共类AAES $ P $ {pssoTest私有静态最后弦乐LAUNCHER_ACTIVITY_FULL_CLASSNAME =com.tri.re.CordActivity;
私有静态类<> launcherActivityClass;静态的 {
    尝试{
        launcherActivityClass =的Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
    }赶上(ClassNotFoundException的E){
        抛出新的RuntimeException(E);
    }
}@规则
公共ActivityTestRule<> mActivityRule =新ActivityTestRule(launcherActivityClass);
@测试公共无效testHello()抛出异常{    。OnView由(withText(浏览旧录像))执行(点击());   // ID不可访问显示红色
    OnView由(withId(R.id.button))执行(点击());}}


解决方案

您可以使用一个辅助函数来获取ID:

 私有静态诠释的getId(字符串ID){
  上下文targetContext = InstrumentationRegistry.getTargetContext();
  串的packageName = targetContext.getPackageName();
  返回targetContext.getResources()则getIdentifier(ID,ID,的packageName)。
}

然后你可以使用该ID在居preSSO:

  OnView由(withId(的getId(按钮)))执行(点击());

I am switching from robotium to espresso, I am writing tests using apk, I dont have access to code. In robotium using solo.getView("view-id") we can access the view but I am not geting how to do it in espresso? espresso witId() method needs R.id.viewid which I dont have access.

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());

}

}

解决方案

You can use a helper function to get the id:

private static int getId(String id) {
  Context targetContext = InstrumentationRegistry.getTargetContext();
  String packageName = targetContext.getPackageName();
  return targetContext.getResources().getIdentifier(id, "id", packageName);
}

Then you can use the id in Espresso:

onView(withId(getId("button"))).perform(click());

这篇关于ES preSSO如何访问视图,而无需使用R.id.viewid正如我们在robotium呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆