Skip to content

Commit 33cf79a

Browse files
authored
Issue 730 step on video (#731)
* ISSUE-730: Step toast
1 parent c678c7e commit 33cf79a

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.kaspersky.kaspresso.interceptors.watcher.testcase.impl.toast
2+
3+
import android.content.Context
4+
import android.os.Handler
5+
import android.widget.Toast
6+
import com.kaspersky.kaspresso.interceptors.watcher.testcase.StepWatcherInterceptor
7+
import com.kaspersky.kaspresso.testcases.models.info.StepInfo
8+
import org.jetbrains.annotations.ApiStatus
9+
10+
/**
11+
* Shows a toast with step info before each step execution.
12+
*/
13+
@ApiStatus.Experimental
14+
class ToastStepWatcherInterceptor(
15+
private val context: Context,
16+
private val toastContentProvider: (StepInfo) -> CharSequence = ToastContentProviders.description,
17+
) : StepWatcherInterceptor {
18+
override fun interceptBefore(stepInfo: StepInfo) {
19+
toast(toastContentProvider.invoke(stepInfo))
20+
}
21+
22+
private fun toast(content: CharSequence) {
23+
Handler(context.mainLooper).post {
24+
Toast.makeText(context.applicationContext, content, Toast.LENGTH_SHORT).show()
25+
}
26+
}
27+
28+
/**
29+
* A set of default implementations of a toast content provider
30+
*/
31+
object ToastContentProviders {
32+
val description: (StepInfo) -> CharSequence = { stepInfo ->
33+
stepInfo.description
34+
}
35+
val number: (StepInfo) -> CharSequence = { stepInfo ->
36+
"Step #${getNumber(stepInfo)}"
37+
}
38+
val fullInfo: (StepInfo) -> CharSequence = { stepInfo ->
39+
"${getNumber(stepInfo)} ${stepInfo.description}"
40+
}
41+
42+
private fun getNumber(stepInfo: StepInfo) = stepInfo.number ?: stepInfo.ordinal.toString()
43+
}
44+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.kaspersky.kaspressample
2+
3+
import android.Manifest
4+
import androidx.test.ext.junit.rules.activityScenarioRule
5+
import androidx.test.rule.GrantPermissionRule
6+
import com.kaspersky.kaspressample.screen.MainScreen
7+
import com.kaspersky.kaspressample.screen.SimpleScreen
8+
import com.kaspersky.kaspressample.simple_tests.CheckEditScenario
9+
import com.kaspersky.kaspresso.interceptors.watcher.testcase.impl.toast.ToastStepWatcherInterceptor
10+
import com.kaspersky.kaspresso.kaspresso.Kaspresso
11+
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
12+
import org.junit.Rule
13+
import org.junit.Test
14+
15+
/**
16+
* This test shows how to use a ToastStepWatcherInterceptor that shows a step name in a toast. The toast will appear before each step execution.
17+
* toastContentProvider parameter is a lambda that takes a [com.kaspersky.kaspresso.testcases.models.info.StepInfo] and returns a CharSequence.
18+
* You could provide your own implementation of the content provider or use one of the predefined lambdas in the [ToastStepWatcherInterceptor.ToastContentProviders] object.
19+
*/
20+
class StepToastSample : TestCase(kaspressoBuilder = Kaspresso.Builder.simple().apply {
21+
stepWatcherInterceptors.add(ToastStepWatcherInterceptor(
22+
instrumentation.targetContext,
23+
toastContentProvider = ToastStepWatcherInterceptor.ToastContentProviders.description,
24+
))
25+
}) {
26+
27+
@get:Rule
28+
val runtimePermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
29+
Manifest.permission.WRITE_EXTERNAL_STORAGE,
30+
Manifest.permission.READ_EXTERNAL_STORAGE
31+
)
32+
33+
@get:Rule
34+
val activityRule = activityScenarioRule<MainActivity>()
35+
36+
@Test
37+
fun test() = run {
38+
step("Open Simple Screen") {
39+
device.screenshots.take("Additional_screenshot")
40+
MainScreen {
41+
simpleButton {
42+
isVisible()
43+
}
44+
}
45+
46+
step("Sub-step") {
47+
MainScreen {
48+
simpleButton {
49+
click()
50+
}
51+
}
52+
}
53+
}
54+
55+
step("Click button_1 and check button_2") {
56+
SimpleScreen {
57+
button1 {
58+
click()
59+
}
60+
button2 {
61+
isVisible()
62+
}
63+
}
64+
}
65+
66+
step("Click button_2 and check edit") {
67+
SimpleScreen {
68+
button2 {
69+
click()
70+
}
71+
edit {
72+
flakySafely(timeoutMs = 7000) { isVisible() }
73+
hasText(R.string.simple_fragment_text_edittext)
74+
}
75+
}
76+
}
77+
78+
step("Check all possibilities of edit") {
79+
scenario(
80+
CheckEditScenario()
81+
)
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)