Interceptors allow you to inject logic into the call chain Kautomator -> UI Automator. You can use them for logging or to completely override UiAssertion or UiAction calls.
Interceptor Levels
Interceptors can be provided at three levels:
- Kautomator runtime: Global interceptors.
- UiScreen level: Interceptors active for all elements within a specific screen.
- UiView level: Interceptors for a specific UI element instance.
Execution Order
When a function is called, Kautomator aggregates interceptors and executes them in this order:
UiView interceptor $\rightarrow$ Active Screens interceptors $\rightarrow$ Kautomator interceptor.
Overriding Calls
An interceptor can stop the chain and prevent the actual UI Automator call by setting isOverride = true. If you override, you are responsible for manually executing the UI Automator call if needed.
// Example: Overriding at the View level
myView {
intercept {
onPerform(true) { uiInteraction, uiAction ->
// Kautomator runtime interceptors will NOT be called
// You must manually call the interaction
uiInteraction.perform(uiAction)
}
}
}
class SomeTest {
@Before
fun setup() {
KautomatorConfigurator {
intercept {
onUiInteraction {
onPerform { uiInteraction, uiAction ->
testLogger.i("KautomatorIntercept", "interaction=$uiInteraction, action=$uiAction")
}
}
}
}
}
@Test
fun test() {
MyScreen {
intercept {
onUiInteraction {
onCheck { uiInteraction, uiAssert ->
testLogger.i("KautomatorIntercept", "interaction=$uiInteraction, assert=$uiAssert")
}
}
}
myView {
intercept {
onPerform(true) { uiInteraction, uiAction ->
Log.d("KAUTOMATOR_VIEW", "$uiInteraction performs $uiAction")
uiInteraction.perform(uiAction)
}
}
}
}
}
}