Skip to content

Commit 1a92717

Browse files
chore: Describe how to precompile the server into the AUT (#1043)
1 parent 5905c9c commit 1a92717

File tree

4 files changed

+95
-25
lines changed

4 files changed

+95
-25
lines changed

.eslintrc.json

-19
This file was deleted.

.github/dependabot.yml

-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@ updates:
66
interval: daily
77
time: "11:00"
88
open-pull-requests-limit: 10
9-
commit-message:
10-
prefix: "chore"
11-
include: "scope"

README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ On top of standard Appium requirements Espresso driver also expects the followin
3737
- Both the server package and the application under test must be signed with the same digital signature. Appium does sign them automatically upon session creation, so this could only be an issue if one wants to test an application, which is already installed on the device (using `noReset=true` capability).
3838
- The package under test must not have mangled class names (e.g. [Proguard](https://developer.android.com/studio/build/shrink-code) must not be enabled for it)
3939

40+
41+
## Consuming Espresso Server as Library
42+
43+
If you have access to the source code of the application under test then it is
44+
possible to integrate Espresso server into your application and make it to a library.
45+
This approach allows to simplify the dependency conflicts resolution
46+
as well as to optimize the session startup performance.
47+
Read the corresponding [article](./docs/as-library.md) from the driver
48+
documentation for more details.
49+
50+
4051
### Doctor
4152

4253
Since driver version 2.31.0 you can automate the validation for the most of the above
@@ -1773,14 +1784,14 @@ Appium allows to do this on per-process (multiple server processes running on di
17731784
or per-request basis (single server process managing multiple sessions, more preferable, uses less resources and ensures better control over running sessions). Check [Parallel Android Tests](docs/parallel-tests.md) article for
17741785
more details.
17751786

1776-
> **Note**
1787+
> [!NOTE]
17771788
> If you are _not_ going to run your tests in parallel then consider enabling the `--session-override` Appium server argument. It forces the server to close all pending sessions before a new one could be opened,
17781789
> which allows you to avoid possible issues with such sessions silently running/expiring in the background.
17791790
1780-
17811791
## Troubleshooting
17821792

1783-
* If you observe Espresso server crash on startup and various exceptions about missing class/method in the logcat output then consider updating [appium:espressoBuildConfig](#espresso-build-config) capability with module versions that match your application under test. This might require some experimentation, as different apps have different module requirements. Check, for example, [issue #812](https://github.com/appium/appium-espresso-driver/issues/812)
1793+
* If you observe Espresso server crash on startup and various exceptions about missing class/method in the logcat output then consider updating [appium:espressoBuildConfig](#espresso-build-config) capability with module versions that match your application under test. This might require some experimentation, as different apps have different module requirements. Check, for example, [issue #812](https://github.com/appium/appium-espresso-driver/issues/812). Another solution might be
1794+
to [integrate](#consuming-espresso-server-as-library) Espresso Server with the application under test in form of a library.
17841795
* If you experince issues with application activities being not found or not starting then consider checking [How To Troubleshoot Activities Startup](docs/activity-startup.md) article.
17851796
* Espresso requires the debug APK and app-under-test APK (AUT) to have the same signature. It automatically signs the AUT with the `io.appium.espressoserver.test` signature. This may be problematic if you're using an outdated Android SDK tools and/or an outdated Java version.
17861797
* If there are problems starting a session, set the capability `forceEspressoRebuild` to `true` and retry. This will force rebuilding of Espresso Server. If the following session startup is successfull, set it back to `false`, so the session startup performance is back to normal.

docs/as-library.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)