Fix FailshotRule

This commit is contained in:
grechka62
2023-03-14 13:46:27 +03:00
parent 8c913342ae
commit a772c4efc2
3 changed files with 43 additions and 2 deletions
@@ -3,12 +3,12 @@
package com.yandex.div.rule
import com.yandex.test.rules.ClosePopupsRule
import com.yandex.test.rules.FailshotRule
import com.yandex.test.rules.LogcatReportRule
import com.yandex.test.rules.NoAnimationsRule
import com.yandex.test.rules.WindowHierarchyRule
import com.yandex.test.util.chain
import org.junit.rules.TestRule
import ru.tinkoff.allure.android.FailshotRule
fun uiTestRule(innerRule: () -> TestRule): TestRule {
return LogcatReportRule()
@@ -5,13 +5,13 @@ package com.yandex.div.rule
import com.yandex.divkit.demo.Container
import com.yandex.test.idling.waitForIdlingResource
import com.yandex.test.rules.ClosePopupsRule
import com.yandex.test.rules.FailshotRule
import com.yandex.test.rules.LogcatReportRule
import com.yandex.test.rules.NoAnimationsRule
import com.yandex.test.rules.WindowHierarchyRule
import com.yandex.test.screenshot.ScreenshotRule
import com.yandex.test.util.chain
import org.junit.rules.TestRule
import ru.tinkoff.allure.android.FailshotRule
fun screenshotRule(
relativePath: String = "",
@@ -0,0 +1,41 @@
package com.yandex.test.rules
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import ru.tinkoff.allure.android.AllureAndroidLifecycle
import java.io.File
import java.util.concurrent.TimeUnit
private const val FILE_NAME = "failshot"
private const val FILE_TYPE = "image/png"
private const val FILE_EXTENSION = ".png"
class FailshotRule : TestRule {
private val instrumentation = InstrumentationRegistry.getInstrumentation()
private val cacheDir get() = instrumentation.targetContext.cacheDir
private val uiDevice get() = UiDevice.getInstance(instrumentation)
override fun apply(base: Statement, description: Description) = object : Statement() {
override fun evaluate() {
runCatching {
base.evaluate()
}.onFailure { error ->
failshot()
throw error
}
}
}
private fun failshot() {
val file = File.createTempFile(FILE_NAME, FILE_TYPE, cacheDir)
uiDevice.waitForIdle(TimeUnit.SECONDS.toMillis(5L))
uiDevice.takeScreenshot(file)
AllureAndroidLifecycle.addAttachment(FILE_NAME, FILE_TYPE, FILE_EXTENSION, file)
}
}