Check if a dialog is displayed with Espresso

后端 未结 6 602
孤街浪徒
孤街浪徒 2020-12-04 14:07

I\'m trying to write some tests with the new android-test-kit (Espresso). But I can\'t find any information on how to check if a dialog is displayed and per

6条回答
  •  没有蜡笔的小新
    2020-12-04 14:55

    The button Ids R.id.button1 and R.id.button2 are not going to be same across devices. The Ids may change with the OS versions.

    The correct way to achieve this is to use UIAutomator. Include UIAutomator dependency in your build.gradle

    // Set this dependency to build and run UI Automator tests
      androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    

    and use

    // Initialize UiDevice instance
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    
    // Search for correct button in the dialog.
    UiObject button = uiDevice.findObject(new UiSelector().text("ButtonText"));
    if (button.exists() && button.isEnabled()) {
        button.click();
    }
    

提交回复
热议问题