https://kotlinlang.org logo
Title
b

Bradleycorn

11/19/2021, 7:40 PM
Testing problem … please help, this has been driving me nuts! I have a simple `TextField`:
TextField( value = myText,
    onValueChanged = { new -> myText = new },
    modifier = Modifier.testTag("MyInput"))
And I’m trying to set some text in the text field in a test:
composeTestRule.onNodeWithTag("MyInput").performTextInput("Test")
My test fails, and I get the error:
No input session started. Missing a focus?
What am I doing wrong here? Doing some digging into the testing source and debugging the nodes, it looks like the
TextField
node doesn’t get focused for some reason. I tried adding a
performClick()
before setting the text, but that didn’t help either. The node still has
focused =  false
… What am I missing?
l

Laura Kelly

11/19/2021, 9:10 PM
I'm not sure why your code doesn't work, but does
composeTestRule.onNode(hasSetTextAction()).performTextInput("Test")
work?
b

Bradleycorn

11/19/2021, 9:40 PM
hmm … thanks @Laura Kelly. If I create a composable that ONLY has a TextField, and use your suggestion, it does work. That at least gives me a starting point to build back up and see where things go wrong
🤦 I figured it out. What a rookie mistake …
j

Jesse Hill

11/19/2021, 10:00 PM
Do you mind sharing what the issue was? I remember encountering this error message but forget what I was doing wrong back then 😂
b

Bradleycorn

11/19/2021, 10:04 PM
I had a composable, let’s call it
MyForm
that rendered several `TextField`s and a
Button
, like this:
TextField()
TextField()
TextField()
Button()
I was getting lucky that it was working in my app, because
MyForm
was being called from a parent that had a `Column`:
@Composable fun MyScreen() {
   Column {
      Header()
      MyForm()
      Footer()
   }
}
But to test
MyForm
, it gets loaded on it’s own:
composeTestRule.setContent { MyForm() }
So in the test, all of those fields and the button are rendered on top of one another. In the end, the test can’t “focus” the first text field because it is underneath the button. 🤦 As soon as I put a
Column
IN
MyForm
, everything works like a champ
👍 2
Huge thanks to @Laura Kelly! Her hint put me on the right track and in building
MyForm
back up piece by piece, I found my mistake
👏 2
🆒 1
c

Colton Idle

11/20/2021, 3:12 AM
Oh hey its @Laura Kelly! your talks from droidcon helped our team in our last big feature we developed this year. 👋 and thanks for sharing!
:awesome: 1