Android Espresso如何使用apk编写测试?

Android Espresso how to write tests using apk?

我是 robotium 用户,现在 正在切换到 Espresso 谁能告诉我如何像我们一样在 Espresso 中使用 apk 编写测试在 robotium 中,无需访问代码,但使用应用程序 apk。

以及如何在没有 R.id.viewid 的情况下访问 espresso 中的视图?正如我们在 robotium

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

  }
}

如何指定class名称?

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

@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() {

  }
}