How to enter text in Jetpack compose TextField through UI tests?

AndroidDev

In Jetpack compose I have a TextField and I'm trying to write Espresso UI tests. I didn't find how I can enter text in the TextField, any ideas please?

        TextField(
            value = textState.value,
            modifier = Modifier.fillMaxWidth(),
            onValueChange = {
                textState.value = it
                apiServiceCall(textState.value.text)
            },
            keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences)
        )

@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()

@Test
fun enterTextAndMakeServiceCall() {
    ActivityScenario.launch(MainActivity::class.java)

    //TODO: Enter text inside the TextField
    composeTestRule.onNode(hasText(getString(R.string.result)))
}
jeprubio

I first set the testTag modifier on the composable I want to test:

const val MY_TEXTFIELD_TAG = "myTextFieldTag"

TextField(
    value = textState.value,
    modifier = Modifier.fillMaxWidth().testTag(MY_TEXTFIELD_TAG),
    onValueChange = {
        textState.value = it
    },
    keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences),
)

And then from your test you can set and check the value like this:

@Test
fun setAndCheckTheTextFieldValue() {
    ActivityScenario.launch(MainActivity::class.java)
    val resultText = "result"

    // Sets the TextField value
    composeTestRule.onNodeWithTag(MY_TEXTFIELD_TAG).performTextInput(resultText)

    // Asserts the TextField has the corresponding value
    composeTestRule.onNodeWithTag(MY_TEXTFIELD_TAG).assert(hasText(resultText))
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to know if Text is visible on Jetpack Compose?

From Dev

Aligning a resized TextField in Jetpack Compose

From Dev

How do I enter text in an alert textfield using Xcode UI Automation testing

From Java

Jetpack compose: Android Tests won't run

From Dev

How to obtain reference to TextField in UI Tests in Xcode 7

From Dev

How do I enter text in textfield with onkeyup/onfocus javascript?

From Dev

Scrolling a tableview for enter text in textfield?

From Dev

How to handle navigation in Jetpack Compose?

From Dev

Show a new line in a Text component in Jetpack Compose

From Dev

UI Design:How to set TextField text invisible while i am entering text into TextField

From Java

How can I get onTouchEvent in Jetpack Compose?

From Dev

How to add Image in Android using Jetpack Compose

From Dev

How to convert TextUnit to Dp in Jetpack Compose?

From Dev

How to invalidate a TextField in Material UI

From Dev

How to set Line Spacing in UI Text View (Empty text view) to enter message with line space

From Dev

How to enter text with ' into table?

From Dev

How to draw text through line and How to highlight the text on mouse enter using Html Canvas. Please help me

From Dev

QML TextField how to capitalize the text

From Dev

how to stop text overlapping in textfield

From Dev

How to set a text padding in a TextField?

From Dev

how to add minimum text in textfield

From Dev

How to set a text padding in a TextField?

From Dev

How can I achieve this Button Animation in Jetpack Compose?

From Dev

How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose?

From Dev

How to change the outline color of OutlinedTextField from jetpack compose?

From Dev

How can I achieve this Button Animation in Jetpack Compose?

From Java

Center composable in Jetpack compose

From Dev

ProvideEmphasis not found in jetpack compose

From Dev

ProvideEmphasis not found in jetpack compose

Related Related

  1. 1

    How to know if Text is visible on Jetpack Compose?

  2. 2

    Aligning a resized TextField in Jetpack Compose

  3. 3

    How do I enter text in an alert textfield using Xcode UI Automation testing

  4. 4

    Jetpack compose: Android Tests won't run

  5. 5

    How to obtain reference to TextField in UI Tests in Xcode 7

  6. 6

    How do I enter text in textfield with onkeyup/onfocus javascript?

  7. 7

    Scrolling a tableview for enter text in textfield?

  8. 8

    How to handle navigation in Jetpack Compose?

  9. 9

    Show a new line in a Text component in Jetpack Compose

  10. 10

    UI Design:How to set TextField text invisible while i am entering text into TextField

  11. 11

    How can I get onTouchEvent in Jetpack Compose?

  12. 12

    How to add Image in Android using Jetpack Compose

  13. 13

    How to convert TextUnit to Dp in Jetpack Compose?

  14. 14

    How to invalidate a TextField in Material UI

  15. 15

    How to set Line Spacing in UI Text View (Empty text view) to enter message with line space

  16. 16

    How to enter text with ' into table?

  17. 17

    How to draw text through line and How to highlight the text on mouse enter using Html Canvas. Please help me

  18. 18

    QML TextField how to capitalize the text

  19. 19

    how to stop text overlapping in textfield

  20. 20

    How to set a text padding in a TextField?

  21. 21

    how to add minimum text in textfield

  22. 22

    How to set a text padding in a TextField?

  23. 23

    How can I achieve this Button Animation in Jetpack Compose?

  24. 24

    How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose?

  25. 25

    How to change the outline color of OutlinedTextField from jetpack compose?

  26. 26

    How can I achieve this Button Animation in Jetpack Compose?

  27. 27

    Center composable in Jetpack compose

  28. 28

    ProvideEmphasis not found in jetpack compose

  29. 29

    ProvideEmphasis not found in jetpack compose

HotTag

Archive