|
| 1 | +# espresso |
| 2 | + |
| 3 | +Provides bindings for Espresso tests of Flutter Android apps. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add the `espresso` package as a `dev_dependency` in your app's pubspec.yaml. If you're testing the example app of a package, add it as a dev_dependency of the main package as well. |
| 8 | + |
| 9 | +Add ```android:usesCleartextTraffic="true"``` in the ```<application>``` in the AndroidManifest.xml |
| 10 | +of the Android app used for testing. It's best to put this in a debug or androidTest |
| 11 | +AndroidManifest.xml so that you don't ship it to end users. (See the example app of this package.) |
| 12 | + |
| 13 | +Add dependencies to your build.gradle: |
| 14 | + |
| 15 | +```groovy |
| 16 | +dependencies { |
| 17 | + testImplementation 'junit:junit:4.12' |
| 18 | + testImplementation "com.google.truth:truth:1.0" |
| 19 | + androidTestImplementation 'androidx.test:runner:1.1.1' |
| 20 | + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' |
| 21 | +
|
| 22 | + // Core library |
| 23 | + api 'androidx.test:core:1.2.0' |
| 24 | +
|
| 25 | + // AndroidJUnitRunner and JUnit Rules |
| 26 | + androidTestImplementation 'androidx.test:runner:1.1.0' |
| 27 | + androidTestImplementation 'androidx.test:rules:1.1.0' |
| 28 | +
|
| 29 | + // Assertions |
| 30 | + androidTestImplementation 'androidx.test.ext:junit:1.0.0' |
| 31 | + androidTestImplementation 'androidx.test.ext:truth:1.0.0' |
| 32 | + androidTestImplementation 'com.google.truth:truth:0.42' |
| 33 | +
|
| 34 | + // Espresso dependencies |
| 35 | + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' |
| 36 | + androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0' |
| 37 | + androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0' |
| 38 | + androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0' |
| 39 | + androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0' |
| 40 | + androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0' |
| 41 | +
|
| 42 | + // The following Espresso dependency can be either "implementation" |
| 43 | + // or "androidTestImplementation", depending on whether you want the |
| 44 | + // dependency to appear on your APK's compile classpath or the test APK |
| 45 | + // classpath. |
| 46 | + androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.1.0' |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Create an `android/app/src/androidTest` folder and put a test file in a package-appropriate subfolder, e.g. `android/app/src/androidTest/java/com/example/MainActivityTest.java`: |
| 51 | + |
| 52 | +```java |
| 53 | +package com.example.espresso_example; |
| 54 | + |
| 55 | +import static androidx.test.espresso.flutter.EspressoFlutter.onFlutterWidget; |
| 56 | +import static androidx.test.espresso.flutter.action.FlutterActions.click; |
| 57 | +import static androidx.test.espresso.flutter.action.FlutterActions.syntheticClick; |
| 58 | +import static androidx.test.espresso.flutter.assertion.FlutterAssertions.matches; |
| 59 | +import static androidx.test.espresso.flutter.matcher.FlutterMatchers.isDescendantOf; |
| 60 | +import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withText; |
| 61 | +import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withTooltip; |
| 62 | +import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withType; |
| 63 | +import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withValueKey; |
| 64 | +import static com.google.common.truth.Truth.assertThat; |
| 65 | +import static org.junit.Assert.fail; |
| 66 | + |
| 67 | +import androidx.test.core.app.ActivityScenario; |
| 68 | +import androidx.test.espresso.flutter.EspressoFlutter.WidgetInteraction; |
| 69 | +import androidx.test.espresso.flutter.assertion.FlutterAssertions; |
| 70 | +import androidx.test.espresso.flutter.matcher.FlutterMatchers; |
| 71 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 72 | +import org.junit.Before; |
| 73 | +import org.junit.Test; |
| 74 | +import org.junit.runner.RunWith; |
| 75 | + |
| 76 | +/** Unit tests for {@link EspressoFlutter}. */ |
| 77 | +@RunWith(AndroidJUnit4.class) |
| 78 | +public class MainActivityTest { |
| 79 | + |
| 80 | + @Before |
| 81 | + public void setUp() throws Exception { |
| 82 | + ActivityScenario.launch(MainActivity.class); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void performClick() { |
| 87 | + onFlutterWidget(withTooltip("Increment")).perform(click()); |
| 88 | + onFlutterWidget(withValueKey("CountText")).check(matches(withText("Button tapped 1 time."))); |
| 89 | + } |
| 90 | + ``` |
| 91 | + |
| 92 | +You'll need to create a test app that enables the Flutter driver extension. |
| 93 | +You can put this in your test_driver/ folder, e.g. test_driver/example.dart. |
| 94 | +
|
| 95 | +```dart |
| 96 | +import 'package:flutter_driver/driver_extension.dart'; |
| 97 | +import '../lib/main.dart' as app; |
| 98 | +
|
| 99 | +void main() { |
| 100 | + enableFlutterDriverExtension(); |
| 101 | + app.main(); |
| 102 | +} |
| 103 | +``` |
| 104 | +
|
| 105 | +The following command line command runs the test locally: |
| 106 | +
|
| 107 | +``` |
| 108 | +./gradlew app:connectedAndroidTest -Ptarget=`pwd`/../test_driver/example.dart |
| 109 | +``` |
| 110 | +
|
| 111 | +Espresso tests can also be run on [Firebase Test Lab](https://firebase.google.com/docs/test-lab): |
| 112 | +
|
| 113 | +``` |
| 114 | +./gradlew app:assembleAndroidTest |
| 115 | +./gradlew app:assembleDebug -Ptarget=<path_to_test>.dart |
| 116 | +gcloud auth activate-service-account --key-file=<PATH_TO_KEY_FILE> |
| 117 | +gcloud --quiet config set project <PROJECT_NAME> |
| 118 | +gcloud firebase test android run --type instrumentation \ |
| 119 | + --app build/app/outputs/apk/debug/app-debug.apk \ |
| 120 | + --test build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk\ |
| 121 | + --timeout 2m \ |
| 122 | + --results-bucket=<RESULTS_BUCKET> \ |
| 123 | + --results-dir=<RESULTS_DIRECTORY> |
| 124 | +``` |
| 125 | +
|
0 commit comments