0

I am performing a click on the "Set"-button in a DatePickerDialog with Robotium via

solo.clickOnButton("Set"); 

If I now change the language of the testing device to a different language, Robotium is not able to find the button, as the text is not "Set" anymore but the translated word.

Is there any possibility to access the button in the Picker in a different way?

As in Jelly Bean the DatePicker lost the "Cancel" button, I cannot use the clickOnButton(int index) method.

The only idea I have would be to use setButton on the DatePickerDialog to have access to the localized string resource of the button text or keep a reference to the button. But maybe someone knows of a better way to gain access without the need of custom button text.

Regards Kim

Micky
  • 5,578
  • 7
  • 31
  • 55

4 Answers4

0

If you have access to the source code, you can use both getString() and getView():

Button button = (Button) solo.getView(R.id.x);

solo.clickOnView(button);

There is also solo.getString(R.string.x) that is good to use for localized builds.

Renas
  • 1,919
  • 1
  • 18
  • 17
  • Thanks for your answer. I know those methods. The x is the crucial part, how do I get the id or string resource for the "set" button of a standard DatePickerDialog? As I said, without passing in a custom button reference? – Micky Aug 10 '12 at 14:09
0

I know that it's not the best solution but it works for me:

solo.clickOnButton(0);
Semyon
  • 528
  • 5
  • 16
0

Here's my suggestion (assuming you are showing the dialog via a DialogFragment): I have a SelectDateDialogFragment with a unique TAG and an onCreateDialog() method which creates a DatePickerDialog. I then show the dialog via selectDateDialogfragment.show(getFragmentManager(), SelectDateDialogFragment.TAG). In the Robotium tests, I use code like the following to click the dialog's buttons:

solo.clickOnView(editDateButton);

solo.waitForFragmentByTag(SelectDateDialogFragment.TAG);

solo.setDatePicker(0, 2000, 1, 1);

SelectDateDialogFragment dialogFragment = (SelectDateDialogFragment) activity.getFragmentManager()
        .findFragmentByTag(SelectDateDialogFragment.TAG);
DatePickerDialog dialog = (DatePickerDialog) dialogFragment.getDialog();
Button okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
solo.clickOnView(okButton);

solo.waitForDialogToClose();
csoltenborn
  • 1,127
  • 1
  • 12
  • 22
0

I would like to share some details with you.

First:

solo.clickOnButton(0);

worked well for me some time. But as the new Dialogs don't have the "Set" and "Cancel" buttons, but instead "Cancel" and "OK", this solution would now select the cancel button on newer devices while just switching to

solo.clickOnButton(1);

would break the test for older devices.

So I migrated to csoltenborn's solution with two modifications:

  • as I want to stay compatible with older devices I use the SupportFragmentManager
  • as my fragment is nested in another fragment depending on the device and it's orientation, I sometimes have to access a certain fragments ChildFragmentManager.

This is my solution, maybe it can add to csoltenborn's good answer:

DialogFragment dialogFrag;

Fragment outerFragment = getActivity().getSupportFragmentManager().findFragmentByTag("outerFragmentTAG");
    if (outerFragment == null) {
        dialogFrag = (DialogFragment)getActivity().getSupportFragmentManager().findFragmentByTag("datePicker");
    } else {
        dialogFrag = (DialogFragment)outerFragment.getChildFragmentManager().findFragmentByTag("datePicker");
    }
    Button okButton = ((DatePickerDialog)dialogFrag.getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
    solo.clickOnView(okButton);
Christoph
  • 1
  • 1