{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}
class ViewInteraction
Provides the primary interface for test authors to perform actions or asserts on views.
Each interaction is associated with a view identified by a view matcher. All view actions and asserts are performed on the UI thread (thus ensuring sequential execution). The same goes for retrieval of views (this is done to ensure that view state is "fresh" prior to execution of each operation).
Public constructors |
|
|---|---|
Public functions |
|
|---|---|
ViewInteraction! |
check(viewAssert: ViewAssertion!)Checks the given |
ViewInteraction! |
Makes this ViewInteraction scoped to the root selected by the given root matcher. |
ViewInteraction! |
Removes the need of waiting for an Activity before performing a ViewAction/ViewAssertion |
ViewInteraction! |
perform(viewActions: Array<ViewAction!>!)Performs the given action(s) on the view selected by the current view matcher. |
ViewInteraction! |
withFailureHandler(failureHandler: FailureHandler!)Replaces the default failure handler (@see Espresso.setFailureHandler) with a custom FailureHandler for this particular interaction. |
Extension functions |
|
|---|---|
@ExperimentalTestApi @RequiresApi(value = 16) Bitmap |
Captures an image of the matching view into a {@link Bitmap}. |
fun check(viewAssert: ViewAssertion!): ViewInteraction!
Checks the given ViewAssertion on the view selected by the current view matcher.
| Parameters | |
|---|---|
viewAssert: ViewAssertion! |
the assertion to check. |
| Returns | |
|---|---|
ViewInteraction! |
this interaction for further perform/verification calls. |
| Throws | |
|---|---|
java.lang.RuntimeException: java.lang.RuntimeException |
when being invoked on the main thread. |
fun inRoot(rootMatcher: Matcher<Root!>!): ViewInteraction!
Makes this ViewInteraction scoped to the root selected by the given root matcher.
fun noActivity(): ViewInteraction!
Removes the need of waiting for an Activity before performing a ViewAction/ViewAssertion
fun perform(viewActions: Array<ViewAction!>!): ViewInteraction!
Performs the given action(s) on the view selected by the current view matcher. If more than one action is provided, actions are executed in the order provided with precondition checks running prior to each action.
If the test argument `--enable_testflow_gallery` is present, TestFlowVisualizer captures data for each of the interactions and generates an output artifact for the test run. NOTE, this is an experimental feature.
| Parameters | |
|---|---|
viewActions: Array<ViewAction!>! |
one or more actions to execute. |
| Returns | |
|---|---|
ViewInteraction! |
this interaction for further perform/verification calls. |
| Throws | |
|---|---|
java.lang.RuntimeException: java.lang.RuntimeException |
when being invoked on the main thread. |
fun withFailureHandler(failureHandler: FailureHandler!): ViewInteraction!
Replaces the default failure handler (@see Espresso.setFailureHandler) with a custom FailureHandler for this particular interaction.
| Parameters | |
|---|---|
failureHandler: FailureHandler! |
a non-null FailureHandler to use to report failures. |
| Returns | |
|---|---|
ViewInteraction! |
this interaction for further perform/verification calls. |
@ExperimentalTestApi
@RequiresApi(value = 16)
fun ViewInteraction.captureToBitmap(): Bitmap
Captures an image of the matching view into a {@link Bitmap}.
For devices below {@link Build.VERSION_CODES#O} (or if the view's window cannot be determined), the image is obtained using {@link View#draw}. Otherwise, {@link PixelCopy} is used.
This method will also enable {@link HardwareRendererCompat#setDrawingEnabled(boolean)} if required.
This API is currently experimental and subject to change or removal.