UI Testing Failure - Neither element nor any descendant has keyboard focus on secureTextField

后端 未结 24 1472
难免孤独
难免孤独 2020-12-12 10:41

This is my case:

let passwordSecureTextField = app.secureTextFields[\"password\"]
passwordSecureTextField.tap()
passwordSecureTextField.typeText(\"wrong_pass         


        
相关标签:
24条回答
  • 2020-12-12 11:13

    We encountered the same error when setting the accessibilityIdentifier value for a custom view (UIStackView subclass) containing UIControl subviews. In that case XCTest was unable to get the keyboard focus for the descendent elements.

    Our solution was simply to remove the accessibilityIdentifier from our parent view and set the accessibilityIdentifier for the subviews through dedicated properties.

    0 讨论(0)
  • 2020-12-12 11:16

    It occurred with me for many times. You have to disable Keyboard Hardware and Same Layout as OSX in your Simulator

    Hardware/Keyboard (disable all)

    After that keyboard software won't dismiss and your tests can type text

    0 讨论(0)
  • 2020-12-12 11:17

    Record case however you want, keyboard or without keyboard attached. But do the following before playing test.

    This following option (connect hardware keyboard) should be unchecked while playing test.

    0 讨论(0)
  • 2020-12-12 11:17

    The problem for me was the same as for Ted. Actually if password field gets tapped after login field and hardware KB is on, software keyboard will dismiss itself on second field tap, and it's not specific to UI tests.

    After some time messing around with AppleScript, here's what I came up with(improvements are welcome):

    tell application "Simulator" activate tell application "System Events" try tell process "Simulator" tell menu bar 1 tell menu bar item "Hardware" tell menu "Hardware" tell menu item "Keyboard" tell menu "Keyboard" set menuItem to menu item "Connect Hardware Keyboard" tell menu item "Connect Hardware Keyboard" set checkboxStatus to value of attribute "AXMenuItemMarkChar" of menuItem if checkboxStatus is equal to "✓" then click end if end tell end tell end tell end tell end tell end tell end tell on error tell application "System Preferences" activate set securityPane to pane id "com.apple.preference.security" tell securityPane to reveal anchor "Privacy_Accessibility" display dialog "Xcode needs Universal access to disable hardware keyboard during tests(otherwise tests may fail because of focus issues)" end tell end try end tell end tell

    Create a script file with code above and add it to necessary targets(probably UI tests target only, you may want to add similar script to your development targets to re-enable HW keyboard during development). You should add Run Script phase in build phases and use it like this: osascript Path/To/Script/script_name.applescript

    0 讨论(0)
  • 2020-12-12 11:17

    Finally, I wrote a script which edits the Simulator's .plist file and sets the ConnectHardwareKeyboard property to false for the selected simulator. You heard it right, it changes the property for the specifically selected simulator inside "DevicePreferences" dictionary rather than editing the global property.

    First, create a shell script named disable-hardware-keyboard.sh with the following contents. You can place it within "YourProject/xyzUITests/Scripts/".:

    echo "Script: Set ConnectHardwareKeyboard to false for given Simulator UDID"
    
    if [[ $1 != *-*-*-*-* ]]; then
        echo "Pass device udid as first argument."
        exit 1
    else
        DEVICE_ID=$1
    fi
    
    DEVICE_PREFERENCES_VALUE='<dict><key>ConnectHardwareKeyboard</key><false/></dict>'
    killall Simulator # kill restart the simulator to make the plist changes picked up
    defaults write com.apple.iphonesimulator DevicePreferences -dict-add $DEVICE_ID $DEVICE_PREFERENCES_VALUE
    open -a Simulator # IMPORTANT
    

    Now follow these steps to call it with passing the selected simulator's udid as an argument:

    1. Edit your Xcode scheme (or UI tests specific scheme if you have one)
    2. Go to: Test > Pre-actions
    3. Add new script by tapping "+" symbol > "New Run Script Action".
    4. Important: Inside "Provide build settings from" dropdown choose your main app target, not the UI tests target.
    5. Now add the following script in the text area below.

    Script inside Test>Pre-actions:

    #!/bin/sh
    # $PROJECT_DIR is path to your source project. This is provided when we select "Provide build settings from" to "AppTarget"
    # $TARGET_DEVICE_IDENTIFIER is the UDID of the selected simulator
    sh $PROJECT_DIR/xyzUITests/Scripts/disable-hardware-keyboard.sh $TARGET_DEVICE_IDENTIFIER
    
    # In order to see output of above script, append following with it:
    #  | tee ~/Desktop/ui-test-scheme-prescript.txt
    

    Time to test it:

    1. Launch simulator
    2. Enable hardware keyboard for it
    3. Run any UI test with keyboard interaction. Observe the simulator restarts and the hardware keyboard is disabled. And the test's keyboard interaction is working fine. :)
    0 讨论(0)
  • 2020-12-12 11:21

    [Reposting Bartłomiej Semańczyk's comment as an answer because it solved the problem for me]

    I needed to do Simulator > Reset Contents and Settings in the simulator menu bar to make this start working for me.

    0 讨论(0)
提交回复
热议问题