-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #242 - Add support to write Kaspresso-like tests that run on the JVM with Robolectric #243
Conversation
… that can run on the JVM and on a device/emulator
Wow, great job! Thank you for contribution. |
Hi @sergio-sastre! So, my thoughts:
|
@matzuk Regarding point 4, no real time limits on my side. |
Hi there! First of all, enabling tests to also run on the JVM would look like this: class SharedTest : TestCase(Kaspresso.Builder.simple(sharedTest = true)){} Coming back to the solution, the main problem was the hard dependency to Automator in Kaspresso, namely private val uiDevice = UiDevice.getInstance(instrumentation) as well the one in the Kaspresso constructor: data class Kaspresso(
...
internal val device: Device,
...
) I solved it by first removing object KautomatorConfigResolver {
fun build(sharedTest: Boolean): KautomatorConfig =
when (sharedTest) {
true -> KautomatorOnJVMConfig()
false -> KautomatorOnDeviceConfig()
}
} class KautomatorOnDeviceConfig : KautomatorConfig {
override fun getAdbServer(libLogger: UiTestLogger): AdbServer =
AdbServerImpl(LogLevel.WARN, libLogger)
override fun getPermissions(libLogger: UiTestLogger): Permissions =
PermissionsImpl(libLogger, getUiDevice())
override fun getNetwork(adbServer: AdbServer, libLogger: UiTestLogger): Network =
NetworkImpl(getInstrumentation().targetContext, adbServer, libLogger)
...
private fun getUiDevice() : UiDevice = UiDevice.getInstance(getInstrumentation())
} and in Robolectric those methods return empty implementations of the objects (they are not supported by Robolectric anyway, since are UIAutomator-dependent) class KautomatorOnJVMConfig : KautomatorConfig {
override fun getAdbServer(libLogger: UiTestLogger): AdbServer = object: AdbServer{}
override fun getPermissions(libLogger: UiTestLogger): Permissions = object: Permissions{}
override fun getNetwork(adbServer: AdbServer, libLogger: UiTestLogger): Network = object: Network{}
...
} and in the @Suppress("detekt.ComplexMethod")
private fun postInitVariables(sharedTest: Boolean) {
val kautomatorConfig = KautomatorConfigResolver.build(sharedTest)
if (!::libLogger.isInitialized) libLogger = UiTestLoggerImpl(DEFAULT_LIB_LOGGER_TAG)
...
if (!::adbServer.isInitialized) adbServer = kautomatorConfig.getAdbServer(libLogger)
if (!::network.isInitialized) network = kautomatorConfig.getNetwork(adbServer, libLogger)
...
} Let me know your thoughts on this ;) |
Hi @sergio-sastre! |
@sergio-sastre Hi! |
Hi @sergio-sastre! |
Hi @matzuk! Wrapping UiDevice (the direct UiAutomator class that causes the problem) in an interface would solve the problem and help catch and throw the desired exceptions to warn the user about the incompatibility of the running test with Robolectric. I believe that would be a pretty nice solution, and seems feasible. Regarding the code duplication in |
Hi @sergio-sastre! About |
Hi @matzuk! |
merged in #286 |
No description provided.