我使用bash和Xcode构建脚本是全新的,所以我的代码可能是一个充满错误的丛林。
这里的想法是触发下面的脚本,该脚本将为任何.js自动化脚本刮取保存的目录。然后,它会将这些脚本发送到一次运行一个的仪器。我发现了一些创建带时间戳文件的漂亮代码,所以我用它来创建一个更有意义的存储系统。
#!/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="Plans.app"
# 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}$"`
echo "$BUILT_PRODUCTS_DIR"
echo "$PRODUCT_NAME"
EXECUTABLE="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/"
SCRIPTS=`find $ROOT -name '*.js'`
# Prepare traces folder
TRACES="${ROOT}/Traces/`date +%Y-%m-%d_%H-%M-%S`"
mkdir -p "$TRACES"
printf "\n" >> "$ROOT/results.log"
echo `date +%Y-%m-%d_%H-%M-%S` >> "$ROOT/results.log"
# 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 "iPhone 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"
# Run all the tests.
for SCRIPT in $SCRIPTS; do
echo -e "\nRunning test script $SCRIPT"
TESTC="sudo -u ${USER} xcrun instruments -l -c -t ${TEMPLATE} ${EXECUTABLE} -e UIARESULTSPATH ${TRACES}/${TRACENAME} -e UIASCRIPT ${SCRIPT} >> ${ROOT}/results.log"
#echo "$COMMAND"
echo "Executing command $TESTC" >> "$ROOT/results.log"
echo "here $TESTC" >> "$ROOT/results.log"
OUTPUT=$(TESTC)
echo $OUTPUT >> "$ROOT/results.log"
echo "Finished logging" >> "$ROOT/results.log"
SCRIPTNAME=`basename "$SCRIPT"`
TRACENAME=`echo "$SCRIPTNAME" | sed 's_\.js$_.trace_g'`
for i in $(ls -A1t $PWD | grep -m 1 '.trace')
do
TRACEFILE="$PWD/$i"
done
if [ -e $TRACEFILE ]; then
mv "$TRACEFILE" "${TRACES}/${TRACENAME}"
fi
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
其中很大一部分来自另一个Stack Overflow答案,但由于我正在使用的存储库设置,我需要保持路径抽象并与脚本的根文件夹分开。除了用于启动仪器的实际xcrun命令外,一切似乎都有效(尽管可能效率不高)。
TESTC="sudo -u ${USER} xcrun instruments -l -c -t ${TEMPLATE} ${EXECUTABLE} -e UIARESULTSPATH ${TRACES}/${TRACENAME} -e UIASCRIPT ${SCRIPT} >> ${ROOT}/results.log"
echo "Executing command $TESTC" >> "$ROOT/results.log"
OUTPUT=$(TESTC)
无论黑魔法运行什么,这都会变成以下内容:
sudo -u Braains xcrun instruments -l -c -t
/Users/Braains/Documents/Automation/AppName/TestCases/UIAutomationTemplate.tracetemplate
/Users/Braains/Library/Developer/Xcode/DerivedData/AppName-
ekqevowxyipndychtscxwgqkaxdk/Build/Products/Debug-iphoneos/AppName.app/ -e UIARESULTSPATH
/Users/Braains/Documents/Automation/AppName/TestCases/Traces/2014-07-17_16-31-49/ -e
UIASCRIPT /Users/Braains/Documents/Automation/AppName/TestCases/Test-Case_1js
(^为了清晰起见,已插入换行符^)
我看到的结果错误是:
posix spawn failure; aborting launch (binary ==
/Users/Braains/Library/Developer/Xcode/DerivedData/AppName-
ekqevowxyipndychtscxwgqkaxdk/Build/Products/Debug-iphoneos/AppName.app/AppName).
我已经全神贯注地寻找解决方案,但我找不到任何东西,因为Appium有类似的问题。不幸的是,我不太了解系统,知道如何将修复程序转换为Appium到我自己的代码,但我认为它是一个类似的问题。
我知道posix spawn失败与线程有关,但我不太了解xcrun来说明造成线程问题的原因。
相关信息: - 我正在为模拟器构建,但在真实设备上工作也很棒 - 我正在使用xCode 5.1.1和iOS模拟器7.1 - 此脚本旨在作为xCode中的构建后期操作脚本运行 - 在我打破它并且无法恢复到工作状态之前,我确实让它短暂工作了一次。所以我认为这意味着我的所有权限都设置正确。
更新:所以我已经找到了这个问题的根源,尽管我还没有找到修复程序。首先,我不知道xcrun是什么,所以我放弃了它。然后玩了之后我发现我的Xcode环境变量返回错误的路径,可能是因为某些项目设置。如果从上面复制Bash命令但是用Debug-iphonesimulator替换Debug-iphoneos,脚本可以从命令行运行并按预期工作。
答案 0 :(得分:0)
因此,对于遇到这种情况的任何人,我能找到的唯一解决方案是对模拟器的脚本进行硬编码。
我将EXECUTABLE="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/"
更改为EXECUTABLE="${SYMROOT}/Debug-iphonesimulator/${EXECUTABLE_PATH}"
。这显然不是一个很好的解决方案,但它现在有效。