从Xcode运行UIAutomation脚本

Dru*_*erB 20 xcode instruments ui-automation

有没有人成功在Xcode中设置自动化UIAutomation测试?

我正在尝试在我的Xcode项目中设置一个目标,该目标应该运行我准备的所有UIAutomation脚本.目前,此目标的唯一构建阶段是此运行脚本块:

TEMPLATE="/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate"
MY_APP="/Users/Me/Library/Application Support/iPhone Simulator/6.0/Applications/564ED15A-A435-422B-82C4-5AE7DBBC27DD/MyApp.app"
RESULTS="/Users/Me/Projects/MyApp/Tests/UI/Traces/Automation.trace"
SCRIPT="/Users/Me/Projects/MyApp/Tests/UI/SomeTest.js"
instruments -t $TEMPLATE $MY_APP -e UIASCRIPT $SCRIPT -e UIARESULTSPATH $RESULTS
Run Code Online (Sandbox Code Playgroud)

当我构建此目标时,它会在几秒钟后成功,但脚本实际上并未运行.在构建日志中,我收到以下错误:

instruments[7222:707] Failed to load Mobile Device Locator plugin
instruments[7222:707] Failed to load Simulator Local Device Locator plugin
instruments[7222:707] Automation Instrument ran into an exception while trying to run the script.  UIATargetHasGoneAWOLException
+0000 Fail: An error occurred while trying to run the script.
Instruments Trace Complete (Duration : 1.077379s; Output : /Users/Me/Projects/MyApp/Tests/UI/Traces/Automation.trace)
Run Code Online (Sandbox Code Playgroud)

我很确定,我的javascript和我的运行脚本都是正确的,因为如果我在bash中运行完全相同的仪器命令它按预期工作.这可能是Xcode中的一个错误吗?

Dru*_*erB 4

我终于找到了解决这个问题的方法。Xcode 似乎正在以有限的权限运行运行脚本。我不完全确定是什么导致仪器命令失败,但使用su更改为您的用户将修复它。

su $USER -l -c <instruments command>
Run Code Online (Sandbox Code Playgroud)

显然,这会要求您输入密码,但作为脚本运行时您无法输入密码。我没有找到一种方法来指定 的密码su,但是如果您以 root 身份运行它,则不必指定密码。幸运的是sudo可以通过管道接受密码:

echo <password> | sudo -S su $USER -l -c <instruments command>
Run Code Online (Sandbox Code Playgroud)

如果您不想对密码进行硬编码(这总是一个坏主意),您可以使用一些 AppleScript 来请求密码。

我在下面发布了生成的脚本。将其复制到项目中的 *.sh 文件并从运行脚本运行该脚本。

#!/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
Run Code Online (Sandbox Code Playgroud)