You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用xcodebuild终端命令时如何设置iOS模拟器定位?

iOS自动化UI测试中通过xcodebuild设置模拟器GPS定位的解决方案

问题背景

运行iOS自动化UI测试时,在Xcode界面中通过Scheme>Run>Options>Default Location配置GPX文件,或在测试计划的Configurations>Simulated Location中设置GPX文件均能正常生效,但使用终端xcodebuild命令运行测试计划时,这些定位设置会被忽略,模拟器定位默认设为None。测试计划最终需要在CircleCI这类无法手动操作的环境中运行,必须通过命令行完成定位设置。

尝试过的无效操作

  • 使用set-simulator-location工具设置定位
  • 执行xcrun simctl location 'iPhone 14' set <lat, long>命令

以上两种方式无论是否提前关闭模拟器,仅在事后手动打开模拟器时定位设置才会生效,xcodebuild运行测试期间定位始终无效。

有效解决方案:通过AppleScript配置定位

最终通过AppleScript模拟人工操作模拟器菜单的方式完成定位设置,具体步骤如下:

终端执行命令

xcrun simctl boot 'iPhone 14'
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/
chmod 755 location_script.scpt
osascript location_script.scpt
xcodebuild ... # 替换为你的测试运行命令

AppleScript脚本文件(location_script.scpt)

tell application "Simulator"
    activate
end tell

tell application "System Events"
    tell process "Simulator"
        click menu item "Custom Location..." of menu of menu item "Location" of menu "Features" of menu bar 1
        set value of text field 1 of window 1 to <latitude> # 替换为目标纬度值
        set value of text field 2 of window 1 to <longitude> # 替换为目标经度值
        click button "OK" of window 1
    end tell
end tell

内容的提问来源于stack exchange,提问作者SoConfused

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 01:05:23