XCUIElement#typeText 两个文本字段失败

XCUIElement#typeText fails for two text fields

我在 Xcode 11.1 上观察到 XCUITest 的奇怪行为。我只有一个包含两个 UITextField 的情节提要,这两个 UITextField 都设置了可访问性标识符。我想在它们中都输入文本:

    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")  
        }  

    }  

在第二个字段中键入失败并出现以下错误,光标在第二个文本字段中闪烁:

    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'  
          }  

如果我删除输入 field1 的代码,它就可以工作。

我上传了一个example project here

我在这里想念什么?

"Hardware » Erase all Content and Settings" 在模拟器中解决了这个问题。 可能是 Beta 的一些剩余部分。

这是由切换软件键盘引起的。

如果您的测试失败,请启动您的应用程序,点击一个文本字段并注意缺少软件键盘。 Select I/O > Keyboard > Toggle Software Keyboard 从模拟器菜单,或 Cmd-K 焦点在模拟器上,然后应该显示键盘。重新运行你的测试应该不会再有这个问题了。

擦除模拟器也会修复它,因为默认情况下启用软件键盘。