Skip to content

Commit

Permalink
Fix crash after build failure (#200)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pedrovgs authored Mar 8, 2021
1 parent 53685b1 commit ef7721c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions shot/src/main/scala/com/karumi/shot/ShotPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
8 changes: 5 additions & 3 deletions shot/src/main/scala/com/karumi/shot/tasks/Tasks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit ef7721c

Please sign in to comment.