【问题标题】:Android Espresso how to write tests using apk?Android Espresso 如何使用 apk 编写测试?
【发布时间】:2015-04-27 12:23:58
【问题描述】:

我现在是 robotium 用户 切换到 Espresso 谁能告诉我如何在 espresso 中使用 apk 编写测试,就像我们在 robotsium 中所做的那样,无需访问代码但使用应用 apk。

以及如何在 espresso 中访问没有 R.id.viewid 的视图?就像我们在机器人中所做的那样

solo.getview("viewidText")

在robotium中我们就是这样做的

public class CoreTest extends ActivityInstrumentationTestCase2 {
private Solo solo;

//class name of the app launcher activity
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.rex.binderapps.RecorderActivity";


private static Class<?> launcherActivityClass;

static {
    try {
        launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

@SuppressWarnings("unchecked")
public CoreRecordingTest() throws ClassNotFoundException {
    super(launcherActivityClass);
}

public void setUp() throws Exception {
    super.setUp();
    solo = new Solo(getInstrumentation());
    Test.setup(this);
    getActivity();
}

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
    super.tearDown();
}
...

浓缩咖啡

 @RunWith(AndroidJUnit4.class)
public class MainActivityTest {

// RecorderActivity is not accessible
@Rule public final ActivityRule<RecorderActivity> main = new ActivityRule<>(RecorderActivity.class);

@Test
public void launchMain(){

  }
}

如何指定类名?

【问题讨论】:

  • UI Automator 更适合这种无源集成测试,恕我直言。
  • @CommonsWare 谢谢,我们有复杂的功能测试,用 ui automator 做那些是不可行的。我们有两个独立的项目,因此目前我们无法将 espresso 测试直接集成到主项目中,因此找到一种解决方法来使用 apk 编写测试,一旦测试项目准备就绪,我们将与主项目集成。

标签: android junit4 robotium android-testing android-espresso


【解决方案1】:

您可以使用相同的反射技术来指定ActivityTestRule中的类:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

  private static final String CLASSNAME = "com.rex.binderapps.RecorderActivity";

  private static Class<? extends Activity> activityClass;
  static {
    try {
      activityClass = (Class<? extends Activity>) Class.forName(CLASSNAME);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
  }

  @Rule 
  public final ActivityTestRule<?> activityRule 
      = new ActivityTestRule<>(activityClass);

  @Test
  public void launchMain() {

  }
}

【讨论】:

  • 是的,我做了同样的事情,但是在机器人中,solo.getView("view_name") 可以访问视图,但 espresso 需要 R.id.viewname,因为我说过我无法访问代码所以什么是解决方法?如何访问视图?帮助将不胜感激。
  • 请对此提出不同的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
相关资源
最近更新 更多