-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBaseTests.kt
More file actions
97 lines (82 loc) · 2.87 KB
/
BaseTests.kt
File metadata and controls
97 lines (82 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.rootstrap.android.utils
import android.app.Activity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.clearText
import androidx.test.espresso.action.ViewActions.closeSoftKeyboard
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
import androidx.test.runner.lifecycle.Stage
import com.rootstrap.data.managers.session.SessionManager
import com.rootstrap.data.dto.response.UserDTO
import com.rootstrap.data.api.ApiServiceFactory
import dagger.hilt.android.testing.HiltAndroidRule
import okhttp3.mockwebserver.Dispatcher
import org.junit.Rule
import org.junit.runner.RunWith
import javax.inject.Inject
@RunWith(AndroidJUnit4ClassRunner::class)
open class BaseTests {
@Inject lateinit var sessionManager: SessionManager
var mockServer: MockServer = MockServer
@get:Rule
var hiltRule = HiltAndroidRule(this)
open fun setServerDispatch(dispatcher: Dispatcher) {
mockServer.server().dispatcher = dispatcher
}
open fun before() {
mockServer.startServer()
ApiServiceFactory.URL_API = mockServer.server().url("/").toString()
hiltRule.inject()
}
open fun after() {
mockServer.stopServer()
}
open fun testUser() = UserDTO(
"9032",
"user123@mail.com",
"Richard",
"Richard",
"99090909",
"asdasdasdasda",
"Richard"
)
open fun scrollAndTypeText(id: Int, text: String) {
onView(withId(id)).perform(
scrollTo(),
click(),
clearText(),
typeText(text),
closeSoftKeyboard()
)
}
open fun typeText(id: Int, text: String) {
onView(withId(id)).perform(click(), clearText(), typeText(text), closeSoftKeyboard())
}
open fun scrollAndPerformClick(viewId: Int) {
onView(withId(viewId)).perform(scrollTo(), click())
}
open fun performClick(viewId: Int) {
onView(withId(viewId)).perform(click())
}
open fun stringMatches(viewId: Int, value: String) {
onView(withId(viewId)).check(
ViewAssertions.matches(
ViewMatchers.withText(
value
)
)
)
}
open fun currentActivity(): Activity {
// Get the activity that currently started
val activities =
ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED)
return activities.first()
}
}