android - Espresso How to access views without using R.id.viewid as we do in robotium? -


i switching robotium espresso, writing tests using apk, dont have access code. in robotium using solo.getview("view-id") can access view not geting how in espresso? espresso witid() method needs r.id.viewid 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 not accessible shows red     onview(withid(r.id.button)).perform(click());  }  } 

you can use helper function id:

private static int getid(string id) {   context targetcontext = instrumentationregistry.gettargetcontext();   string packagename = targetcontext.getpackagename();   return targetcontext.getresources().getidentifier(id, "id", packagename); } 

then can use id in espresso:

onview(withid(getid("button"))).perform(click()); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -