KasperskyLab / Kaspresso

Android UI test framework
https://kasperskylab.github.io/Kaspresso/
Apache License 2.0
1.81k stars 153 forks source link

Feature request: Make Kaspresso robolectric-compatible (Project Nitrogen) #242

Closed sergio-sastre closed 3 years ago

sergio-sastre commented 3 years ago

Hi there! I was trying to write Kaspresso tests that run on the JVM with Robolectric, as stated by Google in this video

However, I was getting the following error:

java.lang.NullPointerException
    at androidx.test.uiautomator.QueryController.<init>(QueryController.java:95)
    at androidx.test.uiautomator.UiDevice.<init>(UiDevice.java:109)
    at androidx.test.uiautomator.UiDevice.getInstance(UiDevice.java:261)
    at com.kaspersky.kaspresso.kaspresso.Kaspresso$Builder.<init>(Kaspresso.kt:297)
    at com.kaspersky.kaspresso.kaspresso.Kaspresso$Builder$Companion.simple(Kaspresso.kt:215)
    ...

That is because Robolectric is just compatible with Espresso and not with uiAutomator, and the TestCase class initialize both, Espresso through Kaspresso and UiAutomator through Kautomator.

Therefore I forked Kaspresso and created a NitrogenTestCase() that only initializes Kaspresso, leaving Kautomator aside. This also means, no adb-server possible either, as well as some other interceptors e.g. the "Close System Dialog interceptor" I successfully made the error disappear and the tests I wrote ran green as JVM tests as well as Instrumentation tests :) It enables to make tests that inherit from NitrogenTestCase() to run on both, JVM and emulators/devices.

Moreover, the Autoscroll and flakySafety interceptors still work on the JVM: running with pure Kakao, one test failed because the button was not visible and needed to scroll to find it, and another one also failed because the view appears after 7-seconds delay, while both succeeded when running with Kasspreso.NitrogenTestCase()

However, there are some issues on the Robolectric-Espresso side to keep in mind, that would also not work with NitrogenTestCase() while running on the JVM: 1) Robolectric-Espresso does not support Idling resources yet 2) Robolectric-Espresso will not support tests that start new activities (i.e. activity jumping). It means, such integration tests will successfully run on devices/emulators and fail on the JVM.

If would gladly create a pull request and add the docu to the wiki if you find this feature (as me) interesting!

artkoenig commented 3 years ago

@sergio-sastre can you please fix the "Roboelectric" to "Robolectric" for matching the keyword search results?