0

Cucumber-JVMとEspressoを使用してAndroid UIテストスイートを実行しています。しかし、私は対話の断片と対話することに問題に直面しています。isDialog()がEspressoのダイアログフラグメントと一致しません

もう1つのフラグメントは、いくつかのビュー要素を含むダイアログフラグメントを上に作成します。私がチェック/これらと対話しようとすると、エスプレッソはビューの階層内でエスプレッソを見つけられないようです。エラーメッセージに表示されるビュー階層は、ダイアログフラグメントではなく、根底にあるフラグメントのビュー階層であり、誤ったルートビューを取得していると考えられます。この問題を解決しようとする

は、私は、ステートメントにinRoot(isDialogを())を追加しました:

onView(withText("Ok")).inRoot(isDialog()).check(matches(isDisplayed())); 

これは、結果、次のエラーメッセージ

android.support.test.espresso.NoMatchingRootException: Matcher 'is dialog' did not match any of the following roots: [Root{[email protected], [email protected], has-window-focus=true, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#20 ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{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}}, Root{[email protected], [email protected], has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#20 ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=INVISIBLE, width=1080, height=1794, has-focus=true, has-focusable=false, has-window-focus=false, 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}}, Root{[email protected], [email protected], has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=INVISIBLE, width=1080, height=1794, has-focus=true, has-focusable=false, has-window-focus=false, 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}}, Root{[email protected], [email protected], has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#1810100 wanim=0x10303e5 needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=INVISIBLE, width=1080, height=1794, has-focus=false, has-focusable=false, has-window-focus=false, 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}}] 
at dalvik.system.VMStack.getThreadStackTrace(Native Method) 
at java.lang.Thread.getStackTrace(Thread.java:580) 

ダイアログを作成するためのコード:

public class ServiceItemFragment extends Fragment { 
    ... 
    public void showASampleDialog() { 
     DialogFragment newFragment = SampleDialogFragment.newInstance(
       R.string.dialog_title); 
     newFragment.show(getFragmentManager(), "dialog"); 
    } 
} 

ダイアログフラグメントのコード:

この問題を解決しようとする
public class SampleDialogFragment extends DialogFragment { 

    public static SampleDialogFragment newInstance(int title) { 
     SampleDialogFragment frag = new SampleDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("title", title); 
     frag.setArguments(args); 
     return frag; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     int title = getArguments().getInt("title"); 

     return new AlertDialog.Builder(getActivity()) 
       .setTitle(title) 
       .setPositiveButton("Ok", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           Log.i(getClass().getSimpleName(), "Positive click"); 
          } 
         } 
       ) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           Log.i(getClass().getSimpleName(), "Negative click"); 
          } 
         } 
       ) 
       .create(); 
    } 
} 
+0

当面はUIAutomatorの問題を回避しています: UiDeviceデバイス= UiDevice.getInstance(getInstrumentation());UiObject uiObject = device.findObject(new UiSelector()。text( "Ok")); { uiObject.click(); } catch(UiObjectNotFoundException e){ throw new RuntimeException( "UIオブジェクトが見つかりません"、e); } – slarbu

答えて

2

、私は声明にinRoot(isDialogを())を追加しました:

onView(withText("Ok")).inRoot(isDialog()).check(matches(isDisplayed())); 

をおAlertDialogを使用していた場合、それが仕事だろうが、ないDialogFragment

この場合、実際の回避策は実際の回避策です。つまり、

UIAutomator: UiDevice device = UiDevice.getInstance(getInstrumentation()); 
UiObject uiObject = device.findObject(new UiSelector().text("Ok")); 
try { 
    uiObject.click(); 
} catch (UiObjectNotFoundException e) { 
    throw new RuntimeException("UI Object not found", e); 
} 

同様のポストと答えがここにあります:Android UI testing with Espresso on an AlertDialog inner views

inRoot()マッチャーをOMMITし、できるだけ簡単なテストなどを書くことも試してみてください。

onView(withText("Ok")).check(matches(isDisplayed())); 

また、これは便利かもしれません:http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

+0

さて、私は回避策を続けます。エスプレッソがDialogFragmentsをまったく使うことができない場合、少し残念です。 – slarbu

1

現在のアクティビティではないルートと一致するものもあります:

onView(withText("Ok")).inRoot(withDecorView(not(is(testRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed())

デバイスアニメーションの問題でもあります。あなたのデバイスのdev設定でそれを無効にしてください。

+0

このオプションは、テストが停止し、後で非アクティブなrootviewが原因で終了するため、他のルートと一致するようです。私はまた、開発者の設定でアニメーションを無効にしました。 – slarbu

関連する問題