从 Xcode 运行 UIAutomation 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13923272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running UIAutomation scripts from Xcode
提问by DrummerB
Did anyone succeed in setting up automated UIAutomation tests in Xcode?
有没有人成功地在 Xcode 中设置自动 UIAutomation 测试?
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 Phaseof this target is this Run Scriptblock:
我正在尝试在我的 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
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)
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?
我很确定,我的 javascript 和我的运行脚本都是正确的,因为如果我在 bash 中运行完全相同的仪器命令,它会按预期工作。这可能是 Xcode 中的错误吗?
采纳答案by DrummerB
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.
我终于找到了解决这个问题的方法。看起来 Xcode 正在以有限的权限运行运行脚本。我不完全确定是什么导致仪器命令失败,但使用su
更改为您的用户将修复它。
su $USER -l -c <instruments command>
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:
显然,这会要求您输入密码,但是作为脚本运行时您无法输入密码。我没有找到为 指定密码的方法su
,但是如果您以 root 身份运行它,则不必指定密码。幸运的是sudo
可以通过管道接受密码:
echo <password> | sudo -S su $USER -l -c <instruments command>
If you don't want to hardcode your password (always a bad idea), you could use some AppleScript to ask for the password.
如果您不想硬编码您的密码(总是一个坏主意),您可以使用一些 AppleScript 来询问密码。
I posted the resulting script below. Copy that to a *.sh file in your project and run that script from a Run Script.
我在下面发布了生成的脚本。将其复制到项目中的 *.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 }'`
# 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
回答by epologee
Note: this is not a direct answer to the question, but it is an alternative solution to the underlying problem.
注意:这不是问题的直接答案,而是潜在问题的替代解决方案。
While searching for in-depth information about UIAutomation, I stumbled across a framework by Square called KIF (Keep it functional). It is a integration testing framework that allows for many of the same features as UIAutomation, but the great thing about is is that you can just write your integration tests in Objective-C.
在搜索有关 UIAutomation 的深入信息时,我偶然发现了 Square 的一个名为 KIF(保持功能性)的框架。它是一个集成测试框架,允许许多与 UIAutomation 相同的功能,但最棒的是你可以用 Objective-C 编写集成测试。
It is very easy to setup (via CocoaPods), they have good examples too, and the best thing is that it's a breeze to set up with your CI system like Jenkins.
它很容易设置(通过 CocoaPods),他们也有很好的例子,最好的事情是设置像 Jenkins 这样的 CI 系统是轻而易举的。
Have a look at: http://github.com/square/KIF
回答by MyTallest
Late to the game but I have a solution that works for Xcode 5.1. Don't know if that's what broke the above solution or not. With the old solution I was still getting:
游戏迟到了,但我有一个适用于 Xcode 5.1 的解决方案。不知道这是否是破坏上述解决方案的原因。使用旧的解决方案,我仍然得到:
Failed to load Mobile Device Locator plugin, etc.
However, this works for the release version of Xcode 5.1.
但是,这适用于 Xcode 5.1 的发行版。
echo <password> | sudo -S -u username xcrun instruments
Notice I removed the unneeded su command and added the xcrun command. The xcrun was the magic that was needed.
请注意,我删除了不需要的 su 命令并添加了 xcrun 命令。xcrun 是需要的魔法。
Here is my complete command:
这是我的完整命令:
echo <password> | sudo -S -u username xcrun instruments\
-w "iPhone Retina (3.5-inch) - Simulator - iOS 7.1"\
-D "${PROJECT_DIR}/TestResults/Traces/Traces.trace"\
-t "${DEVELOPER_DIR}/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate"\
"${BUILT_PRODUCTS_DIR}/MyApp.app"\
-e UIARESULTSPATH "${PROJECT_DIR}/TestResults"\
-e UIASCRIPT "${PROJECT_DIR}/UITests/main.js"
By the way if you type:
顺便说一下,如果你输入:
instruments -s devices
you will get a list of all the supported devices you can use for the -w option.
您将获得可用于 -w 选项的所有受支持设备的列表。
Edit:To make this work for different people checking out the project replace the following:
编辑:为了让不同的人检查项目,请替换以下内容:
echo <password> | sudo -S -u username xcrun instruments
with
和
sudo -u ${USER} xcrun instruments
Since you are just doing an sudo to the same user no password is required.
由于您只是对同一用户执行 sudo 操作,因此不需要密码。
回答by Arkaaito
It seems as though this really might be an Xcode problem; at any rate, at least one person has filed a Radar report on it. Someone in this other threadclaims you can work around this exception by disconnecting any iDevices that are currently connected to the computer, but I suspect that does not apply when you're trying to run the script as an Xcode target.
看起来这真的可能是 Xcode 的问题;无论如何,至少有一个人已经提交了一份雷达报告。另一个线程中的某个人声称您可以通过断开当前连接到计算机的任何 iDevices 来解决此异常,但我怀疑当您尝试将脚本作为 Xcode 目标运行时,这并不适用。
I would suggest filing a Radar reportas well; you may get further details on the issue from Apple, or at least convince them that many peopleare having the problem and they ought to figure out what's going on.
我建议也提交一份雷达报告;你可能会从 Apple 那里得到关于这个问题的更多细节,或者至少让他们相信很多人都遇到了这个问题,他们应该弄清楚发生了什么。
Sorry for a not-terribly-helpful answer (should have been a comment, but comments and links/formatting do not mix very well). Please update this question with anything you find out on the issue.
很抱歉给出了一个不太有用的答案(应该是评论,但评论和链接/格式不能很好地混合)。请使用您在该问题上发现的任何信息更新此问题。
回答by MacN00b
Take a look at this tutorial that explains how to have Automated UI testing with Jenkins. It also uses Jasmine in the tutorial though. http://shaune.com.au/automated-ui-testing-for-ios-apps-uiautomation-jasmine-jenkins/hope this helps. It has an example project file so you can download that as a template. Hope this helps.
看看这个教程,它解释了如何使用 Jenkins 进行自动化 UI 测试。不过,它也在教程中使用了 Jasmine。http://shaune.com.au/automated-ui-testing-for-ios-apps-uiautomation-jasmine-jenkins/希望这会有所帮助。它有一个示例项目文件,因此您可以将其下载为模板。希望这可以帮助。
回答by Ronald Palmer
In XCode - if you load up organizer (XCode->Window->Organizer)
在 XCode 中 - 如果你加载管理器(XCode->Window->Organizer)
Then select your machine under devices -> 'Enable Developer Mode'
然后在设备下选择您的机器 - >“启用开发者模式”
This should remove the need for prompts with instruments.
这应该消除对仪器提示的需要。