Search code examples
iosswiftuitextfieldxcode-ui-testingfastlane

Fastlane Scan "Failed to synthesize event" when running UI Tests, works from Xcode


I have some UI Tests for my Swift project (iOS application, Swift 5) that work when running them from Xcode; however when I run them using fastlane scan, they always fail at the same point on each test that involves typing into text fields and I am not sure why.

The failure comes from filling out a UITextField, then attempting to tap the next one and fill that in.

Example:

app.textFields[“identifierOne”].tap()
app.textFields[“identifierOne”].typeText(“Text to Type”)

app.textFields[“identifierTwo”].tap()
app.textFields[“identifierTwo”].typeText(“Text to Type”)

This results in:

Failed to synthesize event: Neither element nor any descendant has keyboard focus. Event dispatch snapshot: TextField, label: ‘identifierTwo‘, placeholderValue: ‘Some ‘Value‘‘

As I mentioned earlier, this works when running the exact same tests from Xcode. I am at a loss with this one, any help is greatly appreciated.


Solution

  • So I figured this out in the end. The tests were working fine.

    But I had disable_slide_to_type: true in my scan call, which was causing some weird behaviour it seems.

    I also added the following before attempting to type:

    extension XCUIElement {
        var isFocused: Bool {
            let isFocused = (self.value(forKey: "hasKeyboardFocus") as? Bool) ?? false
            return isFocused
        }
    }
    
    
    if textField.isFocused == false {
        textField.tap()
    }
    

    And it seems to be working 100% of the time now.