-
-
Save BorisOsipov/36d6ee3237ae5591cea819dfcdbb2489 to your computer and use it in GitHub Desktop.
Additional helpers methods for kakao/kaspresso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View | |
import android.webkit.WebView | |
import android.widget.CheckedTextView | |
import android.widget.TextView | |
import androidx.test.espresso.ViewInteraction | |
import androidx.test.espresso.assertion.ViewAssertions | |
import com.agoda.kakao.common.views.KBaseView | |
import com.agoda.kakao.common.views.KView | |
import com.agoda.kakao.screen.Screen | |
import com.agoda.kakao.text.KTextView | |
import io.reactivex.Flowable | |
import io.reactivex.android.schedulers.AndroidSchedulers | |
import org.hamcrest.Description | |
import org.hamcrest.TypeSafeMatcher | |
class Capture<T : View>(val klass: Class<T>) : TypeSafeMatcher<View>(View::class.java) { | |
companion object { | |
inline operator fun <reified T : View> invoke() = Capture(T::class.java) | |
fun getText(textView: KTextView): String { | |
return getText(textView.view.interaction) | |
} | |
fun getText(view: ViewInteraction): String { | |
val capture = Capture<TextView>() | |
view.check(ViewAssertions.matches(capture)) | |
return capture.view?.text?.toString() ?: "" | |
} | |
fun isChecked(textView: KTextView): Boolean? { | |
return isChecked(textView.view.interaction) | |
} | |
fun isChecked(view: ViewInteraction): Boolean? { | |
val capture = Capture<TextView>() | |
view.check(ViewAssertions.matches(capture)) | |
return (capture.view as? CheckedTextView)?.isChecked | |
} | |
fun getCoordinates(textView: KTextView): Pair<Int, Int> { | |
return getCoordinates(textView.view.interaction) | |
} | |
fun getCoordinates(view: ViewInteraction): Pair<Int, Int> { | |
val capture = Capture<TextView>() | |
view.check(ViewAssertions.matches(capture)) | |
val screenPos = IntArray(2) | |
capture.view?.getLocationOnScreen(screenPos) | |
return screenPos[0] to screenPos[1] | |
} | |
fun getContentDescription(anyView: KView, attempt: Int = 0): String { | |
return getContentDescription(anyView.view.interaction, attempt) | |
} | |
fun getContentDescription(view: ViewInteraction, attempt: Int = 0): String { | |
var result = "" | |
for (i in 0..attempt) { | |
val capture = Capture<View>() | |
view.check(ViewAssertions.matches(capture)) | |
result = capture.view?.contentDescription?.toString() ?: "" | |
Screen.idle() | |
if (result.isNotEmpty()) { return result } | |
} | |
return result | |
} | |
fun getViewWidth(view: ViewInteraction): Int { | |
val capture = Capture<View>() | |
view.check(ViewAssertions.matches(capture)) | |
return capture.view?.width ?: -1 | |
} | |
fun getViewHeight(view: ViewInteraction): Int { | |
val capture = Capture<View>() | |
view.check(ViewAssertions.matches(capture)) | |
return capture.view?.height ?: -1 | |
} | |
fun getViewWidth(view: KBaseView<*>): Int { | |
return getViewWidth(view.view.interaction) | |
} | |
fun getViewHeight(view: KBaseView<*>): Int { | |
return getViewHeight(view.view.interaction) | |
} | |
fun getUrl(webView: KView): String? { | |
var urlToReturn: String? = null | |
val capture = Capture<WebView>() | |
webView.view.check(ViewAssertions.matches(capture)) | |
Flowable.just(capture).map { | |
it.view?.url ?: "null" | |
}.subscribeOn(AndroidSchedulers.mainThread()).blockingSubscribe { | |
urlToReturn = it | |
} | |
return urlToReturn | |
} | |
} | |
var view: T? = null | |
override fun describeTo(desc: Description) { | |
} | |
override fun matchesSafely(v: View): Boolean { | |
if (!klass.isAssignableFrom(v.javaClass)) { | |
return false | |
} | |
this.view = v as T | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment