diff options
| author | Niklas Berglund <niklas.berglund@gmail.com> | 2024-10-01 15:05:52 +0200 |
|---|---|---|
| committer | David Göransson <david.goransson@mullvad.net> | 2024-10-15 14:26:40 +0200 |
| commit | d61bc04d6dc2804f3ba1af6fb376b02908256b9f (patch) | |
| tree | a798367a6768793ff1d085cbd8f3d8fc9fe7d3b5 /android | |
| parent | 291615ef3da058394760c9c0820f00232b811757 (diff) | |
| download | mullvadvpn-d61bc04d6dc2804f3ba1af6fb376b02908256b9f.tar.xz mullvadvpn-d61bc04d6dc2804f3ba1af6fb376b02908256b9f.zip | |
Add video recording of e2e tests
Diffstat (limited to 'android')
3 files changed, 66 insertions, 0 deletions
diff --git a/android/test/common/src/main/kotlin/net/mullvad/mullvadvpn/test/common/misc/CaptureScreenRecordingsExtension.kt b/android/test/common/src/main/kotlin/net/mullvad/mullvadvpn/test/common/misc/CaptureScreenRecordingsExtension.kt new file mode 100644 index 0000000000..21cc915482 --- /dev/null +++ b/android/test/common/src/main/kotlin/net/mullvad/mullvadvpn/test/common/misc/CaptureScreenRecordingsExtension.kt @@ -0,0 +1,60 @@ +package net.mullvad.mullvadvpn.test.common.misc + +import android.os.Environment +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.uiautomator.UiDevice +import co.touchlab.kermit.Logger +import java.io.File +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Assertions.fail +import org.junit.jupiter.api.extension.AfterEachCallback +import org.junit.jupiter.api.extension.BeforeEachCallback +import org.junit.jupiter.api.extension.ExtensionContext + +class CaptureScreenRecordingsExtension : BeforeEachCallback, AfterEachCallback { + private lateinit var job: Job + private val coroutineScope = CoroutineScope(Dispatchers.IO) + private lateinit var device: UiDevice + + override fun beforeEach(context: ExtensionContext?) { + device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + val testMethodName = context?.testMethod!!.get().name + val fileName = "${testMethodName}.mp4" + Logger.v("Starting screen recording. Saving to $testMethodName") + startScreenRecord(fileName) + } + + override fun afterEach(context: ExtensionContext?) { + Logger.v("Stopping screen recording") + stopScreenRecord() + } + + private fun startScreenRecord(fileName: String) { + if (File(OUTPUT_DIRECTORY).exists().not()) { + File(OUTPUT_DIRECTORY).mkdirs() + } + + job = + coroutineScope.launch { + device.executeShellCommand("screenrecord $OUTPUT_DIRECTORY/$fileName") + } + } + + private fun stopScreenRecord() { + try { + device.executeShellCommand("pkill -2 screenrecord") + runBlocking { job.join() } + } catch (e: Exception) { + fail("Failed to stop screen recording") + } + } + + companion object { + val OUTPUT_DIRECTORY = + "${Environment.getExternalStorageDirectory().path}/Download/test-attachments/video" + } +} diff --git a/android/test/e2e/README.md b/android/test/e2e/README.md index b1f08c8751..50e8d177a5 100644 --- a/android/test/e2e/README.md +++ b/android/test/e2e/README.md @@ -54,3 +54,6 @@ docker run --rm --volumes-from gcloud-config -v ${PWD}:/android gcr.io/google.co --use-orchestrator \ --environment-variables clearPackageData=true,valid_test_account_number=XXXX,invalid_test_account_number=XXXX ``` + +## Test artefacts +Test artefacts are stored on the test device in `/sdcard/Download/test-attachments`. In CI this directory is cleared in between each test run, but note that when running tests locally the directory isn't cleared but already existing files are overwritten. diff --git a/android/test/e2e/src/main/kotlin/net/mullvad/mullvadvpn/test/e2e/EndToEndTest.kt b/android/test/e2e/src/main/kotlin/net/mullvad/mullvadvpn/test/e2e/EndToEndTest.kt index 278def0b35..ba559ffab0 100644 --- a/android/test/e2e/src/main/kotlin/net/mullvad/mullvadvpn/test/e2e/EndToEndTest.kt +++ b/android/test/e2e/src/main/kotlin/net/mullvad/mullvadvpn/test/e2e/EndToEndTest.kt @@ -8,11 +8,14 @@ import androidx.test.uiautomator.UiDevice import co.touchlab.kermit.Logger import de.mannodermaus.junit5.extensions.GrantPermissionExtension import net.mullvad.mullvadvpn.test.common.interactor.AppInteractor +import net.mullvad.mullvadvpn.test.common.misc.CaptureScreenRecordingsExtension import net.mullvad.mullvadvpn.test.common.rule.CaptureScreenshotOnFailedTestRule import net.mullvad.mullvadvpn.test.e2e.constant.LOG_TAG import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.RegisterExtension +@ExtendWith(CaptureScreenRecordingsExtension::class) abstract class EndToEndTest(private val infra: String) { @RegisterExtension @JvmField val rule = CaptureScreenshotOnFailedTestRule(LOG_TAG) |
