简体   繁体   中英

Test a Snackbar with UiAutomator, is there a way?

I'm starting to study UIAutomator on Android. I created a simple poc project, inside this there are a very few elements : one button and one editText. The behaviour is quite simple: When I push the botton the message written in the editText appears in a snackBar.

Now I want to make two simple test :

  1. see if the snackbar correctly appears
  2. see if the editTest message is correctly reported in the snackbar

For point one I have done in this way :

 @Test
public void pressEmailButton() throws UiObjectNotFoundException {
    mDevice.findObject( By.res(POC_PACKAGE,"fab") ).click();

    // wait for the snackBar appear
    UiObject snackBar2 = new UiObject (new UiSelector().text("Send"));

    // Verify the snackBarIsShowed is displayed in the Ui
    assertTrue("Timeout while snackbar",  snackBar2.waitForExists(1000));
}

That is i'm watching the snackbar's action to check if the snack bar is correctly opened . Are there the better ways to do that? In this way if there are more elements named in the same way of the snackbar's action I will have a problem

For the second point I don't find a way to test it. I have to use only uiAutomator and not Espresso :)

Thanks to everyone :)

I just tried it in a new android project: I have one Button in the main layout and show the snackbar on button click like this:

button = (Button) findViewById(R.id.button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        View parentLayout = findViewById(R.id.root_layout);
        Snackbar.make(parentLayout, "Snackbar Text", Snackbar.LENGTH_LONG)
                .show();
        }
});

In my test I then test it like this:

//With espresso:
onView(withId(R.id.button)).perform(click()); //click the button
onView(withText("Snackbar Text")).check(matches(isDisplayed()));

and the same with using UI Automator:

UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject snackbarTextView = mDevice.findObject(new UiSelector().text("Snackbar Text"));

if(!snackbarTextView.getText().equals("Snackbar Text")) {
    throw new RuntimeException("No snackbar!");
}

Both tests are working just fine! Another way to select the Snackbar text would be via the resource id like this:

//replace the package name in the next line with your package name
UiObject snackbarTextView = mDevice.findObject(new UiSelector().resourceId("com.example.testespressoapplication:id/snackbar_text"));

The way you did it also works but is deprecated so I would not use it, as the documentation says:

* @deprecated Use {@link UiDevice#findObject(UiSelector)} instead. This version hides
* UiObject's dependency on UiDevice and is prone to misuse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM