xcuitest-best-practices
Skill2 starsChanged 4 months agoMITView on GitHub
Use when writing, reviewing, or debugging XCUITest UI tests for iOS apps. Makes tests idempotent, free of manual setup, and portable between local runs and Xcode Cloud. Covers app-state reset before service construction, page objects, readiness waits, system alerts and permissions, custom gestures, relaunch and persistence checks, secrets, and flaky-test diagnosis. Symptoms: a test passes alone but fails in a full run or on CI, taps miss, waits time out, the app starts in the wrong state.
In this repository3 other files it gives its agents.
- logging-best-practicesSkill
- reclaim-dev-disk-spaceSkill
- reflectSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. About 0 are common, 11 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈11
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 23
Every test is self-contained.
- Distinctive: line 29
Do not replace the behavior under test with a fake.
- Distinctive: line 32
Use accessibility identifiers only where a label is not unique or not stable, and only if the project allows them.
- Very specific: line 34
Wait for readiness (exists, isHittable, isEnabled, or a proven data-ready state) before every action.
- Distinctive: line 37
Do not retry committed actions (create, save, send, pay) and do not dismiss alerts automatically.
- Distinctive: line 41
Start a test that only needs a signed-in account in a prepared state, set by a Debug simulator launch flag, never a deep link.
- Very specific: line 43
Use only standard XCUITest APIs in test code: no Process, xcrun simctl, or host file access.
- Distinctive: line 44
Never type, log, or attach real credentials or personal data.
- Distinctive: line 45
If the project says only a person runs UI tests, build them but do not run them, and say clearly that a build is not a run.
- Distinctive: line 53
If the flow needs network data, credentials, or system permissions.
- Distinctive: line 54
If it fails always, only in a full run, only on CI, or only on one device type.
- Distinctive: line 93
No test depends on another test's result or on state from an earlier run.
- Distinctive: line 98
Every launch turns off animations with a Debug simulator flag.
- Very specific: line 100
Test code uses no Process, simctl, or host paths.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
XCUITest Best Practices
Overview
A UI test drives the real app as a person would. It must prepare its own state, prove each screen is ready before it acts, act once, and fail at the first unexpected result. Investigate timing, data readiness, and setup ownership before you add a retry.
Project rules (for example in AGENTS.md, CLAUDE.md, or a testing guide) take priority over
this skill.
Agent behavior contract
- Every test is self-contained. It resets the state it needs at launch, does not depend on test order, and leaves nothing that changes the next run.
- Tests need no manual step: no reset command, no simulator setting, no specific test order. Host-only preparation goes in the scheme's Test pre-action, never in the test process.
- Reset app state inside the app, before services are constructed, behind an explicit launch flag that only Debug simulator builds read. Remove the flag before a relaunch that checks persistence.
- Do not replace the behavior under test with a fake. End-to-end tests use production implementations and normal storage unless the project defines another boundary.
- Query by visible text and accessibility labels first. They test what people see and hear. Use accessibility identifiers only where a label is not unique or not stable, and only if the project allows them.
- Wait for readiness (
exists,isHittable,isEnabled, or a proven data-ready state) before every action. Never usesleepas a wait. - A timed-out wait throws and ends the test. Do not continue with dependent steps.
- Do not retry committed actions (create, save, send, pay) and do not dismiss alerts automatically. An unexpected alert fails the test at once.
- Give the app one driver and each feature one page object. The driver knows no feature's screens, and a page holds only its own feature's steps. Tests read as user steps and outcomes.
- Start a test that only needs a signed-in account in a prepared state, set by a Debug simulator launch flag, never a deep link. Keep one test that drives the real sign-in flow.
- Use only standard XCUITest APIs in test code: no
Process,xcrun simctl, or host file access. - Never type, log, or attach real credentials or personal data. UI test logs record typed text.
- If the project says only a person runs UI tests, build them but do not run them, and say clearly that a build is not a run.
First 60 seconds (triage)
- Find the goal: a new test, a review, a flaky failure, or CI preparation.
- Find the facts:
- Which screens and services the flow touches, and which persistent state it reads or writes.
- If the flow needs network data, credentials, or system permissions.
- If it fails always, only in a full run, only on CI, or only on one device type.
- Branch:
- wrong start state or a test that depends on test order →
references/state-and-idempotency.md - taps that miss, waits that time out, data not loaded →
references/waiting-and-interaction.md - repeated queries or long tests →
references/page-objects.md - permission alerts, share sheets, background, deep links →
references/system-ui.md - secrets, pre-actions, Xcode Cloud →
references/ci-and-xcode-cloud.md - a failure you cannot explain →
references/diagnosing-failures.md
- wrong start state or a test that depends on test order →
Routing map
- Reset hooks, turning off animations, prepared start states, launch and relaunch, remote state, fixtures →
references/state-and-idempotency.md - Readiness predicates, scrolling, moving content, gestures →
references/waiting-and-interaction.md - The shared app driver, one page per feature, test layout and naming →
references/page-objects.md - SpringBoard alerts, permission resets, share sheets, backgrounding, URLs →
references/system-ui.md - Local and Xcode Cloud configuration, secrets, test plans →
references/ci-and-xcode-cloud.md - Simulator clones, result bundles, regression tests →
references/diagnosing-failures.md
Common pitfalls → next best move
| Symptom | Cause | Fix |
| Wrong start state on launch | Reset runs after services start work | Reset before service construction |
| Passes alone, fails in a full run | Leftover state, or a parallel clone | Reset per launch; read logs from the simulator the run used |
| Tap misses a dialog button | The dialog is still animating | Wait for isHittable |
| "Expected X to exist" after an action | Data not loaded yet | Wait for a proven ready state, then act once |
app.open(url) starts in the wrong state | open does not apply launchArguments | launch() first, then open(url) |
| System alert not found | It belongs to SpringBoard | Query XCUIApplication(bundleIdentifier: "com.apple.springboard") |
| Share sheet action not found | Actions are cells | Query app.cells["Copy"] |
| Slide control does nothing | tap() is a touch, not activation | Drag with press(forDuration:thenDragTo:) |
| Tap lands on the wrong row | Content moved after the scroll | Swipe slowly, then wait for a stable frame |
| Two buttons share one label | A sheet covers the screen behind it | Scope by container, or take the hittable match |
| Passes locally, fails on CI | Host command or a missing secret | Standard APIs only; supply secrets through CI configuration |
| One broken sign-in screen fails every test | Each test drives sign-in | Launch in a prepared state; keep one sign-in test |
| A feature's steps live on the driver or another page | No page for that feature | Add the feature's page and move the steps there |
Verification checklist
- Each test launches with the reset flag and passes when run alone, twice in a row, and in the full plan.
- No test depends on another test's result or on state from an earlier run.
- Each action waits for readiness; each wait throws on timeout.
- No
sleep, no generic alert dismissal, no retry of a committed action. - Queries use user-facing text where it is unique and stable.
- Relaunches that check persistence omit the reset flag.
- Every launch turns off animations with a Debug simulator flag.
- Remote state that a test creates is unique to the run or removed by the test.
- Test code uses no
Process,simctl, or host paths; secrets come from configuration. - One page per feature owns its queries; the driver holds no feature steps; tests read as user steps.
- Tests that only need a signed-in account start from the prepared-state flag; one test drives the real sign-in.
References
references/state-and-idempotency.mdreferences/waiting-and-interaction.mdreferences/page-objects.mdreferences/system-ui.mdreferences/ci-and-xcode-cloud.mdreferences/diagnosing-failures.md
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

