Running UIAutomation scripts from Xcode

前端 未结 6 2040
甜味超标
甜味超标 2020-12-14 08:44

Did anyone succeed in setting up automated UIAutomation tests in Xcode?

I\'m trying to set up a target in my Xcode project that should run all the UIAutomation scrip

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 09:31

    I finally found a solution for this problem. It seems like Xcode is running the Run Scripts with limited rights. I'm not entirely sure, what causes the instruments command to fail, but using su to change to your user will fix it.

    su $USER -l -c 
    

    Obviously, this will ask you for your password, but you can't enter it when running as a script. I didn't find a way to specify the password for su, however if you run it as root, you don't have to specify one. Luckily sudo can accept a password via the pipe:

    echo  | sudo -S su $USER -l -c 
    

    If you don't want to hardcode your password (always a bad idea), you could use some AppleScript to ask for the password.

    I posted the resulting script below. Copy that to a *.sh file in your project and run that script from a Run Script.

    #!/bin/bash
    
    # This script should run all (currently only one) tests, independently from
    # where it is called from (terminal, or Xcode Run Script).
    
    # REQUIREMENTS: This script has to be located in the same folder as all the
    # UIAutomation tests. Additionally, a *.tracetemplate file has to be present
    # in the same folder. This can be created with Instruments (Save as template...)
    
    # The following variables have to be configured:
    EXECUTABLE="TestApp.app"
    
    # Optional. If not set, you will be prompted for the password.
    #PASSWORD="password"
    
    # Find the test folder (this script has to be located in the same folder).
    ROOT="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    
    # Prepare all the required args for instruments.
    TEMPLATE=`find $ROOT -name '*.tracetemplate'`
    EXECUTABLE=`find ~/Library/Application\ Support/iPhone\ Simulator | grep "${EXECUTABLE}$"`
    SCRIPTS=`find $ROOT -name '*.js'`
    
    # Prepare traces folder
    TRACES="${ROOT}/Traces/`date +%Y-%m-%d_%H-%M-%S`"
    mkdir -p "$TRACES"
    
    # Get the name of the user we should use to run Instruments.
    # Currently this is done, by getting the owner of the folder containing this script.
    USERNAME=`ls -l "${ROOT}/.." | grep \`basename "$ROOT"\` | awk '{print $3}'`
    
    # Bring simulator window to front. Depending on the localization, the name is different.
    osascript -e 'try
        tell application "iOS Simulator" to activate
    on error
        tell application "iOS-Simulator" to activate
    end try'
    
    # Prepare an Apple Script that promts for the password.
    PASS_SCRIPT="tell application \"System Events\"
    activate
    display dialog \"Password for user $USER:\" default answer \"\" with hidden answer
    text returned of the result
    end tell"
    
    # If the password is not set directly in this script, show the password prompt window.
    if [ -z "$PASSWORD" ]; then
        PASSWORD=`osascript -e "$PASS_SCRIPT"`
    fi
    
    # Run all the tests.
    for SCRIPT in $SCRIPTS; do
        echo -e "\nRunning test script $SCRIPT"
        COMMAND="instruments -t \"$TEMPLATE\" \"$EXECUTABLE\" -e UIASCRIPT \"$SCRIPT\""
        COMMAND="echo '$PASSWORD' | sudo -S su $USER -l -c '$COMMAND'"
        echo "$COMMAND"
        eval $COMMAND > results.log
    
        SCRIPTNAME=`basename "$SCRIPT"`
        TRACENAME=`echo "$SCRIPTNAME" | sed 's_\.js$_.trace_g'`
        mv *.trace "${TRACES}/${TRACENAME}"
    
        if [ `grep " Fail: " results.log | wc -l` -gt 0 ]; then
            echo "Test ${SCRIPTNAME} failed. See trace for details."
            open "${TRACES}/${TRACENAME}"
            exit 1
            break
        fi
    
    done
    
    rm results.log
    

提交回复
热议问题