Skip to content

Commit 3242e89

Browse files
committed
Record e2e tests and screenshot failures
1 parent a8e5a6d commit 3242e89

10 files changed

Lines changed: 123 additions & 25 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,13 @@ jobs:
124124
docker compose logs --no-color > logs/docker-compose.txt 2>&1 || true
125125
docker compose cp humhub:/data/logs logs/humhub || true
126126
127+
# Always uploaded while the suite is being stabilised: includes a screen
128+
# recording of every test file and screenshots of failed tests.
127129
- name: Upload logs
128-
if: failure()
130+
if: always()
129131
uses: actions/upload-artifact@v4
130132
with:
131133
name: e2e-android-logs
132134
path: scripts/e2e/logs
133135
if-no-files-found: ignore
136+
retention-days: 7

‎integration_test/app_launch_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import 'package:flutter_test/flutter_test.dart';
22
import 'package:integration_test/integration_test.dart';
33

44
import 'support/app_harness.dart';
5+
import 'support/e2e_test.dart';
56

67
void main() {
78
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
89

9-
testWidgets('app boots to the opener', (tester) async {
10+
e2eTest('app boots to the opener', (tester) async {
1011
await AppHarness.boot(tester);
1112

1213
expect(find.byKey(openerUrlField), findsOneWidget);

‎integration_test/deep_link_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import 'package:integration_test/integration_test.dart';
44

55
import 'support/app_harness.dart';
66
import 'support/e2e_env.dart';
7+
import 'support/e2e_test.dart';
78

89
/// OS-level App Link delivery can't be triggered from a test; app_links'
910
/// initial and streamed links both end in IntentPluginState.tryNavigateWithOpener.
1011
void main() {
1112
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1213

13-
testWidgets('a deep link opens its URL in the WebView', (tester) async {
14+
e2eTest('a deep link opens its URL in the WebView', (tester) async {
1415
final harness = await AppHarness.boot(tester);
1516

1617
final intentPlugin = tester.state<IntentPluginState>(find.byType(IntentPlugin));

‎integration_test/file_upload_test.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import 'dart:io';
22

3-
import 'package:flutter_test/flutter_test.dart';
43
import 'package:humhub/util/intent/intent_state.dart';
54
import 'package:integration_test/integration_test.dart';
65
import 'package:listen_sharing_intent/listen_sharing_intent.dart';
76
import 'package:path_provider/path_provider.dart';
87

98
import 'support/app_harness.dart';
109
import 'support/e2e_env.dart';
10+
import 'support/e2e_test.dart';
1111

1212
/// Sharing a file into the app uploads it through the logged-in WebView.
1313
/// scripts/e2e/run_android.sh checks afterwards that it reached the backend.
1414
void main() {
1515
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1616

17-
testWidgets('a shared file is uploaded to the instance', (tester) async {
17+
e2eTest('a shared file is uploaded to the instance', (tester) async {
1818
final harness = await AppHarness.boot(tester);
1919
await harness.connectAndLogin(tester);
2020

‎integration_test/login_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import 'package:integration_test/integration_test.dart';
33

44
import 'support/app_harness.dart';
55
import 'support/e2e_env.dart';
6+
import 'support/e2e_test.dart';
67

78
void main() {
89
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
910

10-
testWidgets('opener connects to the instance and the user can log in', (tester) async {
11+
e2eTest('opener connects to the instance and the user can log in', (tester) async {
1112
final harness = await AppHarness.boot(tester);
1213

1314
await harness.connect(tester);

‎integration_test/push_notification_test.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import 'package:flutter_test/flutter_test.dart';
21
import 'package:humhub/util/notifications/channel.dart';
32
import 'package:integration_test/integration_test.dart';
43

54
import 'support/app_harness.dart';
65
import 'support/e2e_env.dart';
6+
import 'support/e2e_test.dart';
77

88
/// Real FCM delivery can't be triggered from a test; onMessageOpenedApp and
99
/// local notification taps both end in NotificationChannel.onTap.
1010
void main() {
1111
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1212

13-
testWidgets('tapping a notification opens its URL in the WebView', (tester) async {
13+
e2eTest('tapping a notification opens its URL in the WebView', (tester) async {
1414
final harness = await AppHarness.boot(tester);
1515

1616
await const NotificationChannel().onTap(E2eEnv.deepLinkTargetUrl);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
3+
/// Marker scripts/e2e/run_android.sh watches for in logcat to take a screenshot.
4+
const _screenshotMarker = 'E2E_SCREENSHOT:';
5+
6+
/// [testWidgets] that requests a screenshot of the device when [body] fails.
7+
///
8+
/// The screenshot is taken on the host with `adb screencap`, so it includes the
9+
/// WebView. The failing state is kept on screen for a moment before rethrowing.
10+
void e2eTest(String description, WidgetTesterCallback body) {
11+
testWidgets(description, (tester) async {
12+
try {
13+
await body(tester);
14+
} catch (_) {
15+
// ignore: avoid_print
16+
print('$_screenshotMarker${_slug(description)}');
17+
final until = DateTime.now().add(const Duration(seconds: 3));
18+
while (DateTime.now().isBefore(until)) {
19+
await tester.pump(const Duration(milliseconds: 250));
20+
}
21+
rethrow;
22+
}
23+
});
24+
}
25+
26+
String _slug(String description) =>
27+
description.toLowerCase().replaceAll(RegExp(r'[^a-z0-9]+'), '-').replaceAll(RegExp(r'^-|-$'), '');

‎integration_test/trusted_navigation_test.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import 'package:integration_test/integration_test.dart';
44

55
import 'support/app_harness.dart';
66
import 'support/e2e_env.dart';
7+
import 'support/e2e_test.dart';
78

89
/// humhub/app#368: posts with external (YouTube) links must not pull the
910
/// main WebView away from the HumHub instance.
1011
void main() {
1112
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1213

13-
testWidgets('a post with an external link keeps the WebView on the instance', (tester) async {
14+
e2eTest('a post with an external link keeps the WebView on the instance', (tester) async {
1415
final harness = await AppHarness.boot(tester);
1516
await harness.connectAndLogin(tester);
1617

‎scripts/e2e/README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ scripts/e2e/down.sh # stop and delete all data
1919
- Backend: http://localhost:8080, login `e2etester` / `E2e-Pass-1`.
2020
- The device reaches it at `http://10.0.2.2:8080` (the emulator's alias for
2121
the host); that is also HumHub's configured base URL.
22+
- Each test file is recorded to `logs/videos/<test>.mp4`, starting when the
23+
app launches. A failing test also leaves a screenshot of the moment it
24+
failed in `logs/screenshots/`, named after the test's description (via
25+
`integration_test/support/e2e_test.dart`). In CI both are in the
26+
`e2e-android-logs` artifact, kept for 7 days.
2227
- Pick a HumHub version with `HUMHUB_IMAGE_TAG=1.19 scripts/e2e/up.sh`
2328
(run `down.sh` first when switching).
2429
- The image is amd64-only; on Apple Silicon it runs under emulation.
@@ -30,7 +35,8 @@ scripts/e2e/down.sh # stop and delete all data
3035
commands create the fixture space/post (`e2e/seed`) and verify the uploaded
3136
file (`e2e/seed/assert-upload`).
3237
- `up.sh` – unattended install, test user, fixtures → `.e2e_fixtures.env`.
33-
- `run_android.sh` – passes the fixtures to the tests via `--dart-define`,
38+
- `run_android.sh` – runs each test file separately with a screen recording,
39+
passes the fixtures to the tests via `--dart-define`,
3440
grants runtime permissions in the background (`grant_permissions.sh`) so
3541
the app's startup permission request doesn't block, writes `logs/logcat.txt`,
3642
then checks the upload reached the backend.

‎scripts/e2e/run_android.sh‎

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
# started by up.sh. Used both locally and in CI (.github/workflows/e2e-tests.yml).
44
#
55
# Env:
6-
# DEVICE adb serial of the emulator (default: emulator-5554)
6+
# DEVICE adb serial of the emulator (default: emulator-5554)
7+
# PACKAGE application id of the app (default: com.humhub.app)
8+
#
9+
# Every test file gets its own screen recording in logs/videos/, and a failing
10+
# test a screenshot in logs/screenshots/ (see integration_test/support/e2e_test.dart).
711
set -euo pipefail
812

913
script_dir="$(cd "$(dirname "$0")" && pwd)"
1014
repo_root="$(cd "$script_dir/../.." && pwd)"
1115
fixtures_file="$script_dir/.e2e_fixtures.env"
1216
export DEVICE="${DEVICE:-emulator-5554}"
17+
export PACKAGE="${PACKAGE:-com.humhub.app}"
1318
upload_file_name="e2e_upload.txt"
1419

1520
if [ ! -f "$fixtures_file" ]; then
@@ -30,23 +35,76 @@ if [ ! -f android/app/google-services.json ]; then
3035
fi
3136

3237
logs_dir="$script_dir/logs"
33-
mkdir -p "$logs_dir"
38+
videos_dir="$logs_dir/videos"
39+
screenshots_dir="$logs_dir/screenshots"
40+
rm -rf "$videos_dir" "$screenshots_dir"
41+
mkdir -p "$videos_dir" "$screenshots_dir"
3442
adb -s "$DEVICE" logcat -c
3543
adb -s "$DEVICE" logcat > "$logs_dir/logcat.txt" 2>&1 &
36-
logcat_pid=$!
3744

3845
"$script_dir/grant_permissions.sh" &
39-
grant_pid=$!
40-
trap 'kill "$grant_pid" "$logcat_pid" 2>/dev/null || true' EXIT
41-
42-
flutter test integration_test \
43-
-d "$DEVICE" \
44-
--dart-define=E2E_SERVER_URL="$E2E_SERVER_URL" \
45-
--dart-define=E2E_USERNAME="$E2E_USERNAME" \
46-
--dart-define=E2E_PASSWORD="$E2E_PASSWORD" \
47-
--dart-define=E2E_EXTERNAL_LINK_POST_URL="$E2E_EXTERNAL_LINK_POST_URL" \
48-
--dart-define=E2E_DEEP_LINK_TARGET_URL="$E2E_DEEP_LINK_TARGET_URL" \
49-
--dart-define=E2E_UPLOAD_FILE_NAME="$upload_file_name"
46+
47+
# Takes a screenshot when a failing test asks for one. The test's own output is
48+
# only printed once it has finished, so watch logcat instead.
49+
adb -s "$DEVICE" logcat -T 1 -v raw -s flutter | while IFS= read -r line; do
50+
case "$line" in
51+
*E2E_SCREENSHOT:*)
52+
name="${line##*E2E_SCREENSHOT:}"
53+
name="${name//[^a-z0-9-]/}"
54+
adb -s "$DEVICE" exec-out screencap -p > "$screenshots_dir/$name.png" < /dev/null
55+
echo "Screenshot of failed test: $screenshots_dir/$name.png"
56+
;;
57+
esac
58+
done &
59+
60+
trap 'kill $(jobs -p) 2>/dev/null || true' EXIT
61+
62+
# Starts recording once the app is running, so the build is not recorded:
63+
# screenrecord stops after 3 minutes and the first build alone takes longer.
64+
start_recording() {
65+
(
66+
until adb -s "$DEVICE" shell pidof "$PACKAGE" > /dev/null 2>&1; do sleep 0.5; done
67+
adb -s "$DEVICE" shell screenrecord --bit-rate 4000000 "/sdcard/e2e_$1.mp4"
68+
) &
69+
recorder_pid=$!
70+
}
71+
72+
stop_recording() {
73+
local video="/sdcard/e2e_$1.mp4"
74+
adb -s "$DEVICE" shell pkill -INT screenrecord > /dev/null 2>&1 || true
75+
# Let screenrecord finish the file; the helper may also still be waiting for
76+
# an app that never started.
77+
for _ in $(seq 20); do
78+
kill -0 "$recorder_pid" 2> /dev/null || break
79+
sleep 0.25
80+
done
81+
pkill -P "$recorder_pid" 2> /dev/null || true
82+
kill "$recorder_pid" 2> /dev/null || true
83+
sleep 1
84+
if adb -s "$DEVICE" shell ls "$video" > /dev/null 2>&1; then
85+
adb -s "$DEVICE" pull "$video" "$videos_dir/$1.mp4" > /dev/null
86+
adb -s "$DEVICE" shell rm -f "$video"
87+
fi
88+
}
89+
90+
status=0
91+
for test_file in integration_test/*_test.dart; do
92+
name="$(basename "$test_file" .dart)"
93+
# A leftover app process would start the recording before the build.
94+
adb -s "$DEVICE" shell am force-stop "$PACKAGE" > /dev/null 2>&1 || true
95+
start_recording "$name"
96+
flutter test "$test_file" \
97+
-d "$DEVICE" \
98+
--dart-define=E2E_SERVER_URL="$E2E_SERVER_URL" \
99+
--dart-define=E2E_USERNAME="$E2E_USERNAME" \
100+
--dart-define=E2E_PASSWORD="$E2E_PASSWORD" \
101+
--dart-define=E2E_EXTERNAL_LINK_POST_URL="$E2E_EXTERNAL_LINK_POST_URL" \
102+
--dart-define=E2E_DEEP_LINK_TARGET_URL="$E2E_DEEP_LINK_TARGET_URL" \
103+
--dart-define=E2E_UPLOAD_FILE_NAME="$upload_file_name" || status=1
104+
stop_recording "$name"
105+
done
50106

51107
echo "Checking the shared file reached the backend..."
52-
(cd "$script_dir" && docker compose exec -T humhub /app/yii e2e/seed/assert-upload "$E2E_USERNAME" "$upload_file_name")
108+
(cd "$script_dir" && docker compose exec -T humhub /app/yii e2e/seed/assert-upload "$E2E_USERNAME" "$upload_file_name") || status=1
109+
110+
exit "$status"

0 commit comments

Comments
 (0)