Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[espresso] Adds EspressoFlutter as a first-party plugin (#2369)
Browse files Browse the repository at this point in the history
* Initial open source release of Espresso bindings for Flutter as a new first-party plugin, espresso.
  • Loading branch information
collinjackson committed Jan 10, 2020
1 parent c544c0e commit a1a4f21
Show file tree
Hide file tree
Showing 129 changed files with 6,117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/espresso/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.dart_tool/

.packages
.pub/

build/
10 changes: 10 additions & 0 deletions packages/espresso/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 0190e40457d43e17bdfaf046dfa634cbc5bf28b9
channel: unknown

project_type: plugin
3 changes: 3 additions & 0 deletions packages/espresso/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* Initial open-source release of Espresso bindings for Flutter.
27 changes: 27 additions & 0 deletions packages/espresso/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125 changes: 125 additions & 0 deletions packages/espresso/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# espresso

Provides bindings for Espresso tests of Flutter Android apps.

## Installation

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.

Add ```android:usesCleartextTraffic="true"``` in the ```<application>``` in the AndroidManifest.xml
of the Android app used for testing. It's best to put this in a debug or androidTest
AndroidManifest.xml so that you don't ship it to end users. (See the example app of this package.)

Add dependencies to your build.gradle:

```groovy
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation "com.google.truth:truth:1.0"
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
// Core library
api 'androidx.test:core:1.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'
// The following Espresso dependency can be either "implementation"
// or "androidTestImplementation", depending on whether you want the
// dependency to appear on your APK's compile classpath or the test APK
// classpath.
androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.1.0'
}
```

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`:

```java
package com.example.espresso_example;

import static androidx.test.espresso.flutter.EspressoFlutter.onFlutterWidget;
import static androidx.test.espresso.flutter.action.FlutterActions.click;
import static androidx.test.espresso.flutter.action.FlutterActions.syntheticClick;
import static androidx.test.espresso.flutter.assertion.FlutterAssertions.matches;
import static androidx.test.espresso.flutter.matcher.FlutterMatchers.isDescendantOf;
import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withText;
import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withTooltip;
import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withType;
import static androidx.test.espresso.flutter.matcher.FlutterMatchers.withValueKey;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.flutter.EspressoFlutter.WidgetInteraction;
import androidx.test.espresso.flutter.assertion.FlutterAssertions;
import androidx.test.espresso.flutter.matcher.FlutterMatchers;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Unit tests for {@link EspressoFlutter}. */
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

@Before
public void setUp() throws Exception {
ActivityScenario.launch(MainActivity.class);
}

@Test
public void performClick() {
onFlutterWidget(withTooltip("Increment")).perform(click());
onFlutterWidget(withValueKey("CountText")).check(matches(withText("Button tapped 1 time.")));
}
```

You'll need to create a test app that enables the Flutter driver extension.
You can put this in your test_driver/ folder, e.g. test_driver/example.dart.
```dart
import 'package:flutter_driver/driver_extension.dart';
import '../lib/main.dart' as app;
void main() {
enableFlutterDriverExtension();
app.main();
}
```
The following command line command runs the test locally:
```
./gradlew app:connectedAndroidTest -Ptarget=`pwd`/../test_driver/example.dart
```
Espresso tests can also be run on [Firebase Test Lab](https://firebase.google.com/docs/test-lab):
```
./gradlew app:assembleAndroidTest
./gradlew app:assembleDebug -Ptarget=<path_to_test>.dart
gcloud auth activate-service-account --key-file=<PATH_TO_KEY_FILE>
gcloud --quiet config set project <PROJECT_NAME>
gcloud firebase test android run --type instrumentation \
--app build/app/outputs/apk/debug/app-debug.apk \
--test build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk\
--timeout 2m \
--results-bucket=<RESULTS_BUCKET> \
--results-dir=<RESULTS_DIRECTORY>
```
8 changes: 8 additions & 0 deletions packages/espresso/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
74 changes: 74 additions & 0 deletions packages/espresso/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
group 'com.example.espresso'
version '1.0'

buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}

dependencies {
implementation 'com.google.guava:guava:28.1-android'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation 'com.google.code.gson:gson:2.8.6'
androidTestImplementation 'org.hamcrest:hamcrest:2.2'

testImplementation 'junit:junit:4.12'
testImplementation "com.google.truth:truth:1.0"
api 'androidx.test:runner:1.1.1'
api 'androidx.test.espresso:espresso-core:3.1.1'

// Core library
api 'androidx.test:core:1.0.0'

// AndroidJUnitRunner and JUnit Rules
api 'androidx.test:runner:1.1.0'
api 'androidx.test:rules:1.1.0'

// Assertions
api 'androidx.test.ext:junit:1.0.0'
api 'androidx.test.ext:truth:1.0.0'
api 'com.google.truth:truth:0.42'

// Espresso dependencies
api 'androidx.test.espresso:espresso-core:3.1.0'
api 'androidx.test.espresso:espresso-contrib:3.1.0'
api 'androidx.test.espresso:espresso-intents:3.1.0'
api 'androidx.test.espresso:espresso-accessibility:3.1.0'
api 'androidx.test.espresso:espresso-web:3.1.0'
api 'androidx.test.espresso.idling:idling-concurrent:3.1.0'

// The following Espresso dependency can be either "implementation"
// or "androidTestImplementation", depending on whether you want the
// dependency to appear on your APK's compile classpath or the test APK
// classpath.
api 'androidx.test.espresso:espresso-idling-resource:3.1.0'
}


4 changes: 4 additions & 0 deletions packages/espresso/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Tue Nov 26 13:04:21 PST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
1 change: 1 addition & 0 deletions packages/espresso/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'espresso'
3 changes: 3 additions & 0 deletions packages/espresso/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.espresso">
</manifest>
Loading

0 comments on commit a1a4f21

Please sign in to comment.