Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 631
Kavin Arasu Avatar asked Apr 27 '12 23:04

Kavin Arasu


People also ask

How can I show alert dialog on top of any activity in Android app?

setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog. The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.

What's the difference between dialog and AlertDialog in Android?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .


2 Answers

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.

like image 50
Avi Cherry Avatar answered Oct 25 '22 15:10

Avi Cherry


Just use solo.clickOnButton("Cancel");

like image 24
Renas Avatar answered Oct 25 '22 17:10

Renas