diff --git a/README.md b/README.md index 1652668..c98b2f8 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ fun test() { } ``` -### Failshot Rule +### Failshot rule You can use `FailshotRule` to capture a screenshot in case of `Throwable` during test ```kotlin class MyTest { @@ -120,13 +120,25 @@ class MyTest { } ``` -### Logcat Rule -You can use `LogcatRule` to capture a device logs in case of `Throwable` during test +### Logcat rule's +You can use `LogcatDumpRule`, `LogcatClearRule` to clear and capture a device logs in case of `Throwable` during test ```kotlin class MyTest { @get:Rule - val logcatRule = LogcatRule() + val ruleChain = RuleChain.outerRule(LogcatClearRule()).around(LogcatDumpRule()) + + // ... +} +``` + +### Window hierarchy rule +You can use `WindowHierarchyRule` to capture a window hierarchy via UIautomator in case of `Throwable` during test +```kotlin +class MyTest { + + @get:Rule + val windowHierarchyRule = WindowHierarchyRule() // ... } diff --git a/allure-android/src/androidTest/kotlin/ru/tinkoff/allure/android/AllureTest.kt b/allure-android/src/androidTest/kotlin/ru/tinkoff/allure/android/AllureTest.kt index 96b8013..9d874f5 100644 --- a/allure-android/src/androidTest/kotlin/ru/tinkoff/allure/android/AllureTest.kt +++ b/allure-android/src/androidTest/kotlin/ru/tinkoff/allure/android/AllureTest.kt @@ -23,7 +23,8 @@ import kotlin.test.fail class AllureTest { @get:Rule - val ruleChain = RuleChain.outerRule(FailshotRule()).around(WindowHierarchyRule()).around(LogcatRule()) + val ruleChain = RuleChain.outerRule(LogcatClearRule()).around(FailshotRule()) + .around(WindowHierarchyRule()).around(LogcatDumpRule()) @Test fun shouldAddGreenStep() = step("Step1") { diff --git a/allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatClearRule.kt b/allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatClearRule.kt new file mode 100644 index 0000000..f05e564 --- /dev/null +++ b/allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatClearRule.kt @@ -0,0 +1,28 @@ +package ru.tinkoff.allure.android + +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 + +class LogcatClearRule : TestRule { + override fun apply(base: Statement, description: Description): Statement { + return object : Statement() { + override fun evaluate() { + try { + clearLogcat() + base.evaluate() + } catch (t: Throwable) { + throw t + } + } + } + } + + private fun clearLogcat() { + with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())) { + executeShellCommand("logcat -c") + } + } +} \ No newline at end of file diff --git a/allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatRule.kt b/allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatDumpRule.kt similarity index 82% rename from allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatRule.kt rename to allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatDumpRule.kt index 63a60bf..26e675b 100644 --- a/allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatRule.kt +++ b/allure-android/src/main/kotlin/ru/tinkoff/allure/android/LogcatDumpRule.kt @@ -9,12 +9,11 @@ import ru.tinkoff.allure.io.TEXT_PLAIN import ru.tinkoff.allure.io.TXT_EXTENSION import ru.tinkoff.allure.utils.createAttachmentFile -class LogcatRule : TestRule { +class LogcatDumpRule : TestRule { override fun apply(base: Statement, description: Description): Statement { return object : Statement() { override fun evaluate() { try { - clearLogcat() base.evaluate() } catch (t: Throwable) { dumpLogcat() @@ -24,12 +23,6 @@ class LogcatRule : TestRule { } } - private fun clearLogcat() { - with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())) { - executeShellCommand("logcat -c") - } - } - private fun dumpLogcat() { val file = createAttachmentFile() with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())) {