|
1 | 1 | # e2e (deprecated) |
2 | 2 |
|
3 | | -## DEPRECATED |
4 | | - |
5 | 3 | This package has been moved to [integration_test](https://github.com/flutter/plugins/tree/master/packages/integration_test). |
6 | | - |
7 | | -## Old instructions |
8 | | - |
9 | | -This package enables self-driving testing of Flutter code on devices and emulators. |
10 | | -It adapts flutter_test results into a format that is compatible with `flutter drive` |
11 | | -and native Android instrumentation testing. |
12 | | - |
13 | | -## Usage |
14 | | - |
15 | | -Add a dependency on the `e2e` package in the |
16 | | -`dev_dependencies` section of pubspec.yaml. For plugins, do this in the |
17 | | -pubspec.yaml of the example app. |
18 | | - |
19 | | -Invoke `E2EWidgetsFlutterBinding.ensureInitialized()` at the start |
20 | | -of a test file, e.g. |
21 | | - |
22 | | -```dart |
23 | | -import 'package:e2e/e2e.dart'; |
24 | | -
|
25 | | -void main() { |
26 | | - E2EWidgetsFlutterBinding.ensureInitialized(); |
27 | | - testWidgets("failing test example", (WidgetTester tester) async { |
28 | | - expect(2 + 2, equals(5)); |
29 | | - }); |
30 | | -} |
31 | | -``` |
32 | | - |
33 | | -## Test locations |
34 | | - |
35 | | -It is recommended to put e2e tests in the `test/` folder of the app or package. |
36 | | -For example apps, if the e2e test references example app code, it should go in |
37 | | -`example/test/`. It is also acceptable to put e2e tests in `test_driver/` folder |
38 | | -so that they're alongside the runner app (see below). |
39 | | - |
40 | | -## Using Flutter driver to run tests |
41 | | - |
42 | | -`E2EWidgetsTestBinding` supports launching the on-device tests with `flutter drive`. |
43 | | -Note that the tests don't use the `FlutterDriver` API, they use `testWidgets` instead. |
44 | | - |
45 | | -Put the a file named `<package_name>_e2e_test.dart` in the app' `test_driver` directory: |
46 | | - |
47 | | -```dart |
48 | | -import 'dart:async'; |
49 | | -
|
50 | | -import 'package:e2e/e2e_driver.dart' as e2e; |
51 | | -
|
52 | | -Future<void> main() async => e2e.main(); |
53 | | -
|
54 | | -``` |
55 | | - |
56 | | -To run a example app test with Flutter driver: |
57 | | - |
58 | | -``` |
59 | | -cd example |
60 | | -flutter drive test/<package_name>_e2e.dart |
61 | | -``` |
62 | | - |
63 | | -To test plugin APIs using Flutter driver: |
64 | | - |
65 | | -``` |
66 | | -cd example |
67 | | -flutter drive --driver=test_driver/<package_name>_test.dart test/<package_name>_e2e.dart |
68 | | -``` |
69 | | - |
70 | | -You can run tests on web in release or profile mode. |
71 | | - |
72 | | -First you need to make sure you have downloaded the driver for the browser. |
73 | | - |
74 | | -``` |
75 | | -cd example |
76 | | -flutter drive -v --target=test_driver/<package_name>dart -d web-server --release --browser-name=chrome |
77 | | -``` |
78 | | - |
79 | | -## Android device testing |
80 | | - |
81 | | -Create an instrumentation test file in your application's |
82 | | -**android/app/src/androidTest/java/com/example/myapp/** directory (replacing |
83 | | -com, example, and myapp with values from your app's package name). You can name |
84 | | -this test file MainActivityTest.java or another name of your choice. |
85 | | - |
86 | | -```java |
87 | | -package com.example.myapp; |
88 | | - |
89 | | -import androidx.test.rule.ActivityTestRule; |
90 | | -import dev.flutter.plugins.e2e.FlutterTestRunner; |
91 | | -import org.junit.Rule; |
92 | | -import org.junit.runner.RunWith; |
93 | | - |
94 | | -@RunWith(FlutterTestRunner.class) |
95 | | -public class MainActivityTest { |
96 | | - @Rule |
97 | | - public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class, true, false); |
98 | | -} |
99 | | -``` |
100 | | - |
101 | | -Update your application's **myapp/android/app/build.gradle** to make sure it |
102 | | -uses androidx's version of AndroidJUnitRunner and has androidx libraries as a |
103 | | -dependency. |
104 | | - |
105 | | -``` |
106 | | -android { |
107 | | - ... |
108 | | - defaultConfig { |
109 | | - ... |
110 | | - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" |
111 | | - } |
112 | | -} |
113 | | -
|
114 | | -dependencies { |
115 | | - testImplementation 'junit:junit:4.12' |
116 | | -
|
117 | | - // https://developer.android.com/jetpack/androidx/releases/test/#1.2.0 |
118 | | - androidTestImplementation 'androidx.test:runner:1.2.0' |
119 | | - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' |
120 | | -} |
121 | | -``` |
122 | | - |
123 | | -To e2e test on a local Android device (emulated or physical): |
124 | | - |
125 | | -``` |
126 | | -./gradlew app:connectedAndroidTest -Ptarget=`pwd`/../test_driver/<package_name>_e2e.dart |
127 | | -``` |
128 | | - |
129 | | -## Firebase Test Lab |
130 | | - |
131 | | -If this is your first time testing with Firebase Test Lab, you'll need to follow |
132 | | -the guides in the [Firebase test lab |
133 | | -documentation](https://firebase.google.com/docs/test-lab/?gclid=EAIaIQobChMIs5qVwqW25QIV8iCtBh3DrwyUEAAYASAAEgLFU_D_BwE) |
134 | | -to set up a project. |
135 | | - |
136 | | -To run an e2e test on Android devices using Firebase Test Lab, use gradle commands to build an |
137 | | -instrumentation test for Android, after creating `androidTest` as suggested in the last section. |
138 | | - |
139 | | -```bash |
140 | | -pushd android |
141 | | -# flutter build generates files in android/ for building the app |
142 | | -flutter build apk |
143 | | -./gradlew app:assembleAndroidTest |
144 | | -./gradlew app:assembleDebug -Ptarget=<path_to_test>.dart |
145 | | -popd |
146 | | -``` |
147 | | - |
148 | | -Upload the build apks Firebase Test Lab, making sure to replace <PATH_TO_KEY_FILE>, |
149 | | -<PROJECT_NAME>, <RESULTS_BUCKET>, and <RESULTS_DIRECTORY> with your values. |
150 | | - |
151 | | -```bash |
152 | | -gcloud auth activate-service-account --key-file=<PATH_TO_KEY_FILE> |
153 | | -gcloud --quiet config set project <PROJECT_NAME> |
154 | | -gcloud firebase test android run --type instrumentation \ |
155 | | - --app build/app/outputs/apk/debug/app-debug.apk \ |
156 | | - --test build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk\ |
157 | | - --timeout 2m \ |
158 | | - --results-bucket=<RESULTS_BUCKET> \ |
159 | | - --results-dir=<RESULTS_DIRECTORY> |
160 | | -``` |
161 | | - |
162 | | -You can pass additional parameters on the command line, such as the |
163 | | -devices you want to test on. See |
164 | | -[gcloud firebase test android run](https://cloud.google.com/sdk/gcloud/reference/firebase/test/android/run). |
165 | | - |
166 | | -## iOS device testing |
167 | | - |
168 | | -You need to change `iOS/Podfile` to avoid test target statically linking to the plugins. One way is to |
169 | | -link all of the plugins dynamically: |
170 | | - |
171 | | -``` |
172 | | -target 'Runner' do |
173 | | - use_frameworks! |
174 | | - ... |
175 | | -end |
176 | | -``` |
177 | | - |
178 | | -To e2e test on your iOS device (simulator or real), rebuild your iOS targets with Flutter tool. |
179 | | - |
180 | | -``` |
181 | | -flutter build ios -t test_driver/<package_name>_e2e.dart (--simulator) |
182 | | -``` |
183 | | - |
184 | | -Open Xcode project (by default, it's `ios/Runner.xcodeproj`). Create a test target |
185 | | -(navigating `File > New > Target...` and set up the values) and a test file `RunnerTests.m` and |
186 | | -change the code. You can change `RunnerTests.m` to the name of your choice. |
187 | | - |
188 | | -```objective-c |
189 | | -#import <XCTest/XCTest.h> |
190 | | -#import <e2e/E2EIosTest.h> |
191 | | - |
192 | | -E2E_IOS_RUNNER(RunnerTests) |
193 | | -``` |
194 | | -
|
195 | | -Now you can start RunnerTests to kick out e2e tests! |
0 commit comments