From ef7721cf43c516b1a5c291182294ce98bb47b5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Vicente=20G=C3=B3mez=20S=C3=A1nchez?= Date: Mon, 8 Mar 2021 09:59:42 +0100 Subject: [PATCH] Fix crash after build failure (#200) * Apply a partial solution that makes us run our test but doesn't let us run them from Android Studio * Update readme * Format code * Remove message we don't actually need --- README.md | 14 +++++++--- .../scala/com/karumi/shot/ShotPlugin.scala | 28 ++++++++++++++----- .../scala/com/karumi/shot/tasks/Tasks.scala | 8 ++++-- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 12a31dbe..1b1557f1 100644 --- a/README.md +++ b/README.md @@ -73,10 +73,12 @@ This plugin sets up a few convenience commands you can list executing ``./gradle executeScreenshotTests - Checks the user interface screenshot tests. If you execute this task using -Precord param the screenshot will be regenerated. blueDebugDownloadScreenshots - Retrieves the screenshots stored into the Android device where the tests were executed for the build BlueDebug blueDebugExecuteScreenshotTests - Records the user interface tests screenshots. If you execute this task using -Precord param the screenshot will be regenerated for the build BlueDebug -blueDebugRemoveScreenshots - Removes the screenshots recorded during the tests execution from the Android device where the tests were executed for the build BlueDebug +blueDebugRemoveScreenshotsBefore - Removes the screenshots recorded before the tests execution from the Android device where the tests were executed for the build BlueDebug +blueDebugRemoveScreenshotsAfter - Removes the screenshots recorded after the tests execution from the Android device where the tests were executed for the build BlueDebug greenDebugDownloadScreenshots - Retrieves the screenshots stored into the Android device where the tests were executed for the build GreenDebug greenDebugExecuteScreenshotTests - Records the user interface tests screenshots. If you execute this task using -Precord param the screenshot will be regenerated for the build GreenDebug -greenDebugRemoveScreenshots - Removes the screenshots recorded during the tests execution from the Android device where the tests were executed for the build GreenDebug +greenDebugRemoveScreenshotsBefore - Removes the screenshots recorded before the tests execution from the Android device where the tests were executed for the build GreenDebug +greenDebugRemoveScreenshotsAfter - Removes the screenshots recorded after the tests execution from the Android device where the tests were executed for the build GreenDebug ``` If for some reason you are running your tests on a different machine and you want to skip the instrumentation tests execution and just compare the sources remember you can use the following shot configuration: @@ -255,11 +257,15 @@ or ## Executing tests from Android Studio -Shot is a Gradle plugin and it does not integrate with AS by default. After running your tests, Shot Gradle plugin will fetch the screenshots generated during tests' execution and use them to check if your tests are passing or not. You always can run your tests from command linke as explained above. However, **if you want to run your tests from AS you can create a configuration like this**: +Shot is a Gradle plugin and it does not integrate with AS by default. After running your tests, Shot Gradle plugin will fetch the screenshots generated during tests' execution and use them to check if your tests are passing or not. You always can run your tests from command line as explained above. However, **if you want to run your tests from AS you can create a configuration like this**: ![asConfig](./art/asConfig.png) -Keep in mind the debugger may not work if use use this option. If you want to debug your tests you can run them from Android Studio as you'd do with any other instrumentation test. +Keep in mind the debugger may not work if use use this option. If you want to debug your tests you can run them from Android Studio as you'd do with any other instrumentation test and you may need to execute this command before running your test: + +``` +adb rm -rf /storage/emulated/0/Download/screenshots/* +``` ## Executing tests in multiple devices diff --git a/shot/src/main/scala/com/karumi/shot/ShotPlugin.scala b/shot/src/main/scala/com/karumi/shot/ShotPlugin.scala index ef818cb5..858a3b3b 100644 --- a/shot/src/main/scala/com/karumi/shot/ShotPlugin.scala +++ b/shot/src/main/scala/com/karumi/shot/ShotPlugin.scala @@ -136,11 +136,22 @@ class ShotPlugin extends Plugin[Project] { Config.defaultInstrumentationTestTask(flavor, buildType.getName) } val tasks = project.getTasks - val removeScreenshots = tasks - .register(RemoveScreenshotsTask.name(flavor, buildType), - classOf[RemoveScreenshotsTask]) - - removeScreenshots.configure { task => + val removeScreenshotsAfterExecution = tasks + .register( + RemoveScreenshotsTask.name(flavor, buildType, beforeExecution = false), + classOf[RemoveScreenshotsTask]) + val removeScreenshotsBeforeExecution = tasks + .register( + RemoveScreenshotsTask.name(flavor, buildType, beforeExecution = true), + classOf[RemoveScreenshotsTask]) + + removeScreenshotsAfterExecution.configure { task => + task.setDescription(RemoveScreenshotsTask.description(flavor, buildType)) + task.flavor = flavor + task.buildType = buildType + task.appId = appId + } + removeScreenshotsBeforeExecution.configure { task => task.setDescription(RemoveScreenshotsTask.description(flavor, buildType)) task.flavor = flavor task.buildType = buildType @@ -172,13 +183,16 @@ class ShotPlugin extends Plugin[Project] { executeScreenshot.configure { task => task.dependsOn(instrumentationTask) task.dependsOn(downloadScreenshots) - task.dependsOn(removeScreenshots) + task.dependsOn(removeScreenshotsAfterExecution) } downloadScreenshots.configure { task => task.mustRunAfter(instrumentationTask) } - removeScreenshots.configure { task => + tasks + .getByName(instrumentationTask) + .dependsOn(removeScreenshotsBeforeExecution) + removeScreenshotsAfterExecution.configure { task => task.mustRunAfter(downloadScreenshots) } } diff --git a/shot/src/main/scala/com/karumi/shot/tasks/Tasks.scala b/shot/src/main/scala/com/karumi/shot/tasks/Tasks.scala index e9200c56..f31404c3 100644 --- a/shot/src/main/scala/com/karumi/shot/tasks/Tasks.scala +++ b/shot/src/main/scala/com/karumi/shot/tasks/Tasks.scala @@ -113,12 +113,14 @@ class DownloadScreenshotsTask extends ShotTask { } object RemoveScreenshotsTask { - def name(flavor: String, buildType: BuildType) = + def name(flavor: String, buildType: BuildType, beforeExecution: Boolean) = { + val suffix = if (beforeExecution) "Before" else "After" if (flavor.isEmpty) { - s"${buildType.getName}RemoveScreenshots" + s"${buildType.getName}RemoveScreenshots${suffix}" } else { - s"${flavor}${buildType.getName.capitalize}RemoveScreenshots" + s"${flavor}${buildType.getName.capitalize}RemoveScreenshots${suffix}" } + } def description(flavor: String, buildType: BuildType) = s"Removes the screenshots recorded during the tests execution from the Android device where the tests were executed for the build ${flavor.capitalize}${buildType.getName.capitalize}"