-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TECH: Screenshot comparison tests (#655)
* TECH: Screenshot comparison tests * TECH: lint * TECH: PR comments * TECH: Mark failed allure screenshot assertions as FAILED instead of BROKEN * TECH: Fix destination path; add documentation * TECH: Fix CustomizedSimpleTest compilation * TECH: Remove unused annotation * TECH: Use test name as diff path * TECH: Don't crash sync if gradle property not set * TECH: PR comment
- Loading branch information
Showing
34 changed files
with
642 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
16 changes: 16 additions & 0 deletions
16
.../kaspersky/components/alluresupport/interceptors/testrun/VisualTestLateFailInterceptor.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.kaspersky.components.alluresupport.interceptors.testrun | ||
|
||
import com.kaspersky.components.alluresupport.results.AllureVisualTestFlag | ||
import com.kaspersky.kaspresso.device.screenshots.ScreenshotsImpl | ||
import com.kaspersky.kaspresso.interceptors.watcher.testcase.TestRunWatcherInterceptor | ||
import com.kaspersky.kaspresso.testcases.models.info.TestInfo | ||
|
||
class VisualTestLateFailInterceptor : TestRunWatcherInterceptor { | ||
override fun onAfterSectionStarted(testInfo: TestInfo) { | ||
if (AllureVisualTestFlag.shouldFailLate.get()) { | ||
// Wrap with assertion error so test would be marked as FAILED instead of BROKEN | ||
// See https://github.com/allure-framework/allure-kotlin allure-kotlin-commons/src/main/kotlin/io/qameta/allure/kotlin/util/ResultsUtils.kt | ||
throw AssertionError(ScreenshotsImpl.ScreenshotDoesntMatchException("There were failed screenshot comparisons. Check the allure report")) | ||
} | ||
} | ||
} |
This file contains 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
11 changes: 11 additions & 0 deletions
11
...rt/src/main/kotlin/com/kaspersky/components/alluresupport/results/AllureVisualTestFlag.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.kaspersky.components.alluresupport.results | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
// TODO(Nikita Evdokimov) - Certainly there should be a better way | ||
/** | ||
* @see com.kaspersky.components.alluresupport.interceptors.testrun.VisualTestLateFailInterceptor | ||
*/ | ||
object AllureVisualTestFlag { | ||
val shouldFailLate = AtomicBoolean(false) | ||
} |
35 changes: 35 additions & 0 deletions
35
.../main/kotlin/com/kaspersky/components/alluresupport/visual/AllureScreenshotsComparator.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kaspersky.components.alluresupport.visual | ||
|
||
import android.graphics.Bitmap | ||
import com.kaspersky.components.alluresupport.files.attachScreenshotToAllureReport | ||
import com.kaspersky.kaspresso.files.resources.ResourceFileNamesProvider | ||
import com.kaspersky.kaspresso.files.resources.ResourcesDirsProvider | ||
import com.kaspersky.kaspresso.files.resources.ResourcesRootDirsProvider | ||
import com.kaspersky.kaspresso.internal.visual.DefaultScreenshotsComparator | ||
import com.kaspersky.kaspresso.logger.Logger | ||
import com.kaspersky.kaspresso.visual.VisualTestParams | ||
import java.io.File | ||
|
||
class AllureScreenshotsComparator( | ||
visualTestParams: VisualTestParams, | ||
logger: Logger, | ||
resourcesRootDirsProvider: ResourcesRootDirsProvider, | ||
resourcesDirsProvider: ResourcesDirsProvider, | ||
resourceFileNamesProvider: ResourceFileNamesProvider, | ||
) : DefaultScreenshotsComparator(visualTestParams, logger, resourcesRootDirsProvider, resourcesDirsProvider, resourceFileNamesProvider) { | ||
override fun compare(originalScreenshot: File, newScreenshot: File): Boolean { | ||
val doScreenshotsMatch = super.compare(originalScreenshot, newScreenshot) | ||
if (!doScreenshotsMatch) { | ||
originalScreenshot.attachScreenshotToAllureReport() | ||
newScreenshot.attachScreenshotToAllureReport() | ||
} | ||
|
||
return doScreenshotsMatch | ||
} | ||
|
||
override fun processScreenshotDiff(original: Bitmap, diffPixels: IntArray, diffName: String): File { | ||
return super.processScreenshotDiff(original, diffPixels, diffName).also { | ||
it.attachScreenshotToAllureReport() | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ort/src/main/kotlin/com/kaspersky/components/alluresupport/visual/AllureVisualTestCase.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kaspersky.components.alluresupport.visual | ||
|
||
import com.kaspersky.components.alluresupport.results.AllureVisualTestFlag | ||
import com.kaspersky.components.alluresupport.withForcedAllureSupport | ||
import com.kaspersky.kaspresso.device.screenshots.ScreenshotsImpl | ||
import com.kaspersky.kaspresso.kaspresso.Kaspresso | ||
import com.kaspersky.kaspresso.testcases.api.testcase.VisualTestCase | ||
import io.qameta.allure.kotlin.Allure | ||
import io.qameta.allure.kotlin.model.Status | ||
import io.qameta.allure.kotlin.model.StatusDetails | ||
|
||
abstract class AllureVisualTestCase( | ||
private val failEarly: Boolean = false, | ||
kaspressoBuilder: Kaspresso.Builder = Kaspresso.Builder.withForcedAllureSupport() | ||
) : VisualTestCase(kaspressoBuilder = kaspressoBuilder) { | ||
|
||
override fun assertScreenshot(tag: String, isFullWindow: Boolean) { | ||
try { | ||
device.screenshots.assert(tag, isFullWindow) | ||
} catch (ex: ScreenshotsImpl.ScreenshotDoesntMatchException) { | ||
if (failEarly) { | ||
// Wrap with assertion error so test would be marked as FAILED instead of BROKEN | ||
// See https://github.com/allure-framework/allure-kotlin allure-kotlin-commons/src/main/kotlin/io/qameta/allure/kotlin/util/ResultsUtils.kt | ||
throw AssertionError(ex) | ||
} | ||
|
||
Allure.lifecycle.updateStep { | ||
it.status = Status.FAILED | ||
it.statusDetails = StatusDetails(known = true, muted = true, message = ex.message, trace = ex.stackTraceToString()) | ||
} | ||
Allure.lifecycle.stopStep() | ||
AllureVisualTestFlag.shouldFailLate.set(true) | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
.../src/main/kotlin/com/kaspersky/components/alluresupport/visual/AllureVisualTestWatcher.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.kaspersky.components.alluresupport.visual | ||
|
||
import com.kaspersky.components.alluresupport.files.dirs.AllureDirsProvider | ||
import com.kaspersky.kaspresso.device.files.Files | ||
import com.kaspersky.kaspresso.files.resources.ResourcesRootDirsProvider | ||
import com.kaspersky.kaspresso.logger.Logger | ||
import com.kaspersky.kaspresso.visual.VisualTestParams | ||
import com.kaspersky.kaspresso.visual.VisualTestType | ||
import com.kaspersky.kaspresso.visual.VisualTestWatcher | ||
import java.io.File | ||
|
||
class AllureVisualTestWatcher( | ||
private val params: VisualTestParams, | ||
private val logger: Logger, | ||
private val dirsProvider: AllureDirsProvider, | ||
resourcesRootDirsProvider: ResourcesRootDirsProvider, | ||
private val files: Files, | ||
) : VisualTestWatcher { | ||
|
||
private val diffDir = dirsProvider.provideNew(resourcesRootDirsProvider.screenshotsDiffRootDir) | ||
private val originalScreenshotsTargetDir: File | ||
get() { | ||
val rootDir = dirsProvider.provideNewOnSdCard(File("")).absolutePath | ||
return File(rootDir, File(params.hostScreenshotsDir).name) | ||
} | ||
|
||
override fun prepare() { | ||
logger.i("Visual test run started. Parameters: $params") | ||
|
||
if (params.testType == VisualTestType.Compare) { | ||
logger.i("Pushing the screenshots unto the device...") | ||
dirsProvider.provideCleared(diffDir) | ||
|
||
// Allure stores all files in the app's private directory. We can't "adb push" directly there, | ||
// so we have to do this in 2 steps | ||
dirsProvider.provideCleared(originalScreenshotsTargetDir) | ||
val tmp = dirsProvider.provideNewOnSdCard(File("")) | ||
files.push(params.hostScreenshotsDir, tmp.absolutePath) | ||
val target = dirsProvider.provideNew(File("")).resolve(params.hostScreenshotsDir) | ||
File(tmp, params.hostScreenshotsDir).copyRecursively(target, overwrite = true) | ||
logger.i("Done pushing the screenshots unto the device") | ||
} | ||
} | ||
|
||
override fun cleanUp() { | ||
// Do nothing | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,5 +32,6 @@ configure<BaseExtension> { | |
resValues = false | ||
shaders = false | ||
viewBinding = false | ||
buildConfig = true | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
publish.artifactGroup=com.kaspersky.android-components | ||
publish.artifactName=kaspresso | ||
publish.publicationName=Kaspresso | ||
publish.bintrayRepo=Kaspresso | ||
publish.bintrayRepo=Kaspresso |
This file contains 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
This file contains 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
Oops, something went wrong.