Skip to content

Commit

Permalink
Merge pull request #413 from KasperskyLab/issue-96/Doc_loc_tests_dont…
Browse files Browse the repository at this point in the history
…_take_fail_screenshots

Issue 96/doc loc tests dont take fail screenshots
  • Loading branch information
Nikitae57 authored Nov 7, 2022
2 parents 70cf8db + 58e6456 commit ee3297a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 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 {
testRunWatcherInterceptors.add(TestRunnerScreenshotWatcherInterceptor(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 @@ -31,4 +31,14 @@ class TestRunnerScreenshotWatcherInterceptor(
override fun onBeforeSectionFinishedFailed(testInfo: TestInfo, throwable: Throwable) {
screenshots.take("BeforeTestSection_failure_${throwable.javaClass.simpleName}")
}

/**
* Takes a screenshot of the screen on which the "main" section failed.
*
* @param testInfo the test info to use in screenshots name.
* @param throwable the error occurred to use in screenshots name.
*/
override fun onMainSectionFinishedFailed(testInfo: TestInfo, throwable: Throwable) {
screenshots.take("MainTestSection_failure_${throwable.javaClass.simpleName}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@ import com.kaspersky.kaspresso.device.screenshots.screenshotmaker.InternalScreen
import com.kaspersky.kaspresso.docloc.DocLocScreenshotCapturer
import com.kaspersky.kaspresso.docloc.MetadataSaver
import com.kaspersky.kaspresso.docloc.rule.LocaleRule
import com.kaspersky.kaspresso.docloc.rule.TestFailRule
import com.kaspersky.kaspresso.files.dirs.DefaultDirsProvider
import com.kaspersky.kaspresso.files.resources.ResourcesRootDirsProvider
import com.kaspersky.kaspresso.files.resources.ResourcesDirsProvider
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.files.resources.impl.DefaultResourceFileNamesProvider
import com.kaspersky.kaspresso.files.resources.impl.DefaultResourceFilesProvider
import com.kaspersky.kaspresso.files.resources.impl.DefaultResourcesDirNameProvider
import com.kaspersky.kaspresso.files.resources.impl.DefaultResourcesDirsProvider
import com.kaspersky.kaspresso.files.resources.impl.DefaultResourcesRootDirsProvider
import com.kaspersky.kaspresso.files.resources.impl.SupportLegacyResourcesDirNameProvider
import com.kaspersky.kaspresso.instrumental.exception.DocLocInUnitTestException
import com.kaspersky.kaspresso.interceptors.watcher.testcase.impl.screenshot.ScreenshotStepWatcherInterceptor
import com.kaspersky.kaspresso.interceptors.watcher.testcase.impl.screenshot.TestRunnerScreenshotWatcherInterceptor
import com.kaspersky.kaspresso.internal.extensions.other.getAllInterfaces
import com.kaspersky.kaspresso.internal.invocation.UiInvocationHandler
import com.kaspersky.kaspresso.kaspresso.Kaspresso
import com.kaspersky.kaspresso.logger.UiTestLogger
import com.kaspersky.kaspresso.params.ScreenshotParams
import java.io.File
import java.lang.reflect.Proxy
import org.junit.Before
import org.junit.Rule
import java.io.File
import java.lang.reflect.Proxy

/**
* The base class for all docloc screenshot tests.
Expand Down Expand Up @@ -65,7 +66,9 @@ abstract class DocLocScreenshotTestCase(
private val changeSystemLocale: Boolean = false,
private val screenshotParams: ScreenshotParams = ScreenshotParams(),
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 @@ -82,7 +85,9 @@ abstract class DocLocScreenshotTestCase(
changeSystemLocale: Boolean = false,
locales: String?,
screenshotParams: ScreenshotParams = ScreenshotParams(),
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 @@ -122,9 +127,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 Down Expand Up @@ -153,8 +155,6 @@ abstract class DocLocScreenshotTestCase(
),
metadataSaver = MetadataSaver(kaspresso.device.activities, kaspresso.device.apps, logger)
)

testFailRule.screenshotCapturer = screenshotCapturer
}

/**
Expand Down

0 comments on commit ee3297a

Please sign in to comment.