# 在 Actions 中运行 iPhone Safari 冒烟测试

> 用 iOS Simulator 打开 Mobile Safari、保存截图证据，并明确哪些失败仍需真机复现。

UNVERCO 于 2026-11-27 发布了一份 GitHub Actions 工作流。它在 `macos-15` runner 上启动可用的 iPhone iOS Simulator，用 Mobile Safari 打开指定 URL，截屏后把图片上传为工作流产物。作者还说明，公开前实际运行的就是文中展示的同一份工作流文件（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

## 托管 iPhone 模拟对发布证据的影响

GitHub 把 hosted runner 定义为执行工作流 job 的机器。除单 CPU runner 外，每个 hosted runner 都是新虚拟机；标准镜像包含 Ubuntu、Windows 和 macOS。GitHub 在 `actions/runner-images` 仓库维护预装软件清单，每次 job 的 **Set up job** 区域还会链接本次运行使用的准确镜像信息（[GitHub Docs](https://docs.github.com/en/actions/concepts/runners/github-hosted-runners)）。

macOS 15 镜像文档列出已安装的 Xcode 版本，并将其中一个标为默认版本。这份流程依赖 `xcrun simctl`，所以截图失败时应同时记录 runner 镜像和 Xcode 版本。不能只看 `macos-15` 标签就推断 iOS runtime，实际答案应取自本次 job 的镜像记录（[GitHub runner images](https://github.com/actions/runner-images/blob/main/images/macos/macos-15-Readme.md)）。

Apple 区分 Simulator 和设备运行：前者在 Mac 上辅助原型开发与测试，后者在连接的硬件上运行。截图通过只能证明页面在选定模拟环境里完成了这次渲染，不能证明依赖物理硬件的行为（[Apple Developer](https://developer.apple.com/documentation/xcode/running-your-app-in-simulator-or-on-a-device)）。

### 创建冒烟工作流

在仓库新增 `.github/workflows/iphone-safari-smoke.yml`，并把 `https://example.com` 换成 hosted runner 能访问的部署地址：

```yaml
name: iPhone Safari smoke test

on:
  workflow_dispatch:

jobs:
  ios:
    runs-on: macos-15
    timeout-minutes: 20
    steps:
      - name: Record runner tools
        run: |
          sw_vers
          xcodebuild -version
          xcrun simctl list runtimes

      - name: Boot an available iPhone simulator
        run: |
          UDID=$(xcrun simctl list devices available -j | python3 -c \
            "import json,sys; d=json.load(sys.stdin)['devices']; print(next(x['udid'] for r in d.values() for x in r if x['name'].startswith('iPhone')))")
          xcrun simctl boot "$UDID"
          xcrun simctl bootstatus "$UDID" -b
          echo "UDID=$UDID" >> "$GITHUB_ENV"

      - name: Open the site and capture Safari
        run: |
          xcrun simctl openurl "$UDID" "https://example.com"
          sleep 20
          xcrun simctl io "$UDID" screenshot iphone-safari.png
          test -s iphone-safari.png

      - name: Upload evidence
        uses: actions/upload-artifact@v4
        with:
          name: iphone-safari-screenshot
          path: iphone-safari.png
          if-no-files-found: error
```

设备选择命令读取 JSON，遍历可用 runtime，并取出第一个名字以 `iPhone` 开头的设备。`bootstatus -b` 等待启动完成，`openurl` 把部署地址交给模拟器，`simctl io` 截屏，`test -s` 则在图片不存在或为空时让步骤失败。UNVERCO 的公开工作流使用这些核心操作；上例额外记录工具版本，并明确验证产物（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

进入仓库 **Actions** 页面，点击 **Run workflow**。成功的 job 会生成 `iphone-safari-screenshot` 产物，日志中还有 macOS、Xcode 和可用 simulator runtime。下载 PNG 后检查实际画面。它只记录一个页面状态，不能顺带证明导航、输入、存储或通知行为已经通过（[GitHub Docs](https://docs.github.com/en/actions/concepts/runners/github-hosted-runners)；[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

把截图与 job 的运行记录一起保存。GitHub 文档说明，每个 hosted runner 都带有 runner 应用和预装工具，macOS runner 还包含操作系统默认工具；准确软件清单可以从 **Set up job** 展开的 **Runner Image** 与 **Included Software** 链接取得。评审截图时写下这两个链接，让记录明确本次使用的镜像（[GitHub Docs](https://docs.github.com/en/actions/concepts/runners/github-hosted-runners)）。

UNVERCO 发布的工作流由 `workflow_dispatch` 启动，把固定的 `https://example.com` 交给 `simctl openurl`，等待 20 秒后截取 `iphone-safari.png`（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

### 让失败结果可以诊断

Python 表达式若抛出 `StopIteration`，说明镜像中没有找到名称以 `iPhone` 开头的可用设备。查看日志里的 `xcrun simctl list devices available`，选取真实存在的名称，或者把选择条件限定到镜像支持的 runtime 与设备。此次执行安装了哪些工具，应以 job 的 **Runner Image** 链接为准（[GitHub Docs](https://docs.github.com/en/actions/concepts/runners/github-hosted-runners)）。

`bootstatus` 失败或 job 超时后，保留前面的 runtime 和设备清单。先重试一次，区分镜像临时故障与稳定复现的配置错误；如果仍失败，就明确选择已安装 runtime，不要猜测浮动标签里包含哪个版本。GitHub 说明 hosted image 软件每周更新，完整部署会持续数天，因此诊断时必须保存准确镜像链接（[GitHub Docs](https://docs.github.com/en/actions/concepts/runners/github-hosted-runners)）。

产物存在但显示错误页时，先在 runner 上独立检查目标响应：

```sh
curl --fail-with-body --silent --show-error \
  --location \
  --output /dev/null \
  --write-out 'status=%{http_code} type=%{content_type}\n' \
  https://example.com
```

预期输出应包含成功的 HTTP 状态和部署页面的内容类型。这个命令验证冒烟测试依赖的 HTTP 结果，不验证 Safari 渲染。受保护目标如果需要认证、私网路由或证书信任，必须先让 hosted runner 具备相应条件（[GitHub Docs](https://docs.github.com/en/actions/concepts/runners/github-hosted-runners)）。

如果测试要求点击、输入、溢出断言或控制台检查，就要把固定等待和单张截图换成自动化会话。UNVERCO 建议用 Appium 的 XCUITest driver 驱动交互流程。Appium 的真机文档还单独列出设备 provisioning、信任、签名和 WebDriverAgent 配置；这些工作超出了 simulator 冒烟测试（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)；[Appium](https://appium.github.io/appium-xcuitest-driver/latest/real-device-config/)）。

原工作流在 `simctl io "$UDID" screenshot iphone-safari.png` 前直接执行 `sleep 20`。UNVERCO 把它称为最小工作流，并另行说明：点击流程、溢出、触控目标和控制台错误需要 WebDriver 会话，例如配合 XCUITest driver 的 Appium（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

### 准确定义“通过”

UNVERCO 报告，其仓库并行运行 Chromium、Firefox、WebKit、Android 14 emulator 和 iOS Simulator 时约耗时 10 到 13 分钟，iOS 部分最慢；仅 Web 检查约两分钟。这些数字是该仓库的测量结果，不能当作其他项目的估算，也不是 GitHub runner 的时长承诺（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

原文还明确写出限制：simulator 和 emulator 不是物理手机，不能复现真实硬件、网络、相机、推送通知或原生应用。依赖这些表面的发布风险必须保留真机测试路线。Apple 同样要求在需要硬件结果时，把应用运行到连接的设备上（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)；[Apple Developer](https://developer.apple.com/documentation/xcode/running-your-app-in-simulator-or-on-a-device)）。

合格的通过说明应只陈述实际观察：指定 hosted image 启动了清单里的 iPhone Simulator，Mobile Safari 打开目标，上传截图显示预期画面。更强的结论必须增加独立断言。点击流程需要自动化；离线结论需要受控断网并刷新；存储恢复需要先写入值，再在重开后做相等断言（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

桌面设备模拟也不能替代这次检查。UNVERCO 指出，桌面模拟会改变 user agent、屏幕尺寸、viewport 和触摸支持，却不会把底层引擎变成 iOS Safari。其报告把动态 viewport、safe-area inset、输入框字体触发缩放和触控目标列为需要在 iPhone 环境里观察的页面问题。这些条目仍应针对自己的页面逐项设置可见断言（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

Android 路线也应单独记录。原文使用 `reactivecircus/android-emulator-runner` 在 Linux runner 启动 Android emulator，并把它与 iOS Simulator 并行运行。Android 通过不能替代 iOS 结果；两条 job 应分别上传证据和报告状态（[UNVERCO](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)）。

### 保留失败，不要掩盖失败

不要让工作流接受空截图，也不要忽略 URL 检查失败。hosted simulation 无法使用时，必需检查应保持失败，再转入已记录的真机测试路线。Appium 的配置会明确暴露设备身份、签名、WebDriverAgent 和连接条件，避免把 simulator 结果悄悄当成硬件证据（[Appium](https://appium.github.io/appium-xcuitest-driver/latest/real-device-config/)）。

真机恢复路线应写清设备与连接前提。Appium 文档要求为真实 iOS 设备配置 provisioning、信任、签名和 WebDriverAgent。若团队没有这些条件，应把依赖真机的发布风险保留为未验证，不能把 simulator 截图改名为真机回归。等设备路线就绪后，再执行同一用户旅程并保留设备与系统版本（[Appium](https://appium.github.io/appium-xcuitest-driver/latest/real-device-config/)）。

:::caution
GitHub 每周更新 hosted image 软件，部署会持续数天。simulator 结果变化时，应按 job 日志中的准确 **Runner Image** 链接诊断，不能只看 YAML 里的 `macos-15` 标签（[GitHub Docs](https://docs.github.com/en/actions/concepts/runners/github-hosted-runners)）。
:::

## 相关条目

- [能力](/zh/reference/capabilities/)：能力分类及其证据见此条目。
- [性能](/zh/reference/performance/)：性能测量和加载指南见此条目。
- [存储](/zh/reference/storage/)：浏览器存储、配额与持久化指南见此条目。

## 待核实

本仓库没有运行这份工作流，因此可用 iPhone 型号、iOS runtime、执行时长和截图结果都未在此确认。并行运行耗时 10 到 13 分钟，Web 检查约两分钟；这些数字只属于来源作者的仓库。

工作流选中的首个可用设备是否符合项目目标 iPhone 与 iOS runtime，仍未核实。固定等待 20 秒后页面是否到达目标状态，也要按每次部署另行验证；需要认证、私网路由或自定义证书信任的目标能否访问，同样没有确认。

真实 iPhone 上的相机、网络切换、推送通知和安装应用行为仍未验证。simulator 截图无法回答这些问题，也没有真机记录可供核对。

## 来源

- [Test iPhone Safari on GitHub Actions without a Mac](https://dev.to/unverco/test-iphone-safari-on-github-actions-without-a-mac-a-working-workflow-and-the-limits-3gbf)（DEV Community）
- [GitHub-hosted runners](https://docs.github.com/en/actions/concepts/runners/github-hosted-runners)（GitHub Docs）
- [macOS 15 runner image](https://github.com/actions/runner-images/blob/main/images/macos/macos-15-Readme.md)（GitHub）
- [Running your app in Simulator or on a device](https://developer.apple.com/documentation/xcode/running-your-app-in-simulator-or-on-a-device)（Apple Developer）
- [Real Device Configuration](https://appium.github.io/appium-xcuitest-driver/latest/real-device-config/)（Appium）