How to select which button to click on Robotium for an alert dialog?
Asked Answered
B

2

10

I am new to Robotium. I have created an alert dialog box using dialog builder and called it using the show command. I was able to trigger the 'ok' button by default using Robotium and I am not able to do the same for the 'cancel' button. As the dialog box is not associated with an id, I am not sure how to get the id of the buttons. Here is my code for the dialog box

alertDialogBuilder
.setMessage("Please enter only numbers without any spaces")
.setCancelable(true)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int id) {
 dialog.cancel();
 }
 })
 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int id) {
 dialog.cancel();
 }
 });

The code I used to trigger the 'ok' button in the Test Class is

 solo.getCurrentActivity().runOnUiThread(new Runnable() {
 public void run() {
 solo.getCurrentActivity().getCurrentFocus().requestFocus();
 }
 });
 this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);

How to do the same for the 'cancel' button? Thanks in advance.

Bunkmate answered 27/4, 2012 at 23:37 Comment(0)
B
9

Just use solo.clickOnButton("Cancel");

Biarritz answered 14/5, 2012 at 15:49 Comment(1)
ya. had it fixed. Thanks RenasBunkmate
S
22

Actually, I suggest you do solo.clickOnView(solo.getView(buttonId)) where the 'Positive' button is android.R.id.button1, the 'Negative' button is android.R.id.button2 and 'Neutral' is android.R.id.button3.

Ski answered 1/6, 2012 at 22:32 Comment(2)
This has proved much more reliable for me than the accepted answer. Thanks.Oneidaoneil
what if we have custom dialog??Lefler
B
9

Just use solo.clickOnButton("Cancel");

Biarritz answered 14/5, 2012 at 15:49 Comment(1)
ya. had it fixed. Thanks RenasBunkmate

© 2022 - 2024 — McMap. All rights reserved.