I'm trying to finally add UI tests to my Android application, to increase coverage (all my other layers are properly tested, hence all my bugs now come from the UI...)
I started to use ActivityInstrumentationTestCase2
as my base class for emulator unit-tests, and simple things are easy to check and work nice.
But now, I'm trying to check a Dialog appears as expected, and I don't know how to do that.
My test:
public void testOpensAboutDialogWhenAboutButtonClicked() {
final MyActivity activity = getActivity();
final Instrumentation instrumentation = getInstrumentation();
final Button aboutButton = (Button) activity.findViewById(R.id.about);
TouchUtils.clickView(this, aboutButton);
// how to test for the AboutDialog?
}
Now my dialog doesn't have an id, so I can't get a pointer to it using findViewById. It has been created using the builder classes available:
final AlertDialog about = new AlertDialog.Builder(parent)
.setTitle(parent.getString(R.string.about_title))
.setCancelable(true)
.setIcon(R.drawable.skull)
....
Any ideas, or pointers to tutorials?
EDIT: To answer Jens comment, I'm not using managed dialogs, just creating the AlertDialog and showing it with .show()
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().
Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is use to set the icon on Alert dialog box.
We have discussed about how to create a dialog in one of our articles Dialog. In this tutorial we would be learning how to create an activity dialog in android. As shown in the below figure, an Activity Dialog is like an Activity floating over another Activity(parent activity).
Since you're already using ActivityInstrumentationTestCase2
you should start using Robotium - it will simplify your testing a lot.
For your case it's as easy as this (if you know the expected title or something else vaguely unique about your dialog):
public void testSomeRandomSentence() {
Solo solo = new Solo(getInstrumentation(), getActivity());
getInstrumentation().waitForIdleSync();
// Now do whatever you need to do to trigger your dialog.
// Let's assume a properly lame dialog title.
assertTrue("Could not find the dialog!", solo.searchText("My Dialog Title"));
}
after assigning id to Toast in setUp() by
toast = (Toast)activity.findViewById(..........);
create testcase() {
ViewAsserts.assertOnScreen(toasts.getRootView(), toast.getRootView());
//pass if toast is visible on screen
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With