2

我正在尝试自动化我在项目中进行的功能测试。为此,我使用 Jenkins 并使用 post-receive git hook 运行测试任务。该作业已正确调用,但在运行测试之前,我需要擦除模拟器以测试应用程序的首次启动。我执行以下操作:

#!/bin/bash --login

# simulator we want
sim="iPhone 6"

# close the iOS simulator if open
echo "Trying to close iOS Simulator"
osascript -e 'tell app "iOS Simulator" to quit'

# find all booted devices
booted=( $(xcrun simctl list | sed -n 's/.*(\(.*\)) (Booted)/\1/p') )
if [ ${#booted[@]} != 0 ]; then
    echo 'Found the following booted devices:'
    for device in ${booted[@]}
    do
        echo $device
    done
else
    echo 'There are no booted devices, skipping'
fi

# shutdown all of them to be able to erase them
for device in ${booted[@]}
do
    echo "Trying to shutdown $device"
    xcrun simctl shutdown $device
    echo "Done"
done

# sanity check, all devices should be shutdown
booted=( $(xcrun simctl list | sed -n 's/.*(\(.*\)) (Booted)/\1/p') )
if [ ${#booted[@]} != 0 ]; then
    echo 'Even though we shut down all the devices, some devices are still booted:'
    for device in ${booted[@]}
    do
        echo $device
    done
    exit 1
fi  

# erase the device so we can test index page and tutorial
allDevices=( $(xcrun simctl list | sed -En 's/.* \((.*)\) \((Shutdown)\)/\1/p') )
for device in ${allDevices[@]}
do
    echo "Erasing device $device"
    xcrun simctl erase $device
    echo
done

# sanity check, all devices should be shutdown
booted=( $(xcrun simctl list | sed -n 's/.*(\(.*\)) (Booted)/\1/p') )
if [ ${#booted[@]} != 0 ]; then
    echo 'Even though we shut down all the devices, some devices are still booted:'
    for device in ${booted[@]}
    do
        echo $device
    done
    exit 1
fi

echo device list:
echo $(xcrun simctl list)

dev=( $(xcrun simctl list | sed -En 's/'"$sim"' \((.*)\) \((Shutdown)\)/\1/p') )
echo Booting the device $dev
xcrun simctl boot $dev

# clean is not good enough, need to remove DerivedData contents manually
rm -rf ~/Library/Developer/Xcode/DerivedData

/usr/local/bin/xctool -workspace MyApp.xcworkspace -scheme MyApp_QA2 clean

xcodebuild -workspace MyApp.xcworkspace -scheme MyApp_QA2 -destination 'platform=iOS Simulator,name=iPhone 6,OS=8.1' test | xcpretty -c -r html

当我运行它时,我得到:

无法启动已启动的设备

这些行负责引导:

dev=( $(xcrun simctl list | sed -En 's/'"$sim"' \((.*)\) \((Shutdown)\)/\1/p') )
echo Booting the device $dev
xcrun simctl boot $dev

所以,我评论他们,但随后构建失败:

2015-03-10 09:56:13.036 xcodebuild[84840:4008451] [MT] iPhoneSimulator:无法连接到“com.apple.instruments.deviceservice.lockdown”(错误域=com.apple.CoreSimulator.SimError Code=146 “无法在当前状态下查找:关机” UserInfo=0x7fbcb2f00af0 {NSLocalizedDescription=无法在当前状态下查找:关机})

在我看来,Xcode 和 simctl 不能同意哪一个应该负责引导正确的 sim。有任何想法吗?

4

2 回答 2

1

在 Xcode 6 和 Xcode 7 中,Simulator.app 负责启动它使用的设备。如果您使用 simctl 引导设备,则 Simulator.app 在该状态下将无法使用它,因为它将被引导到无头状态。

于 2015-06-19T14:46:05.797 回答
1

您可以启动模拟器xcrun instruments -w "iPhone 5 (8.4 Simulator)" 和关闭模拟器killall "iOS Simulator"

于 2015-08-06T09:26:09.180 回答