How to access editText in MaterialDialog and perform typing (Android ui test) -


enter image description here

this code

@runwith(androidjunit4.class) @largetest public class changetextbehaviortest {  public static final string string_to_be_typed = "espresso";  @rule public activitytestrule<landingactivity> mactivityrule = new activitytestrule<> (         landingactivity.class );  @test public void changetext_sameactivity () {     string testtext = "testtext";     onview ( withid ( r.id.action_a) )             .perform ( click (), closesoftkeyboard () );     onview ( withhint ( r.string.add_list ) ).perform ( typetextintofocusedview ( testtext ) );     onview ( withtext ("add" ) ).perform ( click () );  } 

first need click on floatingbutton open materialdialog

then try access edittext reference hint

onview ( withhint ( r.string.add_list ) ).perform ( typetextintofocusedview ( testtext ) ); 

but test got error. seem dialog not in view hierarchy.

    running tests test running started android.support.test.espresso.nomatchingviewexception: no views in hierarchy found matching: string resource id: <2131099668>[add_list] value: add list if target view not part of view hierarchy, may need use espresso.ondata load 1 of following adapterviews:com.nhaarman.listviewanimations.itemmanipulation.dynamiclistview{42ed6100 v.ed.vcl ........ 0,0-1080,1692 #7f0c0074 app:id/listviewtaskincomplete}  view hierarchy: +>decorview{id=-1, visibility=visible, width=1080, height=1920, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} | +->linearlayout{id=-1, visibility=visible, width=1080, height=1920, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} | +-->viewstub{id=16909084, visibility=gone, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} | +-->framelayout{id=-1, visibility=visible, width=1080, height=1860, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=60.0, child-count=1} | +--->fitwindowslinearlayout{id=2131492948, res-name=action_bar_root, visibility=visible, width=1080, height=1860, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} | +---->viewstubcompat{id=2131492949, res-name=action_mode_bar_stub, visibility=gone, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} | +---->contentframelayout{id=16908290, res-name=content, visibility=visible, width=1080, height=1860, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} | +----->coordinatorlayout{id=2131492976, res-name=rootlayout, visibility=visible, width=1080, height=1860, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} | +------>appbarlayout{id=-1, visibility=visible, width=1080, height=540, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} | +------->collapsingtoolbarlayout{id=2131492977, res-name=collapsingtoolbarlayout, visibility=visible, width=1080, height=540, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} | +-------->imageview{id=-1, visibility=visible, width=1080, height=540, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} | +-------->toolbar{id=2131492978, res-name=toolbar, visibility=visible, width=1080, height=168, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} | +--------->view{id=-1, visibility=visible, width=888, height=168, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=48.0, y=0.0} | +--------->actionmenuview{id=-1, visibility=visible, width=144, height=168, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=936.0, y=0.0, child-count=1} | +---------->actionmenuitemview{id=2131493079, res-name=menu_setting, desc=settings, visibility=visible, width=144, height=144, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0, text=, input-type=0, ime-target=false, has-links=false} | +------>nestedscrollview{id=2131492979, res-name=nested_scroll_view, visibility=visible, width=1080, height=1860, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=540.0, child-count=1} | +------->dynamiclistview{id=2131492980, res-name=listviewtaskincomplete, visibility=visible, width=1080, height=1692, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} | +------>floatingactionsmenu{id=2131493035, res-name=multiple_actions, visibility=visible, width=222, height=828, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=429.0, y=1032.0, child-count=3} | +------->floatingactionbutton{id=2131493036, res-name=action_b, visibility=visible, width=222, height=222, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=138.0} | +------->floatingactionbutton{id=2131493037, res-name=action_a, visibility=visible, width=222, height=222, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=372.0} | +------->{id=2131492868, res-name=fab_expand_menu_button, visibility=visible, width=222, height=222, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=606.0} | 

this code create dialog lib https://github.com/afollestad/material-dialogs

    public static void showaddlistdialog ( final activity thiscontext, final listview listview ) {     materialdialog scoredialog = new materialdialog.builder ( thiscontext )             //.customview ( r.layout.dialog_todo, true )             .title ( thiscontext.getstring ( r.string.add_list ) )             .positivetext ( "add" )             .input ( thiscontext.getstring ( r.string.add_list ), "", new materialdialog.inputcallback () {                 @override public                 void oninput ( materialdialog materialdialog, charsequence charsequence ) {                     queryhelper.addlisttodb ( thiscontext, string.valueof ( charsequence ), listview );                 }             } )             .negativetext ( "cancel" )             .show (); } 

i can think of 2 possible causes:

1) might dialog still transitioning , espresso not consider visible. or not yet attached.

sometime espresso fails way - if promises wait until main thread idle, sometime doesn't work.

when need actions on dialogs use couple of helper methods wait little bit transitions , whatnot finish. (code below)

2) espresso searching view on wrong root view


click on view in material dialog (using same lib you)

public void test() {     // opens time picker dialog     onview(withid(r.id.block_edit_start_time)).perform(click());      // click on dialog ok button    performmaterialdialogokclick(); }  /**  * clicks positive button in visible/active {@link com.afollestad.materialdialogs.materialdialog}  */ public static void performmaterialdialogokclick() {     onview(withid(r.id.buttondefaultpositive)).inroot(isdialog()).perform(click()); } 

waiting transitions complete

public void test() {     // open screen     goto(new xyzscreen(datetime.now(), null));      // wait time transitions complete     onview(isroot()).perform(waitatleast(300));      // can screenshot     screenshot(d); }  /**  * perform action of waiting specific time. useful when need  * wait animations end , espresso fails @ waiting.  * <p/>  * e.g.:  * onview(isroot()).perform(waitatleast(sampling.seconds_15));  *  * @param millis  * @return  */ public static viewaction waitatleast(final long millis) {     return new viewaction() {         @override         public matcher<view> getconstraints() {             return anything();         }          @override         public string getdescription() {             return "wait @ least " + millis + " millis.";         }          @override         public void perform(final uicontroller uicontroller, final view view) {             uicontroller.loopmainthreaduntilidle();             uicontroller.loopmainthreadforatleast(millis);         }     }; }  /**  * perform action of waiting until ui thread free.  * <p/>  * e.g.:  * onview(isroot()).perform(waituntilidle());  *  * @return  */ public static viewaction waituntilidle() {     return new viewaction() {         @override         public matcher<view> getconstraints() {             return anything();         }          @override         public string getdescription() {             return "wait until ui thread free";         }          @override         public void perform(final uicontroller uicontroller, final view view) {             uicontroller.loopmainthreaduntilidle();         }     }; } 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -