user2028928
user2028928

Reputation: 91

Robotium & system dialogs

When I try to pair with Bluetooth device, system confirmation dialog with PIN appears. There are buttons "Cancel" and "OK". But I can't click them with Robotium. How can I work with Android OS dialogs in Robotium? Thanks.

Upvotes: 2

Views: 1492

Answers (5)

Ray
Ray

Reputation: 501

Robotium can be difficult when it comes to Android system dialog box buttons however there is a solution.

Found the answer on this post on stack

// Set this dependency in your app's gradle file
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

and use this code snippet in your test project:

// Initialize UiDevice instance
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

// Search for correct button in the dialog.
UiObject button = uiDevice.findObject(new UiSelector().text("ButtonTest"));

if (button.waitForExists(5000)) {
    button.click();
}

Upvotes: 1

Onivas
Onivas

Reputation: 320

As @kamal_prd said, you cannot because the dialog is not part of the same application. May be you could use

clickOnScreen(float x, float y) //Clicks the specified coordinates

I know it is hard to manage with different screen resolution/size, but it is what I'm also using to test my app.

Upvotes: 0

mlchen850622
mlchen850622

Reputation: 668

This works for me:

solo.clickOnView(solo.getView(android.R.id.button1)); 

where the 'Positive' button is android.R.id.button1, the 'Negative' button is android.R.id.button2 and 'Neutral' is android.R.id.button3.

Upvotes: 2

kamal_prd
kamal_prd

Reputation: 543

It is not possible to write a test case that spans over 2 applications. But, if it is part of same application then you can use solo.clickOnText("Cancel"). Same way you can click on other buttons by clicking on their texts.

Upvotes: 1

Basil
Basil

Reputation: 855

you can use solo.clickOnView(solo.getView(buttonId))

Upvotes: -2

Related Questions