Skip to content

Commit

Permalink
#96: Deprecated TestFailRule
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikitae57 committed Nov 7, 2022
1 parent 4224918 commit 37f6652
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import org.junit.runner.Description
/**
* The test rule to capture a screenshot in case of unexpected docloc screenshot test failure.
*/
@Deprecated(
"Current implementation isn't suited for doc loc tests error capturing because it's triggered after test is finished. It leads to capturing screenshots of home screen",
replaceWith = ReplaceWith(
"""
Kaspresso.Builder.simple().apply {
stepWatcherInterceptors.add(ScreenshotStepWatcherInterceptor(screenshots))
}
"""
)
)
class TestFailRule internal constructor() : TestWatcher() {

internal lateinit var screenshotCapturer: DocLocScreenshotCapturer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ private const val TEST_CASE_METHOD_JUNIT_4 = "runReflectiveCall"
private const val TEST_CASE_CLASS_CUCUMBER_JVM = "cucumber.runtime.model.CucumberFeature"
private const val TEST_CASE_METHOD_CUCUMBER_JVM = "run"

internal fun Array<StackTraceElement>.findTestMethod(): TestMethod {
return findTestClassTraceElement()
.let { TestMethod(it.className, it.methodName) }
internal fun Array<StackTraceElement>.findTestMethod(): TestMethod? {
val testTraceElement = findTestClassTraceElement()
return testTraceElement?.let { TestMethod(it.className, it.methodName) }
}

private fun Array<StackTraceElement>.findTestClassTraceElement(): StackTraceElement {
private fun Array<StackTraceElement>.findTestClassTraceElement(): StackTraceElement? {
return this.withIndex().reversed()
.find { (_, element) -> element.isJunit3() || element.isJunit4() || element.isCucumber() }
?.let { (i, _) -> extractStackElement(i) }
?: throw IllegalArgumentException("Could not find test class! Trace: ${this.map { it.toString() }}")
}

private fun StackTraceElement.isJunit3(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ class DefaultResourcesDirsProvider(
}

private fun resolveResourcesDirDest(rootDir: File, subDir: String? = null): File {
val testMethod: TestMethod = testThread.stackTrace.findTestMethod()
val resourcesDirName: String = resourcesDirNameProvider.provideResourcesDirName(testMethod)
return when (subDir) {
null -> rootDir.resolve(resourcesDirName)
else -> rootDir.resolve(subDir).resolve(resourcesDirName)

return rootDir.run {
var resourceDir = this
val testMethod: TestMethod? = testThread.stackTrace.findTestMethod()

subDir?.let { resourceDir = resourceDir.resolve(it) }
testMethod?.let {
val resourcesDirName: String = resourcesDirNameProvider.provideResourcesDirName(testMethod)
resourceDir = resourceDir.resolve(resourcesDirName)
}

resourceDir
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.kaspersky.kaspresso.interceptors.watcher.testcase.impl.screenshot

import com.kaspersky.kaspresso.device.screenshots.Screenshots
import com.kaspersky.kaspresso.interceptors.watcher.testcase.StepWatcherInterceptor
import com.kaspersky.kaspresso.testcases.models.info.StepInfo

class ScreenshotFailStepWatcherInterceptor(
private val screenshots: Screenshots
) : StepWatcherInterceptor {
/**
* Takes a screenshot of the screen on which the step failed.
*
* @param stepInfo the step info to log.
* @param error the error occurred to use in screenshots name.
*/
override fun interceptAfterWithError(stepInfo: StepInfo, error: Throwable) {
screenshots.take("${makeTag(stepInfo)}_failure_${error.javaClass.simpleName}")
}

private fun makeTag(stepInfo: StepInfo): String = "${stepInfo.testClassName}_step_${stepInfo.ordinal}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ abstract class DocLocScreenshotTestCase(
),
private val changeSystemLocale: Boolean = false,
locales: String?,
kaspressoBuilder: Kaspresso.Builder = Kaspresso.Builder.simple()
kaspressoBuilder: Kaspresso.Builder = Kaspresso.Builder.simple().apply {
testRunWatcherInterceptors.add(TestRunnerScreenshotWatcherInterceptor(screenshots))
}
) : TestCase(kaspressoBuilder = kaspressoBuilder) {

@Deprecated(
Expand All @@ -77,7 +79,9 @@ abstract class DocLocScreenshotTestCase(
screenshotNameProvider: ScreenshotNameProvider = DefaultScreenshotNameProvider(addTimestamps = false),
changeSystemLocale: Boolean = false,
locales: String?,
kaspressoBuilder: Kaspresso.Builder = Kaspresso.Builder.simple()
kaspressoBuilder: Kaspresso.Builder = Kaspresso.Builder.simple().apply {
stepWatcherInterceptors.add(ScreenshotStepWatcherInterceptor(screenshots))
}
) : this(
resourcesRootDirsProvider = object : ResourcesRootDirsProvider {
override val logcatRootDir: File = File("logcat")
Expand Down Expand Up @@ -116,9 +120,6 @@ abstract class DocLocScreenshotTestCase(
@get:Rule
val storagePermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)!!

@get:Rule
val testFailRule = TestFailRule()

@Before
fun setup() {
if (!kaspresso.instrumentalDependencyProvider.isAndroidRuntime) {
Expand All @@ -141,8 +142,6 @@ abstract class DocLocScreenshotTestCase(
screenshotMaker = ExternalScreenshotMaker(kaspresso.instrumentalDependencyProvider),
metadataSaver = MetadataSaver(kaspresso.device.activities, kaspresso.device.apps, logger)
)

testFailRule.screenshotCapturer = screenshotCapturer
}

/**
Expand Down

0 comments on commit 37f6652

Please sign in to comment.