0

I am a robotium user now switching to Espresso can anyone tell me how to write tests using apk in espresso, as we do in robotium without having acccess to the code but using app apk.

And how to access views without R.id.viewid in espresso? as we do in robotium

solo.getview("viewidText")

In robotium this is how we do

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

In espresso

 @RunWith(AndroidJUnit4.class)
public class MainActivityTest {

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

@Test
public void launchMain(){

  }
}

How to specify the class name?

Shivaraj Patil
  • 8,186
  • 4
  • 29
  • 56
  • UI Automator would be a better fit for this sort of source-free integration testing, IMHO. – CommonsWare Apr 27 '15 at 12:46
  • @CommonsWare thanks, we have complex functionality tests, doing those with ui automator in not feasible. We have two separate projects so as of now we cannot integrate espresso tests directly to main project so finding a workaround to write tests using apk and once the test project is ready we will integrate with it main project. – Shivaraj Patil Apr 28 '15 at 11:07

1 Answers1

0

You can use the same reflection technique to specify the class in 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() {

  }
}
chiuki
  • 14,580
  • 4
  • 40
  • 38
  • Ya, I did same, but in robotium solo.getView("view_name") gives access to view but espresso needs R.id.viewname as I said I dont have access to code so what is the workaround? how to access view? help would be appreciated. – Shivaraj Patil Apr 29 '15 at 12:56
  • Please ask a different question for that. – chiuki Apr 29 '15 at 13:27
  • http://stackoverflow.com/questions/29946970/espresso-how-to-access-views-without-using-r-id-viewid-as-we-do-in-robotium – Shivaraj Patil Apr 29 '15 at 14:44