1

Have a fragment that creates an alert dialog to ask the user if they wish to call an ambulance. How do I test that it appears , what each button does etc. I have tried

 onView(withId(titleId))
            .inRoot(isDialog().)
            .check(matches(withText("Call Ambulance")))
            .check(matches(isDisplayed()));   

But this fails saying that "Call Ambulance" cannot be found in the root view hierarchy. The buttons dont have assigned views like R.id.button one so how do I test these also.

 public class AmbulanceFragment extends DialogFragment {
        final Context context = getActivity();


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        Log.d(TAG, "Alert Dialog");

        builder.setTitle("CALL AMBULANCE? ");

        builder.setMessage("Do you wish to call an Ambulance?");

        builder.setIcon(R.drawable.warning);

        builder.setPositiveButton("CALL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent call = new Intent(Intent.ACTION_DIAL);
                String number = "tel:" + getString(R.string.phone_number);
                call.setData(Uri.parse(number));
                startActivity(call);
                Log.d(TAG, "Call button pressed and keypad launched");

            }


        });


        builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }


        });


        return builder.create();

    }
Chris
  • 29
  • 1
  • 7
  • Unfortunately, it's unclear what you're asking for. `"Call Ambulance"` and `"CALL AMBULANCE? "` are different strings, so they will not match. `titleId` is undefined in the first block of code. Also you say "The buttons dont have assigned views like R.id.button", but they do have assigned view IDs, like `android.R.id.button1` -- as shown in the answers here: https://stackoverflow.com/questions/21045509/check-if-a-dialog-is-displayed-with-espresso – Mr-IDE Aug 14 '20 at 08:36
  • Sorry I had updated it to the correct string, what should go in the titleId definition? – Chris Aug 14 '20 at 09:09

0 Answers0