从Xcode运行UIAutomation脚本 [英] Running UIAutomation scripts from Xcode

查看:177
本文介绍了从Xcode运行UIAutomation脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I'm trying to set up a target in my Xcode project that should run all the UIAutomation scripts I prepared. Currently, the only Build Phase of this target is this Run Script block:

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

当我建立这个目标时,它会在几秒钟后成功,但是该脚本实际上并未运行.在构建日志中,出现以下错误:

When I build this target it succeeds after a few seconds, but the script didn't actually run. In the build log I get these errors:

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)

我非常确定,我的JavaScript和运行脚本都正确,因为如果我在bash中运行完全相同的Instruments命令,它将按预期运行. 这可能是Xcode中的错误吗?

I am pretty sure, that my javascript and my run script are both correct, because if I run the exact same instruments command in bash it works as expected. Could this be a bug in Xcode?

推荐答案

我终于找到了解决此问题的方法.似乎Xcode正在以有限的权限运行运行脚本".我不确定,是什么原因导致instruments命令失败,但是使用su更改为您的用户将解决此问题.

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 <instruments command>

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

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 <password> | sudo -S su $USER -l -c <instruments command>

如果您不想对密码进行硬编码(总是一个坏主意),则可以使用一些AppleScript要求输入密码.

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

我在下面发布了结果脚本.将其复制到项目中的* .sh文件中,然后从运行脚本"中运行该脚本.

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

这篇关于从Xcode运行UIAutomation脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆