2017-03-13 7 views
1

Android Espressoを使用していますが、一致するものが見つからない場合、ビュー階層を印刷する際に例外がスローされます。 AndroidのView hierarchy考えるとそこにあなたがAndroidのテストを実行しているときに動的にビュー階層のこの種を取得する方法やエスプレッソAndroidまたはAndroid Espressoでビュー階層を取得するには

View Hierarchy: 
+>DecorView{id=-1, visibility=VISIBLE, width=480, height=800,  has-focus=true, 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} 
| 
+->LinearLayout{id=-1, visibility=VISIBLE, width=480, height=800, has-focus=true, 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=16909225, 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} 
| 
+-->FrameLayout{id=-1, visibility=VISIBLE, width=480, height=764, has-focus=true, 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=36.0, child-count=1} 
| 
+--->ActionBarOverlayLayout{id=2131427395, res-name=decor_content_parent, visibility=VISIBLE, width=480, height=764, has-focus=true, 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} 
| 

答えて

1

は、ツリー構造であり、アルゴリズムのいくつかの並べ替えを使用して、各ツリーノードを反復処理する方が簡単ですが、あなたはhttps://developer.android.com/reference/android/support/test/espresso/util/TreeIterables.html

それは例外トレースがbreadthFirstViewTraversalと呼ばれる方法を使用し、ViewFinderImpl https://developer.android.com/reference/android/support/test/espresso/base/ViewFinderImpl.html

+0

グレートクラスでビューツリーを印刷出力しな方法でこれらのメソッドを表示することができ、これは私の問題を解決します。 – Pezh

+0

おそらくこれを答えとしてマークしてください:)。ありがとう – WenChao

+0

ルートビューを関数に渡すために現在のアクティビティを取得する方法の追加の質問をすることができますか? – Pezh