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

时间:2015-04-27 12:23:59

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

我是机器人用户现在切换到Espresso 任何人都可以告诉我如何使用apk在espresso中编写测试,就像我们在robotium中做的那样,没有接受代码但使用app apk。

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

solo.getview("viewidText")

在机器人中这就是我们的工作方式

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

  }
}

如何指定班级名称?

1 个答案:

答案 0 :(得分:0)

您可以使用相同的反射技术在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() {

  }
}