Skip to content

Instantly share code, notes, and snippets.

@wellavelino
Created July 18, 2019 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wellavelino/697a311f552cfc6ee74d44ba656f2ab0 to your computer and use it in GitHub Desktop.
Save wellavelino/697a311f552cfc6ee74d44ba656f2ab0 to your computer and use it in GitHub Desktop.
gradle pull screen shots
// Basead on https://github.com/zawadz88/ScreenshotCapturingSample/blob/master/app/build.gradle
def reportsDirectory = "artifacts/composer-output"
def embedScreenshotsTask = task('embedScreenshots', group: 'reporting') {
dependsOn {
fetchScreenshotsTask
}
doFirst {
//here is the problem we're not creating the folder when needed, if we figure it out
// everything will work fine.
def failureScreenshotsDirectory = new File(reportsDirectory, 'screenshots/failures')
if (!failureScreenshotsDirectory.exists()) {
println 'Could not find screenshot failures. Skipping...'
return
}
failureScreenshotsDirectory.eachFile { failedTestClassDirectory ->
def failedTestClassName = failedTestClassDirectory.name
failedTestClassDirectory.eachFile { failedTestFile ->
def failedTestName = failedTestFile.name
print failedTestName
def failedTestNameWithoutExtension = failedTestName.take(failedTestName.lastIndexOf('.'))
def failedTestClassJunitReportFile = new File(reportsDirectory, "${failedTestClassName}.html")
failedTestClassJunitReportFile.createNewFile()
if (!failedTestClassJunitReportFile.exists()) {
println "Could not find JUnit report file for test class '${failedTestClassJunitReportFile}'"
return
}
print failedTestClassJunitReportFile.text
def failedTestJunitReportContent = failedTestClassJunitReportFile.text
def patternToFind = "<h3 class=\"failures\">${failedTestNameWithoutExtension}</h3>"
def patternToReplace = "${patternToFind} <img src=\"screenshots/failures/${failedTestClassName}/${failedTestName}\" width =\"360\" />"
failedTestJunitReportContent = failedTestJunitReportContent.replaceAll(patternToFind, patternToReplace)
failedTestClassJunitReportFile.write(failedTestJunitReportContent)
}
}
}
}
def getDevices() {
new ByteArrayOutputStream().withStream { os ->
def result = exec {
commandLine android.getAdbExe().toString(), 'devices'
standardOutput = os
}
def connectedDevices = []
def outputAsString = os.toString()
def listOfDevices = outputAsString.readLines()
listOfDevices.remove(0)
listOfDevices.each {
if (!it.isEmpty()) {
def newLine = it.split()
connectedDevices.add(newLine[0])
}
}
return connectedDevices
}
}
def fetchScreenshotsTask = task('fetchScreenshotsTask', type: Exec, group: 'reporting') {
println "Creating output directory"
def currentDir = new File(reportsDirectory).getAbsolutePath()
getDevices().each {
def id = it
commandLine android.getAdbExe().toString()
println "Creating screenshot folder for ${id.toString()}"
args "-s", id.toString(), 'shell', 'mkdir', '-p', '/sdcard/Pictures/scarlett_screenshot_folder'
commandLine android.getAdbExe().toString()
println "Pulling screenshots for ${id.toString()}"
args "-s", id.toString(), 'pull', '/sdcard/Pictures/scarlett_screenshot_folder/.', currentDir
// commandLine android.getAdbExe().toString()
// println "Deleting folder for ${id.toString()}"
// args "-s", id.toString(), 'shell', 'rm', '-r', '/sdcard/Pictures/scarlett_screenshot_folder'
}
}
tasks.whenTaskAdded { task ->
if (task.name == 'connectedDevelopmentDebugAndroidTest') {
task.finalizedBy {
embedScreenshotsTask
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment