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.
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.
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 .
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
.
Just use solo.clickOnButton("Cancel");
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