# Automate Xcode UI Tests on a Remote Mac Guide

Canonical: https://figment.so/blog/mac-ui-testing-xcode-remote-mac

Create a disposable Simulator, run xcodebuild test against its UDID, then export attachments from the .xcresult bundle with xcresulttool. Run every command from the Mac's own shell over SSH or remote desktop.

**You can run Xcode UI tests on a rented Mac without owning hardware: create a disposable iOS Simulator, run `xcodebuild test` against it, let it write a `.xcresult` bundle, and pull attachments out with `xcrun xcresulttool`.** Every command below runs from the Mac's own shell, over SSH or in Terminal inside a remote desktop session.

## How do I run XCUITest on an isolated Simulator?

`XCUIApplication` is the proxy your test uses to launch, activate and terminate your app inside a Simulator. The class documents `init()`, `init(bundleIdentifier:)`, `launch()`, `activate()`, `terminate()` and the `launchArguments` and `launchEnvironment` properties at [developer.apple.com/documentation/xcuiautomation/xcuiapplication](https://developer.apple.com/documentation/xcuiautomation/xcuiapplication). A setup that boots a known-clean app looks like this:

```swift
let app = XCUIApplication()
app.launchArguments = ["-UITesting"]
app.launchEnvironment = ["BASE_URL": "https://staging.example.com"]
app.launch()
```

The shell side needs the same discipline. Do not run `xcrun simctl shutdown all` or `xcrun simctl erase all` on a shared machine. Those commands destroy every simulator on the host, including devices other jobs or teammates are using. Create a disposable device named for this run, confirm its UDID in the command's output (or in `xcrun simctl list devices`), and delete only that one when the test finishes. Replace the scheme, project and runtime with values from your own project:

```bash
set -euo pipefail

TS=$(date +%Y%m%d_%H%M%S)
SIM_NAME="uitests-${TS}-$$"

# Confirm the runtime and device type IDs on your machine:
#   xcrun simctl list runtimes
#   xcrun simctl list devicetypes
RUNTIME="com.apple.CoreSimulator.SimRuntime.iOS-18-0"
DEVTYPE="com.apple.CoreSimulator.SimDeviceType.iPhone-16"

UDID=$(xcrun simctl create "$SIM_NAME" "$DEVTYPE" "$RUNTIME")
trap 'xcrun simctl shutdown "$UDID" >/dev/null 2>&1 || true; \
      xcrun simctl erase    "$UDID" >/dev/null 2>&1 || true; \
      xcrun simctl delete   "$UDID" >/dev/null 2>&1 || true' EXIT

xcodebuild test \
  -project MyApp.xcodeproj \
  -scheme MyApp \
  -destination "platform=iOS Simulator,id=${UDID}" \
  -only-testing:MyAppUITests/OnboardingUITests \
  -resultBundlePath "./UITests_${TS}.xcresult"
```

The `trap ... EXIT` block tears down only the device this run created, even when `xcodebuild` crashes mid-suite. Check `xcodebuild -help` and `xcrun xcresulttool help` on the machine before you commit the pipeline. The flags shift between Xcode releases, and the tool output you actually have beats any copied command.

## How do I export attachments from the .xcresult bundle?

`-resultBundlePath` produces a directory containing the structured action record, coverage data and attachments (screenshots, screen recordings, `XCTAttachment` payloads). There are two realistic ways to look at it:

- **Open it in Xcode on the Mac.** In a remote desktop session, `open UITests_<timestamp>.xcresult` loads the graphical test report with attachments inline. This is fastest when you are already logged in.
- **Export attachments to a folder.** Xcode 16's `xcresulttool` has an `export attachments` subcommand that writes files to `--output-path`. Add `--only-failures` to limit the dump to failing tests, or `--test-id` to scope to one test. The older `get --format json` shape is deprecated and requires `--legacy`. Apple's developer forum thread at [developer.apple.com/forums/thread/763888](https://developer.apple.com/forums/thread/763888) tracks the move to `get test-results summary` and the new export shape.

This script, `ui-test-collect.sh`, runs the isolated Simulator, exports attachments on failure, and writes a summary JSON for CI dashboards:

```bash
#!/usr/bin/env bash
# ui-test-collect.sh <Scheme> [SimulatorDeviceType] [RuntimeID]
# Runs a UI suite on a disposable Simulator, then exports attachments.
set -euo pipefail

SCHEME="${1:?Usage: $0 <Scheme> [DeviceType] [RuntimeID]}"
DEVTYPE="${2:-com.apple.CoreSimulator.SimDeviceType.iPhone-16}"
RUNTIME="${3:-com.apple.CoreSimulator.SimRuntime.iOS-18-0}"

TS=$(date +%Y%m%d_%H%M%S)
WORK_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/figment-ui-test.XXXXXX")
BUNDLE="$WORK_ROOT/UITests.xcresult"
OUT="$WORK_ROOT/attachments"
mkdir -p "$OUT"

UDID=$(xcrun simctl create "uitests-${TS}-$$" "$DEVTYPE" "$RUNTIME")
cleanup() {
  xcrun simctl shutdown "$UDID" >/dev/null 2>&1 || true
  xcrun simctl erase    "$UDID" >/dev/null 2>&1 || true
  xcrun simctl delete   "$UDID" >/dev/null 2>&1 || true
}
trap cleanup EXIT

XCODE_STATUS=0
xcodebuild test \
  -scheme "$SCHEME" \
  -destination "platform=iOS Simulator,id=${UDID}" \
  -resultBundlePath "$BUNDLE" || XCODE_STATUS=$?

# Modern attachment export (Xcode 16+). Confirm on your machine with:
#   xcrun xcresulttool help export attachments
xcrun xcresulttool export attachments \
  --path "$BUNDLE" \
  --output-path "$OUT" \
  --only-failures || true

# Structured summary for CI dashboards.
xcrun xcresulttool get test-results summary \
  --path "$BUNDLE" > "$OUT/summary.json" 2>/dev/null || true

echo "Result bundle : $BUNDLE"
echo "Attachments   : $OUT"
exit "$XCODE_STATUS"
```

The script preserves `xcodebuild`'s exit code, so a failing suite still fails CI. The `|| true` on the export calls is deliberate: you still want the bundle preserved if `xcresulttool` returns an unexpected status on an older toolchain. Drop `--only-failures` to export every attachment. Transport the bundle off the machine with your CI's own upload step, and keep signing secrets in the CI's secret manager rather than in shell history.

## How do I make UI tests reliable on a remote Mac?

Flaky UI suites on remote Macs rarely come from the rental. They come from timing assumptions that only held on a foregrounded laptop. Four fixes cover most real cases:

- **Existence is not interactivity.** `element.waitForExistence(timeout: 5)` waits only for the element to appear in the accessibility hierarchy. It does not guarantee the element is on-screen, enabled or tappable. Before `.tap()`, also assert `element.isHittable`, or wait on an `NSPredicate(format: "exists == true AND isHittable == true")` expectation.
- **Animation assertions run early.** Assert on a post-animation element that exists only after the transition finishes, rather than calling `sleep()`.
- **Dirty device state leaks between runs.** The disposable-UDID pattern above fixes this, because a device created for this run cannot carry data forward.
- **Keyboard focus survives tests.** In `tearDown`, dismiss the keyboard or send `Return` explicitly rather than relying on the next test to ignore it.

For custom checkpoints, `XCTAttachment` captures a screenshot at any step and lets you keep it on green runs:

```swift
func testCheckoutFlow() {
    // drive the cart...
    let shot = app.screenshot()
    let attachment = XCTAttachment(screenshot: shot)
    attachment.name = "cart-before-payment"
    attachment.lifetime = .keepAlways   // default .deleteOnSuccess prunes on pass
    add(attachment)
}
```

If you run a CI agent on the Mac itself, review [GitHub's self-hosted runner hardening guide](https://docs.github.com/en/actions/reference/security/secure-use#hardening-for-self-hosted-runners): run only trusted code on the runner, keep signing material in the CI's secret manager, and do not dump the shell environment to a plaintext file.

## Which Figment Mac plan fits UI tests?

Match the plan to the Xcode and Simulator runtime your suite needs. Apple's [porting guide](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon) lists Xcode 26 or earlier for Intel Macs.

| Suite needs | Figment plan | Price |
|---|---|---|
| Xcode 26.3 or earlier, Intel-compatible builds and tests (macOS Sequoia) | [Intel Mac VM](/mac-hosting#intel-vm) | $25 a month, $2 a day |
| Xcode 26.4 or later, a current iOS Simulator runtime, arm64-only tools | [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) | $39 a month, $6 a day |
| Long suites or several parallel Simulators | [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) | $349 a month ($299 on a 12-month term) |

VM vCPUs are shared, and we have not measured Simulator UI frame performance or suite timings on any plan. Run one day on the plan you are considering before you commit to a month. We do not run your CI, so you set up the runner yourself. See all [Mac hosting plans](/mac-hosting).

**Related reading:**
- [Audit Xcode Build Settings on a Rented Mac](/blog/mac-xcode-build-settings-audit)
- [M-Series vs Intel Mac for Xcode Builds](/blog/mac-m-series-vs-intel-xcode)
- [Run GitHub Actions on a Self-Hosted Mac Runner](/blog/mac-github-actions-self-hosted-runner)
