Add more logs when tests failure
This commit is contained in:
127
src/main/java/ru/samsung/test/core/core/BaseTest.kt
Normal file → Executable file
127
src/main/java/ru/samsung/test/core/core/BaseTest.kt
Normal file → Executable file
@@ -1,100 +1,85 @@
|
|||||||
package ru.samsung.test.core.core
|
package ru.samsung.test.core.core
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.Instrumentation
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import androidx.compose.ui.test.junit4.createComposeRule
|
|
||||||
import androidx.test.core.app.ActivityScenario
|
import androidx.test.core.app.ActivityScenario
|
||||||
import androidx.test.espresso.Espresso
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
|
||||||
import androidx.test.uiautomator.UiDevice
|
|
||||||
import com.kaspersky.components.composesupport.config.addComposeSupport
|
import com.kaspersky.components.composesupport.config.addComposeSupport
|
||||||
import com.kaspersky.kaspresso.idlewaiting.KautomatorWaitForIdleSettings
|
import com.kaspersky.kaspresso.idlewaiting.KautomatorWaitForIdleSettings
|
||||||
import com.kaspersky.kaspresso.kaspresso.Kaspresso
|
import com.kaspersky.kaspresso.kaspresso.Kaspresso
|
||||||
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
|
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
|
||||||
import com.kaspersky.kaspresso.testcases.core.sections.AfterTestSection
|
|
||||||
import com.kaspersky.kaspresso.testcases.core.testcontext.BaseTestContext
|
import com.kaspersky.kaspresso.testcases.core.testcontext.BaseTestContext
|
||||||
import com.kaspersky.kaspresso.testcases.core.testcontext.TestContext
|
import com.kaspersky.kaspresso.testcases.core.testcontext.TestContext
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import org.junit.After
|
|
||||||
import org.junit.Before
|
|
||||||
import org.junit.FixMethodOrder
|
import org.junit.FixMethodOrder
|
||||||
import org.junit.Rule
|
|
||||||
import org.junit.runners.MethodSorters
|
import org.junit.runners.MethodSorters
|
||||||
import ru.samsung.test.core.utils.EmptyRule
|
|
||||||
import ru.samsung.test.core.utils.ResultTestsData
|
import ru.samsung.test.core.utils.ResultTestsData
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Базовый класс тестов, который подключается для запуска основных
|
||||||
|
*
|
||||||
|
* @param clazz класс активити, который будет запущен при старте.
|
||||||
|
* Эта активити должна иметь соответствующий флаг в манифесте и
|
||||||
|
*/
|
||||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
open class BaseTest<LaunchActivity : Activity>(
|
open class BaseTest<LaunchActivity : Activity>(
|
||||||
private val clazz: Class<LaunchActivity>,
|
clazz: Class<LaunchActivity>,
|
||||||
private val isEnabledCompose: Boolean = false
|
isEnabledCompose: Boolean = false,
|
||||||
) : TestCase(kaspressoBuilder = Kaspresso.Builder.simple {
|
private val skipAfterFirstFail: Boolean = true,
|
||||||
kautomatorWaitForIdleSettings = KautomatorWaitForIdleSettings.boost()
|
) : TestCase(
|
||||||
}.apply {
|
kaspressoBuilder = getBuilder(
|
||||||
testRunWatcherInterceptors.add(ResultTestRunWatcherInterceptor())
|
clazz = clazz,
|
||||||
if (isEnabledCompose) addComposeSupport()
|
isEnabledCompose = isEnabledCompose,
|
||||||
}) {
|
)
|
||||||
@get:Rule
|
) {
|
||||||
val composeTestRule = if (isEnabledCompose) createComposeRule() else EmptyRule()
|
|
||||||
|
|
||||||
protected lateinit var activityScenario: ActivityScenario<LaunchActivity>
|
|
||||||
private set
|
|
||||||
protected lateinit var uiDevice: UiDevice
|
|
||||||
private set
|
|
||||||
protected lateinit var appContext: Context
|
|
||||||
private set
|
|
||||||
protected lateinit var instrumentation: Instrumentation
|
|
||||||
private set
|
|
||||||
|
|
||||||
open fun beforeTest() {}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
fun setUp() {
|
|
||||||
instrumentation = InstrumentationRegistry.getInstrumentation()
|
|
||||||
val handler = DescriptionFailureHandler(instrumentation)
|
|
||||||
Espresso.setFailureHandler(handler)
|
|
||||||
|
|
||||||
uiDevice = UiDevice.getInstance(instrumentation)
|
|
||||||
uiDevice.pressHome()
|
|
||||||
Thread.sleep(1_000)
|
|
||||||
|
|
||||||
val nonLocalizedContext = instrumentation.targetContext
|
|
||||||
val configuration = nonLocalizedContext.resources.configuration
|
|
||||||
configuration.setLocale(Locale.UK)
|
|
||||||
appContext = nonLocalizedContext.createConfigurationContext(configuration)
|
|
||||||
|
|
||||||
val intent = Intent(appContext, clazz)
|
|
||||||
|
|
||||||
activityScenario = ActivityScenario.launch(intent)
|
|
||||||
|
|
||||||
beforeTest()
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
fun reset() {
|
|
||||||
uiDevice.pressHome()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun runWithInit(grade: Int, test: TestContext<Unit>.() -> Unit) = run {
|
fun runWithInit(grade: Int, test: TestContext<Unit>.() -> Unit) = run {
|
||||||
|
val hasFailed = ResultTestsData.hasFailed()
|
||||||
ResultTestsData.setupTest(grade)
|
ResultTestsData.setupTest(grade)
|
||||||
|
if (hasFailed && skipAfterFirstFail) throw Throwable("SKIPPED. Prev test failed")
|
||||||
try {
|
try {
|
||||||
test.invoke(this)
|
test.invoke(this)
|
||||||
ResultTestsData.successTest(grade)
|
ResultTestsData.successTest(grade)
|
||||||
} catch (e: Exception) {
|
} catch (e: Throwable) {
|
||||||
ResultTestsData.putResult(instrumentation, uiDevice)
|
DescriptionFailureHandler.throwError(e)
|
||||||
throw AssertionError(e.message)
|
|
||||||
}
|
}
|
||||||
ResultTestsData.putResult(instrumentation, uiDevice)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun runWithGrade(grade: Int, testName: String, steps: TestContext<Unit>.() -> Unit) {
|
private companion object {
|
||||||
ResultTestsData.testGrade[testName] = grade
|
fun <LaunchActivity : Activity> getBuilder(
|
||||||
run(testName, steps)
|
clazz: Class<LaunchActivity>,
|
||||||
}
|
isEnabledCompose: Boolean,
|
||||||
|
): Kaspresso.Builder {
|
||||||
|
val builder = Kaspresso.Builder.simple {
|
||||||
|
kautomatorWaitForIdleSettings = KautomatorWaitForIdleSettings.boost()
|
||||||
|
beforeEachTest(override = true) { beforeEachAction(this, clazz) }
|
||||||
|
afterEachTest(override = true) { afterEachAction(this) }
|
||||||
|
}
|
||||||
|
builder.setupHandler()
|
||||||
|
if (isEnabledCompose) builder.addComposeSupport()
|
||||||
|
return builder
|
||||||
|
}
|
||||||
|
|
||||||
fun beforeWithGrade(grade: Int, testName: String, actions: BaseTestContext.() -> Unit): AfterTestSection<Unit, Unit> {
|
fun <LaunchActivity : Activity> Kaspresso.Builder.beforeEachAction(
|
||||||
ResultTestsData.testGrade[testName] = grade
|
context: BaseTestContext,
|
||||||
return before(testName, actions)
|
clazz: Class<LaunchActivity>,
|
||||||
|
) {
|
||||||
|
with(context.device) {
|
||||||
|
language.switchInApp(Locale.UK)
|
||||||
|
uiDevice.pressHome()
|
||||||
|
}
|
||||||
|
Thread.sleep(1_000)
|
||||||
|
ActivityScenario.launch(clazz)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Kaspresso.Builder.afterEachAction(
|
||||||
|
context: BaseTestContext,
|
||||||
|
) {
|
||||||
|
ResultTestsData.putResult(instrumentation)
|
||||||
|
context.device.uiDevice.pressHome()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Kaspresso.Builder.setupHandler() {
|
||||||
|
stepWatcherInterceptors.add(DescriptionFailureHandler)
|
||||||
|
testRunWatcherInterceptors.add(DescriptionFailureHandler)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
61
src/main/java/ru/samsung/test/core/core/DescriptionFailureHandler.kt
Normal file → Executable file
61
src/main/java/ru/samsung/test/core/core/DescriptionFailureHandler.kt
Normal file → Executable file
@@ -1,31 +1,42 @@
|
|||||||
package ru.samsung.test.core.core
|
package ru.samsung.test.core.core
|
||||||
|
|
||||||
import android.app.Instrumentation
|
import com.kaspersky.kaspresso.interceptors.watcher.testcase.StepWatcherInterceptor
|
||||||
import android.view.View
|
import com.kaspersky.kaspresso.interceptors.watcher.testcase.TestRunWatcherInterceptor
|
||||||
import androidx.test.espresso.FailureHandler
|
import com.kaspersky.kaspresso.testcases.models.info.StepInfo
|
||||||
import androidx.test.espresso.base.DefaultFailureHandler
|
import com.kaspersky.kaspresso.testcases.models.info.TestInfo
|
||||||
import org.hamcrest.Matcher
|
|
||||||
import kotlin.math.min
|
|
||||||
|
|
||||||
class DescriptionFailureHandler(instrumentation: Instrumentation) : FailureHandler {
|
object DescriptionFailureHandler : StepWatcherInterceptor, TestRunWatcherInterceptor {
|
||||||
var extraMessage = ""
|
private val outputLogs = mutableListOf<String>()
|
||||||
private var delegate: DefaultFailureHandler =
|
|
||||||
DefaultFailureHandler(instrumentation.targetContext)
|
|
||||||
|
|
||||||
override fun handle(error: Throwable?, viewMatcher: Matcher<View>?) {
|
override fun onTestStarted(testInfo: TestInfo) {
|
||||||
// Log anything you want here
|
outputLogs.clear()
|
||||||
if (error != null) {
|
|
||||||
val newError = Throwable(
|
|
||||||
|
|
||||||
"$extraMessage " + error.message?.substring(
|
|
||||||
0, min(
|
|
||||||
1_700, error.message?.length ?: 0
|
|
||||||
)
|
|
||||||
) + "...", error.cause
|
|
||||||
)
|
|
||||||
|
|
||||||
// Then delegate the error handling to the default handler which will throw an exception
|
|
||||||
delegate.handle(newError, viewMatcher)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun interceptAfterWithSuccess(stepInfo: StepInfo) {
|
||||||
|
super.interceptAfterWithSuccess(stepInfo)
|
||||||
|
outputLogs.add(stepInfo.description + ": OK")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun interceptAfterWithError(stepInfo: StepInfo, error: Throwable) {
|
||||||
|
super.interceptAfterWithError(stepInfo, error)
|
||||||
|
outputLogs.add(stepInfo.description + ": FAIL")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun throwError(error: Throwable): Nothing {
|
||||||
|
var message = buildString {
|
||||||
|
outputLogs.forEach { str ->
|
||||||
|
append(str)
|
||||||
|
append("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (message.length >= MAX_MESSAGE_LENGTH - MIN_ERROR_LENGTH) {
|
||||||
|
message = message.takeLast(MAX_MESSAGE_LENGTH - MIN_ERROR_LENGTH)
|
||||||
|
}
|
||||||
|
message += error.message?.take(MAX_MESSAGE_LENGTH - message.length) ?: ""
|
||||||
|
|
||||||
|
throw Throwable(message, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
private const val MAX_MESSAGE_LENGTH = 1_700
|
||||||
|
private const val MIN_ERROR_LENGTH = 100
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
package ru.samsung.test.core.core
|
|
||||||
|
|
||||||
import com.kaspersky.kaspresso.testcases.core.sections.MainTestSection
|
|
||||||
import com.kaspersky.kaspresso.testcases.core.testcontext.TestContext
|
|
||||||
|
|
||||||
fun MainTestSection<Unit, Unit>.runWithInit(grade: Int, steps: TestContext<Unit>.() -> Unit, testName: String) {
|
|
||||||
|
|
||||||
this.run(steps)
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package ru.samsung.test.core.core
|
|
||||||
|
|
||||||
import android.app.Instrumentation
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
|
||||||
import androidx.test.uiautomator.UiDevice
|
|
||||||
import com.kaspersky.kaspresso.interceptors.watcher.testcase.TestRunWatcherInterceptor
|
|
||||||
import com.kaspersky.kaspresso.testcases.models.info.TestInfo
|
|
||||||
import ru.samsung.test.core.utils.ResultTestsData
|
|
||||||
|
|
||||||
class ResultTestRunWatcherInterceptor: TestRunWatcherInterceptor {
|
|
||||||
override fun onTestStarted(testInfo: TestInfo) {
|
|
||||||
val grade: Int = ResultTestsData.testGrade[testInfo.testName] ?: 0
|
|
||||||
ResultTestsData.setupTest(grade)
|
|
||||||
super.onTestStarted(testInfo)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onMainSectionFinishedSuccess(testInfo: TestInfo) {
|
|
||||||
val grade: Int = ResultTestsData.testGrade[testInfo.testName] ?: 0
|
|
||||||
ResultTestsData.successTest(grade)
|
|
||||||
super.onMainSectionFinishedSuccess(testInfo)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onTestFinished(testInfo: TestInfo, success: Boolean) {
|
|
||||||
ResultTestsData.putResult(InstrumentationRegistry.getInstrumentation(), UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()))
|
|
||||||
super.onTestFinished(testInfo, success)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
0
src/main/java/ru/samsung/test/core/utils/EmptyRule.kt
Normal file → Executable file
0
src/main/java/ru/samsung/test/core/utils/EmptyRule.kt
Normal file → Executable file
8
src/main/java/ru/samsung/test/core/utils/ResultTestsData.kt
Normal file → Executable file
8
src/main/java/ru/samsung/test/core/utils/ResultTestsData.kt
Normal file → Executable file
@@ -12,7 +12,6 @@ object ResultTestsData {
|
|||||||
private var totalTests = 0
|
private var totalTests = 0
|
||||||
private var maxGrade = 0
|
private var maxGrade = 0
|
||||||
private var passTests = 0
|
private var passTests = 0
|
||||||
val testGrade: MutableMap<String, Int> = mutableMapOf()
|
|
||||||
|
|
||||||
fun setupTest(grade: Int) {
|
fun setupTest(grade: Int) {
|
||||||
totalTests++
|
totalTests++
|
||||||
@@ -24,13 +23,12 @@ object ResultTestsData {
|
|||||||
ResultTestsData.grade += grade
|
ResultTestsData.grade += grade
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun hasFailed() = totalTests != passTests
|
||||||
|
|
||||||
|
|
||||||
fun putResult(
|
fun putResult(
|
||||||
instrumentation: Instrumentation,
|
instrumentation: Instrumentation
|
||||||
uiDevice: UiDevice
|
|
||||||
) {
|
) {
|
||||||
uiDevice.pressHome()
|
|
||||||
|
|
||||||
val results = Bundle()
|
val results = Bundle()
|
||||||
results.putInt("passTests", passTests)
|
results.putInt("passTests", passTests)
|
||||||
results.putInt("totalTests", totalTests)
|
results.putInt("totalTests", totalTests)
|
||||||
|
|||||||
0
src/main/java/ru/samsung/test/core/utils/UiTextViewExtensions.kt
Normal file → Executable file
0
src/main/java/ru/samsung/test/core/utils/UiTextViewExtensions.kt
Normal file → Executable file
0
src/main/java/ru/samsung/test/core/utils/UiViewBuilderExtensions.kt
Normal file → Executable file
0
src/main/java/ru/samsung/test/core/utils/UiViewBuilderExtensions.kt
Normal file → Executable file
0
src/main/java/ru/samsung/test/core/utils/Utils.kt
Normal file → Executable file
0
src/main/java/ru/samsung/test/core/utils/Utils.kt
Normal file → Executable file
Reference in New Issue
Block a user