Ralf Ebert
Ralf Ebert

Reputation: 4092

XCUIElement#typeText fails for two text fields

I am observing a 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 uploaded an example project here

What do I miss here?

Upvotes: 0

Views: 1336

Answers (2)

brindy
brindy

Reputation: 4695

This is caused by toggling the software keyboard.

If your test fails launch your app, tap a text field and note the lack of software keyboard. Select I/O > Keyboard > Toggle Software Keyboard from the simulator menu, or Cmd-K with the simulator in focus and the keyboard should then show. Re-run your test and it should not have this problem any more.

Erasing the simulator will also fix it because the software keyboard is enabled by default.

Upvotes: 0

Ralf Ebert
Ralf Ebert

Reputation: 4092

"Hardware » Erase all Content and Settings" in the simulator fixed the problem. Probably some leftover from the Betas.

Upvotes: 4

Related Questions