Adding your own features
When the built-in commands are not enough, you can add features inside your own project without modifying fleetest
itself. There are five main ways to do it. All of them are written in scenarios (.swift) or in files of your test project.
| What you want | How | Details |
|---|---|---|
| Bundle steps you repeat | A Swift function (custom command) | Custom commands |
| Run something the DSL has no command for, as one step | procedure { } |
Descriptors |
| Use test data or talk to external servers | account() / data() / dataFile() / httpRequest |
Test data and accounts, HTTP request |
| Use a library or share common code | Add a dependency to Package.swift |
Creating a test project |
| Prepare a database or stub server around a run | compose.yaml and start/end scripts |
Services the app depends on and Running scripts before and after a run on this page |
1. Bundle steps into functions
Steps that come up again and again (logging in, preparing a starting state, routine operations on a screen) go into
plain Swift functions. Adding @FTCommand("description") lists the function in the command index (ft_dsl_commands),
so the AI assistant that writes scenarios reuses it instead of writing the same steps by hand again. The index holds only
the name, call shape and summary, and is returned only to the AI assistant on this Mac (nothing goes outside, and the
function does not become runnable through MCP).
@FTCommand("Log in with email and password and go to the home screen")
func login(user: String, password: String) {
tap("#login_email")
type(user)
tap("#login_password")
type(password)
tap("#btn_login")
exist("#home_title")
}
Helpers that work on a grabbed element can be written in extension FTElement, so they chain like
select("#x").tapAndConfirm("#y").
Common uses:
- Functions that build an app-specific state: "logged in", "empty cart", "first-run tutorial done" — keep your team's conventions in one place.
- Grouping by screen: split functions into files per screen (close to the Page Object pattern).
@FTCommandcan only be put on top-level functions and on methods inside anextension, not inside aclass/struct. - Operating common components: date pickers, partial swipes, custom multi-touch built with
gesture, and so on. Component quirks are in UI component patterns and quirks.
The report lists the individual commands inside the function, not the function name. To show them as one unit,
wrap them in group("name") { }.
2. Run code the DSL has no command for, as one step
The body of procedure("description") { ... } is a synchronous closure (() throws -> Void); await cannot be written in it.
DSL commands can be called inside, and the steps inside are recorded with a [description] prefix and folded into one step in
the report (.md); the result JSON keeps all of them. Throwing inside it stops the scenario, just like a failed command.
procedure("Create a test order through the API") {
try seedTestOrder() // a synchronous function you wrote (it can call DSL commands)
}
To verify values that do not depend on the screen (API responses, computed values), use the
thisIs family (for example response.status.thisIs(201)).
- Always wrap code that must not run after an earlier step failed in
procedure { }(plain Swift outside it runs even if an earlier step has failed). - In-app notices and campaigns that may or may not appear are closed automatically if you declare an
irregularHandlerinbeforeEach(). fleetest does not resend an operation that the interruption may have swallowed, so write the recovery in a function if you need one. Only do this for operations that are safe to repeat (such as a tap that moves to another screen). Resending a submit or a purchase can run it twice. - Per-test setup and cleanup go in
beforeEach()/afterEach(); per-class preparation goes inbeforeAll()and preparation that should run once for the whole run inbeforeRun()of an@RunHooksclass; preparation and cleanup that should run once per device go insetUpDevice()/tearDownDevice().
3. Use test data and talk to external servers
| Function | Use it for |
|---|---|
account() / data() |
Reading accounts and input values from JSON. Keep passwords in this Mac's settings, not in the repository |
dataFile() |
Using CSV, JSON, images and other files as files |
addMedia() |
Adding photos and videos to the device's photo library (not possible on a physical iOS device) |
httpRequest / fleetestURLSession |
Creating test data through an API, cleaning up, reading server-side state |
writeRunMemo / readRunMemo |
Passing values prepared in beforeRun() / beforeAll() to other scenarios |
Common uses:
- Create users or orders through an API before the test, and delete them in
afterEach() - Fetch a verification email or one-time password from an inbox API and type it in
- Compare a value shown on screen with the value on the server
By default, scenarios cannot reach anything outside the Mac. Allow the destinations in this Mac's settings first (Network access from scenarios).
4. Add libraries and shared code
External Swift packages (one-time password generation, cryptography, JSON handling, ...) and your own targets shared
between several test projects can be imported from scenarios once you list them in two places of Package.swift:
dependencies: and fleetestScenarioDependencies. See "Adding dependencies for your scenarios" in
Creating a test project.
Building the added dependencies (including macros and build plugins) runs outside the sandbox. Add only packages you trust.
If a shared helper calls withRunLock, keep at least one call under the project's scenarios/ folder: the lock server starts only when such a call is found there (not in _disabled/, comments or strings), and a call that exists only in another target or package fails at once.
Running scripts before and after a run
To prepare data around a run (load a seed, call a setup API) and clean it up afterwards, put scripts in the test
project's workspace. Servers, databases and stubs are not started by a script: declare them in
scripts/compose.yaml (Services the app depends on).
| File | When it runs |
|---|---|
TestProjects/<project>/workspace/scripts/run-setup.sh |
At the start of the run, before any device is touched |
TestProjects/<project>/workspace/scripts/run-teardown.sh |
At the end of the run |
- If the file is there, it runs (there is nothing to set in a profile). If it is not, nothing happens.
- If
run-setup.shexits with a non-zero status, the run stops (no scenario runs).run-teardown.shstill runs in that case, always after the run. A failingrun-teardown.shonly produces a warning; its exit code does not change the result. - Without the execute permission the script is started with
/bin/sh. With it, the shebang is honoured (you can write it in Python, for example). - There is no timeout. Each time the script produces no output for more than 60 seconds, a one-line warning is printed.
- The scripts run inside a sandbox (Seatbelt).
- They can write only inside
<workspace>/data/(plus the paths in"hooks": {"writablePaths": [...]}in this Mac's settings). Use$TMPDIRfor temporary files; it is private to the script and removed after the run. - They cannot read secrets such as
~/.ssh. - They can connect only to the ports published by compose.yaml (
127.0.0.1:<port>) and, through the proxy, tosandbox.allowedDomains. They cannot reach the bridges, adb or Docker. - Do not start a server, database or stub from a script (
docker/containercannot be used). Declare it in compose.yaml. To operate inside a container, usefleetest-services(Services the app depends on). - A process left in the background is stopped after the run.
- For an Android physical device, fleetest sets up
adb reverseitself.
- They can write only inside
- The scripts receive the environment variables
FT_HOOK(setup/teardown),FT_WORKSPACE,FT_PROJECT,FT_PROFILE,FT_REPORT_DIR(readable, not writable),FT_IOS_DEVICESandFT_ANDROID_DEVICES(device names separated by spaces), and the names of the secrets that compose.yaml refers to. The variables are set even when they have no value. The Emulator's adb serial and the Simulator's UDID are not passed. - For a run sent to a runner machine, the scripts run on the runner machine.
- They run with
fleetest runand the MCP toolft_start_run, but not withft_run_scenario(the MCP tool that runs a single scenario). - The scripts do not run with
--dry-run(it does not touch devices).
Feeding results into your own systems
- CI: pass the JUnit XML from
fleetest run --junit <path>and the exit code to your CI (Running on CI). - Aggregation and notifications: read the result JSON (schema, Japanese) and feed it to
your own dashboard or chat notifications. What
fleetest resultscan aggregate is in Analyzing results (fleetest results, dashboard).
What you cannot add
| What you cannot do | Why / what to do instead |
|---|---|
| New kinds of operations on the device, or new information read from it | These are driver and bridge features, so fleetest itself has to change. Only what can be written by combining built-in commands is in your hands |
| Write arbitrary files or read secrets in your home folder from a scenario | Scenarios run inside a sandbox. Write through TestLog.directoryForLog / directoryForTemp |
| Start other apps or use adb / simctl directly from a scenario | fleetest itself performs a fixed set of operations on the scenario's behalf. adb can be opened with allowDirectAdb in this Mac's settings (see Sandbox defaults) |
Run a custom command through ft_batch |
ft_batch only runs built-in commands. Write custom commands in a scenario .swift file and run that |
| Read shell environment variables (tokens and so on) in a scenario | Scenarios only receive the variables fleetest uses. Put secrets in this Mac's account() files |