1

私はonView()を使用して、フラグメントの内部にあるEditTextビューを取得しようとしていますが、各フラグメントのすべてのEditTextは同じIDを持っています。この擬似コードを考えるエスプレッソで再利用された断片から複数の同一のIDからEditText Viewを取得するには?

:私のテストケースで

<Activity> 
    <Fragment> 
     <EditText android:id="@+id/input" /> 
    </Fragment> 
    <Fragment> 
     <EditText android:id="@+id/input" /> 
    </Fragment> 
    <Fragment> 
     <EditText android:id="@+id/input" /> 
    </Fragment> 
</Activity> 

、私は実行時に

onView(withId(R.id.input)).perform(typeText("foo")); 

をやっているが、私はwith id: com.foo.bar.foobar:id/input' matches multiple views in the hierarchy.を取得しています。ビュー階層は以下の通りです:(私は読みやすくするための要素の前に「MATCHES」を移動)

View Hierarchy: 
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, 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=1080, height=1794, 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=16909295, 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=1080, height=1794, 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=1} 
| 
+--->FitWindowsLinearLayout{id=2131427402, res-name=action_bar_root, visibility=VISIBLE, width=1080, height=1794, 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} 
| 
+---->ViewStubCompat{id=2131427403, 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=1794, 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=1} 
| 
+----->ConstraintLayout{id=2131427422, res-name=constraintLayout, visibility=VISIBLE, width=1080, height=1794, 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=7} 
| 
+------>AppCompatTextView{id=2131427423, res-name=stepHeader, visibility=VISIBLE, width=321, height=140, 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=380.0, y=168.0, text=Step 1:, input-type=0, ime-target=false, has-links=false} 
| 
+------>AppCompatTextView{id=2131427425, res-name=stepTitle, visibility=VISIBLE, width=521, height=88, 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=280.0, y=361.0, text=Basic Information, input-type=0, ime-target=false, has-links=false} 
| 
+------>AppCompatTextView{id=2131427426, res-name=stepInstruction, visibility=VISIBLE, width=141, height=71, 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=470.0, y=712.0, text=Name, input-type=0, ime-target=false, has-links=false} 
| 
+------>AppCompatButton{id=2131427427, res-name=nextButton, visibility=VISIBLE, width=231, height=126, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=425.0, y=1479.0, text=Next, input-type=0, ime-target=true, has-links=false} 
| 
+------>AppCompatButton{id=2131427424, res-name=backButton, visibility=VISIBLE, width=231, height=126, 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=21.0, y=21.0, text=Back, input-type=0, ime-target=false, has-links=false} 
| 
+------>ConstraintLayout{id=2131427428, res-name=progressBarContainer, visibility=VISIBLE, width=1080, height=105, 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=1689.0, child-count=2} 
| 
+------->AppCompatTextView{id=2131427429, res-name=progressBar, visibility=VISIBLE, width=1080, height=105, 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, text=Progress bar, input-type=0, ime-target=false, has-links=false} 
| 
+------->View{id=2131427430, res-name=progressIndicator, visibility=VISIBLE, width=154, height=11, 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=94.0} 
| 
+------>ConstraintLayout{id=2131427431, res-name=stepContainer, visibility=VISIBLE, width=1080, height=696, 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=783.0, child-count=6} 
| 
+------->ConstraintLayout{id=2131427436, res-name=viewStepInput, visibility=VISIBLE, width=1080, height=696, 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} 
| 
****MATCHES**** 
+-------->AppCompatEditText{id=2131427437, res-name=userInput, visibility=VISIBLE, width=1038, height=118, 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=true, editor-info=[inputType=0x1 imeOptions=0xc000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0 hintText=null label=null packageName=null fieldId=0 fieldName=null extras=null hintLocales=null contentMimeTypes=null ], x=21.0, y=289.0, text=, input-type=1, ime-target=false, has-links=false} 
| 
+------->ConstraintLayout{id=2131427436, res-name=viewStepInput, 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, child-count=1} 
| 
+-------->GridView{id=2131427439, res-name=selector_grid, visibility=VISIBLE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
| 
+------->ConstraintLayout{id=2131427436, res-name=viewStepInput, 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, child-count=1} 
| 
****MATCHES**** 
+-------->AppCompatEditText{id=2131427437, res-name=userInput, visibility=VISIBLE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=true, editor-info=[inputType=0x1 imeOptions=0xc000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0 hintText=null label=null packageName=null fieldId=0 fieldName=null extras=null hintLocales=null contentMimeTypes=null ], x=0.0, y=0.0, text=, input-type=1, ime-target=false, has-links=false} 
| 
+------->ConstraintLayout{id=2131427436, res-name=viewStepInput, 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, child-count=1} 
| 
+-------->GridView{id=2131427439, res-name=selector_grid, visibility=VISIBLE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
| 
+------->ConstraintLayout{id=2131427436, res-name=viewStepInput, 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, child-count=1} 
| 
+-------->GridView{id=2131427439, res-name=selector_grid, visibility=VISIBLE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
| 
+------->ConstraintLayout{id=2131427436, res-name=viewStepInput, 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, child-count=1} 
| 
****MATCHES**** 
+-------->AppCompatEditText{id=2131427437, res-name=userInput, visibility=VISIBLE, width=0, height=0, 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=true, is-selected=false, root-is-layout-requested=false, has-input-connection=true, editor-info=[inputType=0x1 imeOptions=0xc000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0 hintText=null label=null packageName=null fieldId=0 fieldName=null extras=null hintLocales=null contentMimeTypes=null ], x=0.0, y=0.0, text=, input-type=1, ime-target=false, has-links=false} 
| 
+->View{id=16908336, res-name=navigationBarBackground, visibility=VISIBLE, width=1080, height=126, 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=1794.0} 
| 
at dalvik.system.VMStack.getThreadStackTrace(Native Method) 
at java.lang.Thread.getStackTrace(Thread.java:1566) 
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92) 
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56) 
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184) 
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115) 
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87) 
at com.newfoundry.skyline.facialrecognitionkioskapp.ActivityRegistrationScreenTest.testFilledInputFieldShouldIncrementCurrentStepOnNext(ActivityRegistrationScreenTest.java:73) 
at java.lang.reflect.Method.invoke(Native Method) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) 
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270) 
at org.junit.rules.RunRules.evaluate(RunRules.java:20) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932) 

私がやって試してみた:

EditText input = mActivity.getSupportFragmentManager() 
      .findFragmentByTag("foo") 
      .getView() 
      .findViewById(R.id.input); 

onView(withId(input)); 

しかし、同様にそのの問題に実行しています。この特定の問題に対処するものは何も見つかりませんでした。どんな助けでも大いに感謝します。

+0

エラーメッセージの下に印刷されたビュー階層を追加できますか? –

+0

はビュー階層を追加しました! –

+0

これは問題の解決策に関連していますか? https://stackoverflow.com/a/39756832/1597616。 –

答えて

1

私の理解では、何らかの方法でTextViewsを区別する必要があります。最初のフラグメントがid "fragment1"を持ち、2番目のフラグメントがid "fragment2"を持つとします。

次に、あなたがそのフラグメントに単一のTextViewを選択して、あなたのテストケースにそれを使用するだけに

onView(allOf(withParent(withId(R.id.fragment1)),instanceOf(EditText.class))).perform(typeText("foo")); 

を使用することができます。

+0

問題は、エスプレッソをアクティビティ内で取得するのではなく、テストスイートで使用する場合にのみ発生します。 findFragmentByTagの問題に関して、 'onView'メソッドはMatcherを探しています

+0

変更が役に立ちましたか?元の投稿を編集するときに通知を受け取るかどうかわかりません。 – Zack

+0

ちょうどこれを見て、一度それを試してみると、私は本当に素早く何かを修正します。 –

関連する問題