Grant Screen Recording to your app and the signed xctest tool once on the runner, then add an XCUITest that calls CGPreflightScreenCaptureAccess() and counts named windows from CGWindowListCopyWindowInfo. The test fails loudly when the gate is closed and writes a JSON evidence file per run. You need this if your macOS app reads the window list, for example a screenshot tool, meeting overlay, accessibility helper or screen recorder.
Apple does not ship a supported command to grant a TCC decision from the shell. tccutil exposes only reset. Treat screen-recording permission as a one-time setup step on your runner (an interactive grant kept in the TCC database, or an MDM/PPPC configuration profile on supervised fleets), then let the test suite enforce the grant on every run.
What does the screen-recording TCC gate do?
Without permission, the window list loses its names and the call still succeeds. Since macOS Catalina, CGWindowListCopyWindowInfo does not prompt and does not throw when screen-recording permission is absent. It silently filters the metadata it returns: the kCGWindowName key is withheld from every window, and kCGWindowSharingState is set to zero for windows your app does not own.
That silent degradation is the common CI bug. The test passes because the call succeeds, but your app receives empty window names and cannot do its job.
The modern API for checking and requesting permission is CGPreflightScreenCaptureAccess and CGRequestScreenCaptureAccess:
// Swift — check before reading the window list
if CGPreflightScreenCaptureAccess() {
// permission already granted; proceed
} else {
CGRequestScreenCaptureAccess()
// macOS shows the system prompt; a CI runner must pre-grant interactively or via PPPC
}
CGRequestScreenCaptureAccess() shows a UI prompt, which makes it unsuitable for a headless test leg. Pre-grant on the runner once, then use the preflight call as your gate.
How do I set up the permission on the runner?
Grant it once interactively, or deploy a PPPC profile. tccutil only supports the reset verb. There is no public insert or add verb that writes a grant, so scripts that call tccutil insert ScreenCapture ... allowed fail on current macOS. For a CI Mac you have two supported options:
- One-time interactive grant. Open the runner's desktop over remote desktop (you have administrator access to your own Mac or VM), run the test host once, approve the Screen Recording prompt in System Settings → Privacy & Security → Screen Recording, and leave the decision in the TCC database. Grant both the app under test and the signed
xctesttool. Check the exact identity withcodesign -dr - $(xcrun --find xctest), because the bundle changes across Xcode versions. - Managed profile. On a supervised fleet, deploy a PPPC (Privacy Preferences Policy Control) configuration profile that pre-authorises your signed bundles. This is the only Apple-supported way to pre-seed the decision without a prompt.
If you need to re-prompt during setup, tccutil reset ScreenCapture <bundle-id> is the supported verb. macOS 15 can show a weekly reminder sheet for screen capture. Supervised fleets suppress it with PPPC, and on a solo runner you dismiss the sheet and re-verify the grant on the next run.
What does the XCUITest assertion look like?
The test launches the app, calls CGPreflightScreenCaptureAccess(), then queries the window list and asserts that at least one window has a non-empty name, which is only possible when the test host already holds the grant. If permission is missing, the test fails loudly rather than passing on filtered metadata.
import XCTest
import CoreGraphics
final class ScreenRecordingPermissionTests: XCTestCase {
func testWindowListReturnsNamesWithPermission() throws {
let app = XCUIApplication()
app.launch()
XCTAssertTrue(app.windows.firstMatch.waitForExistence(timeout: 5))
XCTAssertTrue(CGPreflightScreenCaptureAccess(),
"Screen Recording not granted to the test host. Grant it in System Settings "
+ "to the signed xctest tool (check with `codesign -dr - $(xcrun --find xctest)`).")
let windowList = CGWindowListCopyWindowInfo(
[.optionAll, .excludeDesktopElements],
kCGNullWindowID
) as? [[CFString: Any]] ?? []
XCTAssertFalse(windowList.isEmpty, "CGWindowListCopyWindowInfo returned no windows")
let namedCount = windowList.filter { dict in
(dict[kCGWindowName] as? String).map { !$0.isEmpty } ?? false
}.count
XCTAssertGreaterThan(namedCount, 0,
"No window names returned — screen-recording permission is likely absent")
let evidence: [String: Any] = [
"totalWindowCount": windowList.count,
"namedWindowCount": namedCount,
"permissionGranted": namedCount > 0,
"timestamp": ISO8601DateFormatter().string(from: Date())
]
try writeEvidenceJSON(evidence)
}
private func writeEvidenceJSON(_ dict: [String: Any]) throws {
let data = try JSONSerialization.data(
withJSONObject: dict,
options: [.prettyPrinted, .sortedKeys]
)
let dir = ProcessInfo.processInfo.environment["EVIDENCE_DIR"] ?? NSTemporaryDirectory()
let url = URL(fileURLWithPath: dir)
.appendingPathComponent("screen-recording-evidence.json")
try data.write(to: url, options: .atomic)
print("Evidence written to \(url.path)")
}
}
Attach the evidence file to the xcresult bundle with XCTAttachment, or have your CI step copy it next to TestResults.xcresult. The code above is an untested illustration. Run it against your own project and scheme before you trust the output.
How do I keep TCC state predictable between runs?
Do not reset Screen Recording at the end of every run. Once the record is cleared, no supported command puts the decision back without a prompt. The workflow for a dedicated CI Mac:
- Grant permission once during runner setup for the app bundle and the signed
xctesttool. - Document the two bundle identifiers in your runner playbook so a rebuild knows what to re-approve.
- Run
tccutil reset ScreenCapture <bundle>only when you need to re-verify the prompt flow, and schedule an interactive re-grant immediately after.
Both the app under test and the test host call CGWindowListCopyWindowInfo, so both must appear in the Screen Recording pane.
What Mac behaves most like a physical runner for screen recording tests?
The Dedicated M4 Pro 48 GB ($349 a month, or $299 a month on a 12-month term, whole Mac) is the closest match to a physical runner, because the window-server behavior behind CGWindowListCopyWindowInfo is easiest to trust on a whole Mac. The Apple Silicon Mac VM ($39 a month, $6 a day) and the Intel Mac VM ($25 a month, $2 a day, macOS Sequoia) both give you administrator access to your own VM, which the one-time grant needs. We have not measured the screen-recording grant or window-name results on either VM, so run this test once on the machine you choose before you depend on it. See the Mac hosting plans.
For market context, the table recomputes a 12-month nightly runner. CloudMac lists a whole Mac mini M4, so the Dedicated M4 Pro is the nearer comparison than a shared VM.
| Option | Price | 12-month cost | Notes |
|---|---|---|---|
| Figment Apple Silicon Mac VM (4 shared vCPUs, 4GB) | $39/mo | $468 (12 × $39) | VM with shared vCPUs |
| Figment Dedicated M4 Pro 48 GB (48GB, 512GB) | $349/mo | $3,228 (12 months paid up front) | Whole Mac |
| CloudMac M4, 16 GB, 256 GB | $99.99/mo | $1,199.88 (12 × $99.99) | 256 GB row shows "Reserve now"; 512 GB and 1 TB show "Fully reserved" |
| CloudMac M4, 16 GB, 256 GB annual | $999/yr ($83.25/mo effective) | $999 | 12-month prepay |
Related guides:
- xctest-failure-screenshot-ci: capture XCTest failure screenshots automatically
- safari-iframe-sandbox-regression: test Safari sandbox policy in a real browser environment
- github-actions-self-hosted-runner: configure a hosted Mac as a persistent GitHub Actions runner