Last active
January 22, 2020 13:05
-
-
Save wellavelino/ab03e913bce14411cb64087f882ce91d to your computer and use it in GitHub Desktop.
gradle screenshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reportsDirectory = "$buildDir/artifacts/composer-output" | |
def embedScreenshotsTask = task('embedScreenshots', group: 'reporting') { | |
dependsOn { | |
fetchScreenshotsTask | |
} | |
doFirst { | |
println new File(reportsDirectory, 'screenshots/failures') | |
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 | |
def failedTestNameWithoutExtension = failedTestName.take(failedTestName.lastIndexOf('.')) | |
def failedTestClassJunitReportFile = new File(reportsDirectory, "${failedTestClassName}.html") | |
if (!failedTestClassJunitReportFile.exists()) { | |
println "Could not find JUnit report file for test class '${failedTestClassJunitReportFile}'" | |
return | |
} | |
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" | |
new File(reportsDirectory).mkdirs() | |
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/.', reportsDirectory | |
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 { | |
fetchScreenshotsTask | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment