加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

从Xcode运行UIAutomation脚本

发布时间:2020-12-15 02:01:36 所属栏目:百科 来源:网络整理
导读:有没有人成功在Xcode中建立自动化UIAutomation测试? 我试图在我的Xcode项目中设置一个目标,它应该运行我准备的所有UIAutomation脚本。目前,此目标的唯一构建阶段是此运行脚本块: TEMPLATE="/Applications/Xcode.app/Contents/Applications/Instruments.a
有没有人成功在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

当我建立这个目标后,几秒钟后会成功,但脚本实际上没有运行。在构建日志中,我收到以下错误:

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中运行完全相同的仪器命令,它的工作原理如预期。
这可能是Xcode中的错误吗?

解决方法

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

su $USER -l -c <instruments command>

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

echo <password> | sudo -S su $USER -l -c <instruments command>

如果您不想对密码进行硬编码(总是一个坏主意),您可以使用一些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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读