Skip to content

Commit d32299c

Browse files
authored
Merge pull request #140 from sakusaku3939/add-webview-testing
Add a CI pipeline for WebView screen screenshot testing
2 parents e1dbfe6 + 7f44e3d commit d32299c

11 files changed

Lines changed: 356 additions & 28 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
name: Compare WebView Screenshots
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 0 * * 0"
7+
workflow_run:
8+
workflows: [ "Generate WebView Screenshots" ]
9+
types:
10+
- completed
11+
12+
permissions:
13+
contents: read
14+
pages: write
15+
id-token: write
16+
17+
jobs:
18+
compare-screenshots:
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
25+
- name: Set up JDK 17
26+
uses: actions/setup-java@v4
27+
with:
28+
java-version: '17'
29+
distribution: 'temurin'
30+
31+
- name: Setup Android SDK
32+
uses: android-actions/setup-android@v3
33+
34+
- name: Accept Android SDK licenses
35+
run: yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses
36+
37+
- name: Cache Gradle dependencies
38+
uses: actions/cache@v3
39+
with:
40+
path: |
41+
~/.gradle/caches
42+
~/.gradle/wrapper
43+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
44+
restore-keys: |
45+
${{ runner.os }}-gradle-
46+
47+
- name: Install Android SDK components
48+
run: |
49+
$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "platform-tools" "platforms;android-35" "build-tools;35.0.0"
50+
51+
- name: Enable KVM group perms
52+
run: |
53+
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
54+
sudo udevadm control --reload-rules
55+
sudo udevadm trigger --name-match=kvm
56+
57+
- name: AVD cache
58+
uses: actions/cache@v3
59+
id: avd-cache
60+
with:
61+
path: |
62+
~/.android/avd/*
63+
~/.android/adb*
64+
key: avd-35-x86_64
65+
66+
- name: Create AVD and generate snapshot for caching
67+
if: steps.avd-cache.outputs.cache-hit != 'true'
68+
uses: reactivecircus/android-emulator-runner@v2
69+
with:
70+
api-level: 35
71+
arch: x86_64
72+
target: google_apis
73+
force-avd-creation: false
74+
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
75+
disable-animations: false
76+
script: echo "Generated AVD snapshot for caching."
77+
78+
- name: Run screenshot tests
79+
uses: reactivecircus/android-emulator-runner@v2
80+
with:
81+
api-level: 35
82+
arch: x86_64
83+
target: google_apis
84+
force-avd-creation: false
85+
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
86+
disable-animations: false
87+
script: |
88+
./gradlew executeScreenshotTests || true
89+
90+
- name: Prepare report for GitHub Pages
91+
run: |
92+
mkdir -p public
93+
if [ -d "app/build/reports/shot/debug/verification" ]; then
94+
cp -r app/build/reports/shot/debug/verification/* public/
95+
else
96+
echo "<h1>No screenshot report found</h1>" > public/index.html
97+
fi
98+
- name: Upload artifact for Pages
99+
uses: actions/upload-pages-artifact@v3
100+
with:
101+
path: ./public
102+
103+
- name: Upload comparison results as artifact
104+
uses: actions/upload-artifact@v4
105+
if: always()
106+
with:
107+
name: screenshot-comparison-${{ github.run_id }}
108+
path: |
109+
app/build/reports/shot/debug/verification/
110+
public/
111+
retention-days: 30
112+
113+
deploy:
114+
needs: compare-screenshots
115+
runs-on: ubuntu-latest
116+
environment:
117+
name: github-pages
118+
url: ${{ steps.deployment.outputs.page_url }}
119+
120+
permissions:
121+
pages: write
122+
id-token: write
123+
124+
steps:
125+
- name: Deploy to GitHub Pages
126+
id: deployment
127+
uses: actions/deploy-pages@v4
128+
129+
check-result:
130+
needs: [ compare-screenshots, deploy ]
131+
runs-on: ubuntu-latest
132+
steps:
133+
- name: Download comparison artifact
134+
uses: actions/download-artifact@v4
135+
with:
136+
name: screenshot-comparison-${{ github.run_id }}
137+
138+
- name: Fail job if diff found
139+
run: |
140+
REPORT=$(find . -type f -name "TEST-Shot.xml" | head -n 1)
141+
if [ -z "$REPORT" ]; then
142+
echo "❌ TEST-Shot.xml not found"
143+
exit 1
144+
fi
145+
if grep -q "<failure" "$REPORT"; then
146+
echo "❌ Screenshot diff detected"
147+
exit 1
148+
else
149+
echo "✅ No screenshot diff detected"
150+
fi
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Generate WebView Screenshots
2+
3+
on:
4+
workflow_dispatch:
5+
6+
permissions:
7+
contents: write
8+
9+
jobs:
10+
generate-screenshots:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
with:
17+
persist-credentials: true
18+
19+
- name: Set up JDK 17
20+
uses: actions/setup-java@v4
21+
with:
22+
java-version: '17'
23+
distribution: 'temurin'
24+
25+
- name: Setup Android SDK
26+
uses: android-actions/setup-android@v3
27+
28+
- name: Accept Android SDK licenses
29+
run: yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses
30+
31+
- name: Cache Gradle dependencies
32+
uses: actions/cache@v3
33+
with:
34+
path: |
35+
~/.gradle/caches
36+
~/.gradle/wrapper
37+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
38+
restore-keys: |
39+
${{ runner.os }}-gradle-
40+
41+
- name: Install Android SDK components
42+
run: |
43+
$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "platform-tools" "platforms;android-35" "build-tools;35.0.0"
44+
45+
- name: Enable KVM group perms
46+
run: |
47+
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
48+
sudo udevadm control --reload-rules
49+
sudo udevadm trigger --name-match=kvm
50+
51+
- name: AVD cache
52+
uses: actions/cache@v3
53+
id: avd-cache
54+
with:
55+
path: |
56+
~/.android/avd/*
57+
~/.android/adb*
58+
key: avd-35-x86_64
59+
60+
- name: Create AVD and generate snapshot for caching
61+
if: steps.avd-cache.outputs.cache-hit != 'true'
62+
uses: reactivecircus/android-emulator-runner@v2
63+
with:
64+
api-level: 35
65+
arch: x86_64
66+
target: google_apis
67+
force-avd-creation: false
68+
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
69+
disable-animations: false
70+
script: echo "Generated AVD snapshot for caching."
71+
72+
- name: Generate and record screenshots
73+
uses: reactivecircus/android-emulator-runner@v2
74+
with:
75+
api-level: 35
76+
arch: x86_64
77+
target: google_apis
78+
force-avd-creation: false
79+
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
80+
disable-animations: false
81+
script: |
82+
./gradlew executeScreenshotTests -Precord
83+
84+
- name: Commit and push new screenshots
85+
run: |
86+
git config --local user.email "action@github.com"
87+
git config --local user.name "GitHub Action"
88+
git add app/screenshots/
89+
git add **/screenshots/
90+
if git diff --staged --quiet; then
91+
echo "No screenshot changes to commit"
92+
else
93+
git commit -m "Update screenshots"
94+
git push
95+
fi
96+
97+
- name: Upload screenshots
98+
uses: actions/upload-artifact@v4
99+
if: always()
100+
with:
101+
name: screenshots-${{ github.run_id }}
102+
path: |
103+
app/screenshots/
104+
**/screenshots/
105+
retention-days: 30

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
.externalNativeBuild
1414
/.kotlin
1515
.cxx
16-
.idea
16+
.idea

app/build.gradle.kts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
alias(libs.plugins.android.application)
33
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.shot)
45
}
56

67
android {
@@ -15,6 +16,7 @@ android {
1516
versionName = libs.versions.versionName.get()
1617

1718
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
19+
testInstrumentationRunner = "com.karumi.shot.ShotTestRunner"
1820

1921
vectorDrawables {
2022
useSupportLibrary = true
@@ -23,8 +25,8 @@ android {
2325

2426
buildTypes {
2527
debug {
26-
isMinifyEnabled = true
27-
isShrinkResources = true
28+
isMinifyEnabled = false
29+
isShrinkResources = false
2830
proguardFiles(
2931
getDefaultProguardFile("proguard-android-optimize.txt"),
3032
"proguard-rules.pro"
@@ -60,6 +62,11 @@ android {
6062
}
6163
}
6264

65+
shot {
66+
applicationId = "com.example.deeplviewer"
67+
tolerance = 0.1
68+
}
69+
6370
dependencies {
6471
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
6572
implementation(libs.kotlin.stdlib)
@@ -77,4 +84,6 @@ dependencies {
7784
androidTestImplementation(libs.androidx.test.espresso.core)
7885
androidTestImplementation(libs.androidx.test.runner)
7986
androidTestImplementation(libs.androidx.test.rules)
87+
androidTestImplementation(libs.shot.android)
88+
testImplementation(kotlin("test"))
8089
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.deeplviewer.test"
4+
android:sharedUserId="com.example.deeplviewer.uid">
5+
6+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
7+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
8+
9+
</manifest>

app/src/androidTest/java/com/example/deeplviewer/ExampleInstrumentedTest.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)