Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
rft Logcat rules
Browse files Browse the repository at this point in the history
  • Loading branch information
viclovsky committed Sep 3, 2019
1 parent a2e3026 commit 37401e0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()

// ...
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())) {
Expand Down

0 comments on commit 37401e0

Please sign in to comment.