|
| 1 | +## Consuming Espresso Server as Library |
| 2 | + |
| 3 | +After driver version 3.0.0 it is possible to consume Espresso server as a library, |
| 4 | +apply dependency constraints to align dependency versions, |
| 5 | +and embed it either as androidTest component of the app |
| 6 | +or as a standalone test module under the same |
| 7 | +Gradle project. The library only has to expose a single method for starting a |
| 8 | +server and a TestRule for Compose support. |
| 9 | +This way, the Espresso server APK could be built with the correct versions of AGP, |
| 10 | +Kotlin and dependencies automatically. |
| 11 | +If the app is obfuscated, we could also use [slackhq/keeper](https://github.com/slackhq/keeper) |
| 12 | +to infer the obfuscation rules. |
| 13 | + |
| 14 | +### Building The App Under Test (AUT) |
| 15 | + |
| 16 | +Standalone test module: |
| 17 | + |
| 18 | +```groovy |
| 19 | +plugins { |
| 20 | + id "com.android.test" |
| 21 | +} |
| 22 | +
|
| 23 | +android { |
| 24 | + namespace = "com.my.espresso.server" |
| 25 | +
|
| 26 | + defaultConfig { |
| 27 | + testApplicationId = "io.appium.espressoserver.test" |
| 28 | + minSdk = 21 |
| 29 | + targetSdk = 34 |
| 30 | + } |
| 31 | +
|
| 32 | + targetProjectPath = ":app" |
| 33 | +} |
| 34 | +
|
| 35 | +dependencies { |
| 36 | + implementation "androidx.test:runner:1.6.0" |
| 37 | + implementation "io.appium.espressoserver:library:<latest_driver_version>" |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +The test that should reside on the consumer side: |
| 42 | + |
| 43 | +```kotlin |
| 44 | +package com.my.espresso.server |
| 45 | + |
| 46 | +import android.annotation.SuppressLint |
| 47 | +import androidx.test.filters.LargeTest |
| 48 | +import io.appium.espressoserver.lib.http.Server |
| 49 | +import org.junit.Rule |
| 50 | +import org.junit.Test |
| 51 | + |
| 52 | +@LargeTest |
| 53 | +class EspressoServerRunnerTest { |
| 54 | + @get:Rule |
| 55 | + val server = Server() |
| 56 | + |
| 57 | + @Test |
| 58 | + fun startEspressoServer() { |
| 59 | + server.run() |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Build both the app and Espresso server: |
| 65 | + |
| 66 | +```bash |
| 67 | +./gradlew :app:assembleDebug :espresso_server:assembleDebug |
| 68 | +``` |
| 69 | + |
| 70 | +### Running Appium Tests |
| 71 | + |
| 72 | +As soon as the application containing Espresso server is built it might be used |
| 73 | +to run tests with appium-espresso-driver. The only requirements to run a test are |
| 74 | +- The application build according to the above tutorial is already installed on the device |
| 75 | +- The precompiled Espresso Server module version satisfies the driver version: |
| 76 | + - At least major versions should match |
| 77 | + - The driver version must not be older than the server version |
| 78 | + |
| 79 | +Set the [skipServerInstallation](../README.md#driverserver) to |
| 80 | +`true` in your test session capabilities to provide the Espresso driver |
| 81 | +with the hint that Espresso server is already listening on the device under test. |
0 commit comments