Testing problem … please help, this has been drivi...
# compose
b
Testing problem … please help, this has been driving me nuts! I have a simple `TextField`:
Copy code
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:
Copy code
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
I'm not sure why your code doesn't work, but does
Copy code
composeTestRule.onNode(hasSetTextAction()).performTextInput("Test")
work?
b
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
Do you mind sharing what the issue was? I remember encountering this error message but forget what I was doing wrong back then 😂
b
I had a composable, let’s call it
MyForm
that rendered several `TextField`s and a
Button
, like this:
Copy code
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`:
Copy code
@Composable fun MyScreen() {
   Column {
      Header()
      MyForm()
      Footer()
   }
}
But to test
MyForm
, it gets loaded on it’s own:
Copy code
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
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!
👌 1