Compare commits
4 Commits
dc0302d272
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b6291e4770 | |||
| 63010918ee | |||
| 36d60dbf39 | |||
| 67babd8e6e |
@@ -1,9 +1,24 @@
|
||||
plugins {
|
||||
androidApplication
|
||||
kotlinAndroid
|
||||
androidLibrary
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "ru.samsung.test.core"
|
||||
compileSdk = Version.Android.Sdk.compile
|
||||
|
||||
defaultConfig {
|
||||
minSdk = Version.Android.Sdk.min
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = Version.Kotlin.javaSource
|
||||
targetCompatibility = Version.Kotlin.javaSource
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = Version.Kotlin.jvmTarget
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
2
src/main/AndroidManifest.xml
Normal file
2
src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest/>
|
||||
124
src/main/java/ru/samsung/test/core/core/BaseTest.kt
Normal file → Executable file
124
src/main/java/ru/samsung/test/core/core/BaseTest.kt
Normal file → Executable file
@@ -1,83 +1,85 @@
|
||||
package ru.innovationcampus.test.core.core
|
||||
package ru.samsung.test.core.core
|
||||
|
||||
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.espresso.Espresso
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.uiautomator.UiDevice
|
||||
import com.kaspersky.components.composesupport.config.addComposeSupport
|
||||
import com.kaspersky.kaspresso.idlewaiting.KautomatorWaitForIdleSettings
|
||||
import com.kaspersky.kaspresso.kaspresso.Kaspresso
|
||||
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
|
||||
import com.kaspersky.kaspresso.testcases.core.testcontext.BaseTestContext
|
||||
import com.kaspersky.kaspresso.testcases.core.testcontext.TestContext
|
||||
import java.util.Locale
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.FixMethodOrder
|
||||
import org.junit.Rule
|
||||
import org.junit.runners.MethodSorters
|
||||
import ru.innovationcampus.test.core.utils.ResultTestsData
|
||||
import ru.samsung.test.core.utils.ResultTestsData
|
||||
|
||||
/**
|
||||
* Базовый класс тестов, который подключается для запуска основных
|
||||
*
|
||||
* @param clazz класс активити, который будет запущен при старте.
|
||||
* Эта активити должна иметь соответствующий флаг в манифесте и
|
||||
*/
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
open class BaseTest<LaunchActivity : Activity>(
|
||||
private val clazz: Class<LaunchActivity>
|
||||
) : TestCase(kaspressoBuilder = Kaspresso.Builder.simple {
|
||||
kautomatorWaitForIdleSettings = KautomatorWaitForIdleSettings.boost()
|
||||
}.apply { addComposeSupport() }) {
|
||||
@get:Rule
|
||||
val composeTestRule = createComposeRule()
|
||||
|
||||
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()
|
||||
}
|
||||
clazz: Class<LaunchActivity>,
|
||||
isEnabledCompose: Boolean = false,
|
||||
private val skipAfterFirstFail: Boolean = true,
|
||||
) : TestCase(
|
||||
kaspressoBuilder = getBuilder(
|
||||
clazz = clazz,
|
||||
isEnabledCompose = isEnabledCompose,
|
||||
)
|
||||
) {
|
||||
|
||||
fun runWithInit(grade: Int, test: TestContext<Unit>.() -> Unit) = run {
|
||||
val hasFailed = ResultTestsData.hasFailed()
|
||||
ResultTestsData.setupTest(grade)
|
||||
if (hasFailed && skipAfterFirstFail) throw Throwable("SKIPPED. Prev test failed")
|
||||
try {
|
||||
test.invoke(this)
|
||||
ResultTestsData.successTest(grade)
|
||||
} catch (e: Exception) {
|
||||
ResultTestsData.putResult(instrumentation, uiDevice)
|
||||
throw AssertionError(e.message)
|
||||
}
|
||||
ResultTestsData.putResult(instrumentation, uiDevice)
|
||||
} catch (e: Throwable) {
|
||||
DescriptionFailureHandler.throwError(e)
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
fun <LaunchActivity : Activity> getBuilder(
|
||||
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 <LaunchActivity : Activity> Kaspresso.Builder.beforeEachAction(
|
||||
context: BaseTestContext,
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
57
src/main/java/ru/samsung/test/core/core/DescriptionFailureHandler.kt
Normal file → Executable file
57
src/main/java/ru/samsung/test/core/core/DescriptionFailureHandler.kt
Normal file → Executable file
@@ -1,31 +1,42 @@
|
||||
package ru.innovationcampus.test.core.core
|
||||
package ru.samsung.test.core.core
|
||||
|
||||
import android.app.Instrumentation
|
||||
import android.view.View
|
||||
import androidx.test.espresso.FailureHandler
|
||||
import androidx.test.espresso.base.DefaultFailureHandler
|
||||
import org.hamcrest.Matcher
|
||||
import kotlin.math.min
|
||||
import com.kaspersky.kaspresso.interceptors.watcher.testcase.StepWatcherInterceptor
|
||||
import com.kaspersky.kaspresso.interceptors.watcher.testcase.TestRunWatcherInterceptor
|
||||
import com.kaspersky.kaspresso.testcases.models.info.StepInfo
|
||||
import com.kaspersky.kaspresso.testcases.models.info.TestInfo
|
||||
|
||||
class DescriptionFailureHandler(instrumentation: Instrumentation) : FailureHandler {
|
||||
var extraMessage = ""
|
||||
private var delegate: DefaultFailureHandler =
|
||||
DefaultFailureHandler(instrumentation.targetContext)
|
||||
object DescriptionFailureHandler : StepWatcherInterceptor, TestRunWatcherInterceptor {
|
||||
private val outputLogs = mutableListOf<String>()
|
||||
|
||||
override fun handle(error: Throwable?, viewMatcher: Matcher<View>?) {
|
||||
// Log anything you want here
|
||||
if (error != null) {
|
||||
val newError = Throwable(
|
||||
override fun onTestStarted(testInfo: TestInfo) {
|
||||
outputLogs.clear()
|
||||
}
|
||||
|
||||
"$extraMessage " + error.message?.substring(
|
||||
0, min(
|
||||
1_700, error.message?.length ?: 0
|
||||
)
|
||||
) + "...", error.cause
|
||||
)
|
||||
override fun interceptAfterWithSuccess(stepInfo: StepInfo) {
|
||||
super.interceptAfterWithSuccess(stepInfo)
|
||||
outputLogs.add(stepInfo.description + ": OK")
|
||||
}
|
||||
|
||||
// Then delegate the error handling to the default handler which will throw an exception
|
||||
delegate.handle(newError, viewMatcher)
|
||||
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
|
||||
}
|
||||
|
||||
6
src/main/java/ru/samsung/test/core/utils/EmptyRule.kt
Executable file
6
src/main/java/ru/samsung/test/core/utils/EmptyRule.kt
Executable file
@@ -0,0 +1,6 @@
|
||||
package ru.samsung.test.core.utils
|
||||
|
||||
import org.junit.rules.TestRule
|
||||
import org.junit.rules.TestWatcher
|
||||
|
||||
class EmptyRule : TestWatcher()
|
||||
11
src/main/java/ru/samsung/test/core/utils/ResultTestsData.kt
Normal file → Executable file
11
src/main/java/ru/samsung/test/core/utils/ResultTestsData.kt
Normal file → Executable file
@@ -1,12 +1,10 @@
|
||||
package ru.innovationcampus.test.core.utils
|
||||
package ru.samsung.test.core.utils
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Instrumentation
|
||||
import android.app.Instrumentation.ActivityResult
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.uiautomator.UiDevice
|
||||
|
||||
object ResultTestsData {
|
||||
@@ -25,13 +23,12 @@ object ResultTestsData {
|
||||
ResultTestsData.grade += grade
|
||||
}
|
||||
|
||||
fun hasFailed() = totalTests != passTests
|
||||
|
||||
|
||||
fun putResult(
|
||||
instrumentation: Instrumentation,
|
||||
uiDevice: UiDevice
|
||||
instrumentation: Instrumentation
|
||||
) {
|
||||
uiDevice.pressHome()
|
||||
|
||||
val results = Bundle()
|
||||
results.putInt("passTests", passTests)
|
||||
results.putInt("totalTests", totalTests)
|
||||
|
||||
2
src/main/java/ru/samsung/test/core/utils/UiTextViewExtensions.kt
Normal file → Executable file
2
src/main/java/ru/samsung/test/core/utils/UiTextViewExtensions.kt
Normal file → Executable file
@@ -1,4 +1,4 @@
|
||||
package ru.innovationcampus.test.core.utils
|
||||
package ru.samsung.test.core.utils
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.kaspersky.components.kautomator.component.text.UiTextViewAssertions
|
||||
|
||||
2
src/main/java/ru/samsung/test/core/utils/UiViewBuilderExtensions.kt
Normal file → Executable file
2
src/main/java/ru/samsung/test/core/utils/UiViewBuilderExtensions.kt
Normal file → Executable file
@@ -1,4 +1,4 @@
|
||||
package ru.innovationcampus.test.core.utils
|
||||
package ru.samsung.test.core.utils
|
||||
|
||||
import com.kaspersky.components.kautomator.component.common.builders.UiViewBuilder
|
||||
|
||||
|
||||
2
src/main/java/ru/samsung/test/core/utils/Utils.kt
Normal file → Executable file
2
src/main/java/ru/samsung/test/core/utils/Utils.kt
Normal file → Executable file
@@ -1,4 +1,4 @@
|
||||
package ru.innovationcampus.test.core.utils
|
||||
package ru.samsung.test.core.utils
|
||||
|
||||
import androidx.compose.ui.test.SemanticsNodeInteractionsProvider
|
||||
import io.github.kakaocup.compose.node.element.ComposeScreen
|
||||
|
||||
Reference in New Issue
Block a user