From 85f73342d042f15bfa78eccd8f5163d820c031d9 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Sat, 17 Dec 2022 10:33:17 +0100 Subject: [PATCH] Espresso tests --- .github/workflows/Android-CI.yml | 56 +++++++++++++-- app/build.gradle | 7 ++ .../info/hannes/github/sample/SmokeTest.kt | 69 +++++++++++++++++++ 3 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 app/src/androidTest/java/info/hannes/github/sample/SmokeTest.kt diff --git a/.github/workflows/Android-CI.yml b/.github/workflows/Android-CI.yml index b6c5abe..4ec4b68 100644 --- a/.github/workflows/Android-CI.yml +++ b/.github/workflows/Android-CI.yml @@ -3,11 +3,32 @@ name: CI Android on: [pull_request] jobs: - build: - - runs-on: ubuntu-latest - + Test: + runs-on: ${{ matrix.os }} + permissions: + contents: write + pull-requests: write + repository-projects: write + strategy: + fail-fast: false + matrix: + os: [ ubuntu-latest ] + api: [ 28 ] + tag: [ default ] + abi: [ x86_64 ] + emulatorApi: [ 9 ] + java_version: [ 17 ] steps: + - name: kvm support + run: | + egrep -c '(vmx|svm)' /proc/cpuinfo + id + sudo adduser $USER kvm + sudo chown -R $USER /dev/kvm + id + - name: prepare + run: | + sudo apt-get update && sudo apt-get install -y exiftool imagemagick xdg-utils libimage-exiftool-perl zsh jq xorg - uses: actions/checkout@v4 - name: set up JDK uses: actions/setup-java@v4 @@ -16,4 +37,29 @@ jobs: java-version: 17 - uses: gradle/wrapper-validation-action@v3 - name: Build with Gradle - run: ./gradlew build + run: ./gradlew assemble + - name: Install Android SDK + uses: hannesa2/action-android/install-sdk@0.1.16.7 + - name: Run instrumentation tests + uses: hannesa2/action-android/emulator-run-cmd@0.1.16.7 + with: + cmd: ./gradlew cAT --continue + api: ${{ matrix.api }} + tag: ${{ matrix.tag }} + abi: ${{ matrix.abi }} + cmdOptions: -noaudio -no-boot-anim -no-window -metrics-collection + - name: Archive screenshots + if: ${{ always() }} + uses: actions/upload-artifact@v3.1.1 + with: + name: Updater-Screenshots + if-no-files-found: error + path: | + app/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected + app/build/outputs/androidTest-results/connected + - name: Archive Espresso results + uses: actions/upload-artifact@v3 + with: + if-no-files-found: error + name: Updater-Espresso-report + path: app/build/reports/androidTests/connected diff --git a/app/build.gradle b/app/build.gradle index 03a7388..9be9156 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -14,6 +14,9 @@ android { targetSdkVersion 34 buildConfigField "String", 'GIT_REPOSITORY', "\"" + getGitOriginRemote() + "\"" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunnerArguments useTestStorageService: 'true' } packagingOptions { resources { @@ -39,6 +42,10 @@ dependencies { implementation "androidx.appcompat:appcompat:1.7.0" implementation 'com.google.android.material:material:1.12.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + + androidTestImplementation "androidx.test.ext:junit-ktx:1.1.4" + androidTestUtil "androidx.test.services:test-services:1.4.2" + androidTestImplementation "androidx.test.espresso:espresso-core:3.5.0" } static def getGitCommitCount() { diff --git a/app/src/androidTest/java/info/hannes/github/sample/SmokeTest.kt b/app/src/androidTest/java/info/hannes/github/sample/SmokeTest.kt new file mode 100644 index 0000000..534fbd4 --- /dev/null +++ b/app/src/androidTest/java/info/hannes/github/sample/SmokeTest.kt @@ -0,0 +1,69 @@ +package info.hannes.github.sample + +import android.util.Log +import androidx.test.core.app.takeScreenshot +import androidx.test.core.graphics.writeToTestStorage +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.matcher.RootMatchers.isDialog +import androidx.test.espresso.matcher.ViewMatchers.* +import androidx.test.espresso.screenshot.captureToBitmap +import androidx.test.ext.junit.rules.activityScenarioRule +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TestName +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class SmokeTest { + + @get:Rule + val activityScenarioRule = activityScenarioRule() + + @get:Rule + var nameRule = TestName() + + @Test + fun smokeTestSimplyStart() { + onView(isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-R") + + onView(withId(R.id.button)).perform(click()) + onView(isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-2") + onView(withText(info.hannes.github.R.string.new_version)) + .inRoot(isDialog()) + .check(matches(isDisplayed())) + onView(isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-3") + } + + @Test + fun updateGithub() { + onView(isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-1") + onView(withId(R.id.button)).perform(click()) + onView(isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-2") + onView(withText(info.hannes.github.R.string.new_version)) + .inRoot(isDialog()) + .check(matches(isDisplayed())) + onView(isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-3") + + // close dialog + onView(withId(android.R.id.button1)).perform(click()) + + onView(isRoot()) + .captureToBitmap() + .writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-4") + } +}