XCUIElement#typeText fails in simple case

Hi,


I am observing a super strange behavior with XCUITest on Xcode 11.1. I have just a storyboard with two UITextFields both with an accessibility indentifier set. I want to type text in both of them:


class ExampleUITests: XCTestCase {
    
    func testTypingInTextField() {
        let app = XCUIApplication()
        app.launch()

        let field1 = app.textFields["Field1"]
        field1.tap()
        field1.typeText("foo")
        
        let field2 = app.textFields["Field2"]
        field2.tap()
        field2.typeText("bar")
    }

}


Typing into the second field fails with the following error with the cursor blinking in the second text field:


Failed to synthesize event: Neither element nor any descendant has keyboard focus. Event dispatch snapshot: TextField, label: 'Field2'
Element debug description:
Attributes: TextField, {{10.0, 72.0}, {355.0, 34.0}}, label: 'Field2'
Element subtree:
 →TextField, 0x600000cde760, {{10.0, 72.0}, {355.0, 34.0}}, label: 'Field2'
Path to element:
 →Application, 0x600000cddf80, pid: 50581, label: 'CO2Rechner'
  ↳Window (Main), 0x600000cddea0, {{0.0, 0.0}, {375.0, 667.0}}
   ↳Other, 0x600000cddce0, {{0.0, 0.0}, {375.0, 667.0}}
    ↳Other, 0x600000cdddc0, {{0.0, 0.0}, {375.0, 667.0}}
     ↳Other, 0x600000cdf020, {{0.0, 0.0}, {375.0, 667.0}}
      ↳Other, 0x600000cde920, {{10.0, 30.0}, {355.0, 76.0}}
       ↳TextField, 0x600000cde760, {{10.0, 72.0}, {355.0, 34.0}}, label: 'Field2'
Query chain:
 →Find: Target Application 'com.example.CO2Rechner'
  Output: {
    Application, pid: 50581, label: 'CO2Rechner'
  }
  ︎Find: Descendants matching type TextField
    Output: {
      TextField, {{10.0, 30.0}, {355.0, 34.0}}, label: 'Field1', value: foo
      TextField, {{10.0, 72.0}, {355.0, 34.0}}, label: 'Field2'
    }
    ︎Find: Elements matching predicate '"Field2" IN identifiers'
      Output: {
        TextField, {{10.0, 72.0}, {355.0, 34.0}}, label: 'Field2'
      }


If I remove the code to type into field1 it works.


I put a simple example project here: https://github.com/ralfebert/CO2Rechner/archive/bug_textfield_uitest.zip


What do I miss here?


Greetings,


Ralf

Accepted Reply

"Hardware » Erase all Content and Settings" in the simulator fixed the problem.

Probably some leftover from the Betas.

Replies

"Hardware » Erase all Content and Settings" in the simulator fixed the problem.

Probably some leftover from the Betas.