Skip to content

Instantly share code, notes, and snippets.

@eakurnikov
Created June 16, 2020 18:16
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 eakurnikov/d818e8330c67a44bc027df53b14f4f5e to your computer and use it in GitHub Desktop.
Save eakurnikov/d818e8330c67a44bc027df53b14f4f5e to your computer and use it in GitHub Desktop.
Kaspresso-Allure mapping interceptors
class AllureMapperStepInterceptor : StepWatcherInterceptor {
private var allureStep: Step? = null
override fun interceptBefore(stepInfo: StepInfo) {
allureStep = Step().apply { stepStart(stepInfo.description) }
}
override fun interceptAfterWithSuccess(stepInfo: StepInfo) {
allureStep?.stepCompleted()
}
override fun interceptAfterWithError(stepInfo: StepInfo, error: Throwable) {
allureStep?.stepThrown(error)
}
override fun interceptAfterFinally(stepInfo: StepInfo) {
allureStep?.stepStop()
}
}
class ScreenshotStepInterceptor(
private val screenShooter: ScreenShooter
) : StepWatcherInterceptor {
override fun interceptAfterWithSuccess(stepInfo: StepInfo) {
val tag = makeScreenshotTag(stepInfo)
screenShooter.take(tag)?.let {
AllureAndroidLifecycle.addAttachment(
name = tag,
type = IMAGE_PNG,
fileExtension = PNG_EXTENSION,
file = it
)
}
}
override fun interceptAfterWithError(stepInfo: StepInfo, error: Throwable) {
val tag = "${makeScreenshotTag(stepInfo)}_failure_${error.javaClass.simpleName}"
screenShooter.take(tag)?.let {
AllureAndroidLifecycle.addAttachment(
name = tag,
type = IMAGE_PNG,
fileExtension = PNG_EXTENSION,
file = it
)
}
}
private fun makeScreenshotTag(stepInfo: StepInfo): String = "${stepInfo.testClassName}_step_${stepInfo.ordinal}"
}
class DispatchingTestRunInterceptor(
private val testResultsDirsProvider: TestResultsDirsProvider
) : TestRunWatcherInterceptor {
override fun onTestStarted(testInfo: TestInfo) {
testResultsDirsProvider.onNewTestRun()
}
}
class DumpLogcatTestRunInterceptor(
private val testResultsFilesProvider: TestResultsFilesProvider
) : TestRunWatcherInterceptor {
override fun onTestFinished(testInfo: TestInfo, success: Boolean) {
testResultsFilesProvider.logcatFile {
BufferedTestLogger.dump(it)
AllureAndroidLifecycle.addAttachment(
name = it.path,
type = TEXT_PLAIN,
fileExtension = TXT_EXTENSION,
file = it
)
}
}
}
class ScreenshotTestRunInterceptor(
private val screenShooter: ScreenShooter
) : TestRunWatcherInterceptor {
override fun onAfterSectionFinishedFailed(testInfo: TestInfo, throwable: Throwable) {
val tag = "AfterTestSection_failure_${throwable.javaClass.simpleName}"
screenShooter.take(tag)?.let {
AllureAndroidLifecycle.addAttachment(
name = tag,
type = IMAGE_PNG,
fileExtension = PNG_EXTENSION,
file = it
)
}
}
override fun onBeforeSectionFinishedFailed(testInfo: TestInfo, throwable: Throwable) {
val tag = "BeforeTestSection_failure_${throwable.javaClass.simpleName}"
screenShooter.take(tag)?.let {
AllureAndroidLifecycle.addAttachment(
name = tag,
type = IMAGE_PNG,
fileExtension = PNG_EXTENSION,
file = it
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment