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

后端 未结 24 1531
难免孤独
难免孤独 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: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='ConnectHardwareKeyboard'
    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. :)

提交回复
热议问题