My Dependencies:
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestCompile 'junit:junit:4.12'
I can´t find a way to simulate a click outside of an AlertDialog window to check something when it closes...
How can I do it?
The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code. By recording a test scenario, you can record your interactions with a device and add assertions to verify UI elements in particular snapshots of your app.
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. Dialog Design.
Use onView(withText("alert_dialog_text")). perform(pressBack()); this must dismiss your dialog.
Check my answer.
Espresso can't do this.
You need to use uiautomator inside your Espresso test, add this to your project's gradle:
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
After your dialog appears, you can click on the screen at any coordinate:
UiDevice device = UiDevice.getInstance(getInstrumentation());
device.click(x,y);
That will close your dialog
You can do this in many cases by creating a custom ClickAction:
public static ViewAction clickXY(final int x, final int y){
return new GeneralClickAction(
Tap.SINGLE,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final float screenX = screenPos[0] + x;
final float screenY = screenPos[1] + y;
float[] coordinates = {screenX, screenY};
return coordinates;
}
},
Press.FINGER);
}
Then you can locate a known view in your dialog (say the 'OK' button) and invoke it as follows:
onView(withText("OK")).perform(clickXY(-200, 200));
Obviously, the x/y values that you use will be dependent upon your particular situation.
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