Compare commits
4 Commits
dc0302d272
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b6291e4770 | |||
| 63010918ee | |||
| 36d60dbf39 | |||
| 67babd8e6e |
@@ -1,9 +1,24 @@
|
|||||||
plugins {
|
plugins {
|
||||||
androidApplication
|
kotlinAndroid
|
||||||
|
androidLibrary
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "ru.samsung.test.core"
|
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 {
|
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/>
|
||||||
122
src/main/java/ru/samsung/test/core/core/BaseTest.kt
Normal file → Executable file
122
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.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.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.innovationcampus.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>,
|
||||||
) : TestCase(kaspressoBuilder = Kaspresso.Builder.simple {
|
isEnabledCompose: Boolean = false,
|
||||||
kautomatorWaitForIdleSettings = KautomatorWaitForIdleSettings.boost()
|
private val skipAfterFirstFail: Boolean = true,
|
||||||
}.apply { addComposeSupport() }) {
|
) : TestCase(
|
||||||
@get:Rule
|
kaspressoBuilder = getBuilder(
|
||||||
val composeTestRule = createComposeRule()
|
clazz = clazz,
|
||||||
|
isEnabledCompose = isEnabledCompose,
|
||||||
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)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
ResultTestsData.putResult(instrumentation, uiDevice)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
63
src/main/java/ru/samsung/test/core/core/DescriptionFailureHandler.kt
Normal file → Executable file
63
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 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
|
||||||
}
|
}
|
||||||
|
|||||||
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.Activity
|
||||||
import android.app.Instrumentation
|
import android.app.Instrumentation
|
||||||
import android.app.Instrumentation.ActivityResult
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
|
||||||
import androidx.test.uiautomator.UiDevice
|
import androidx.test.uiautomator.UiDevice
|
||||||
|
|
||||||
object ResultTestsData {
|
object ResultTestsData {
|
||||||
@@ -25,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)
|
||||||
|
|||||||
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.google.common.truth.Truth.assertThat
|
||||||
import com.kaspersky.components.kautomator.component.text.UiTextViewAssertions
|
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
|
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 androidx.compose.ui.test.SemanticsNodeInteractionsProvider
|
||||||
import io.github.kakaocup.compose.node.element.ComposeScreen
|
import io.github.kakaocup.compose.node.element.ComposeScreen
|
||||||
|
|||||||
Reference in New Issue
Block a user