Search code examples
androidui-testingandroid-uiautomator

Alternative of exists() in UIObject2


With UIObject we have a method : exists() to check if view exists
I am looking for same alternative with UIObject2.


Solution

  • You can do it this way:

    import android.support.test.uiautomator.UiDevice; 
    import android.support.test.uiautomator.UiObject2;
    import android.support.test.InstrumentationRegistry; 
    
    
    private UiDevice device = UiDevice.getInstance(
        InstrumentationRegistry.getInstrumentation()
    );
    List<UiObject2> objs = device.findObjects(...)
    // Now you can check if objs.size() > 0