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).
711set -euo pipefail
812
913script_dir=" $( cd " $( dirname " $0 " ) " && pwd) "
1014repo_root=" $( cd " $script_dir /../.." && pwd) "
1115fixtures_file=" $script_dir /.e2e_fixtures.env"
1216export DEVICE=" ${DEVICE:- emulator-5554} "
17+ export PACKAGE=" ${PACKAGE:- com.humhub.app} "
1318upload_file_name=" e2e_upload.txt"
1419
1520if [ ! -f " $fixtures_file " ]; then
@@ -30,23 +35,76 @@ if [ ! -f android/app/google-services.json ]; then
3035fi
3136
3237logs_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 "
3442adb -s " $DEVICE " logcat -c
3543adb -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
51107echo " 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