The device-control backend for MonkeyTester. It runs on a Mac, owns every iPhone and Android phone, and exposes them over HTTP (see API.md). It knows nothing about tests or vision models.
MonkeyTester (Linux) ──HTTP──► device_controller.py (Mac, :8090)
├── Appium (:4723) ──► iPhone (XCUITest)
├── xcrun devicectl ─► iPhone (install)
└── adb + ffmpeg ────► Android (input, video)
Warning: a token is required by default. The service listens on every interface (
CONTROLLER_BIND=0.0.0.0) so the MonkeyTester viewer can reach it over the LAN. It refuses to start (exit code 2) unlessCONTROLLER_TOKENis set, orCONTROLLER_BINDis loopback (127.0.0.1,::1,localhost). Enter the same token in the MonkeyTester viewer under Settings. See SECURITY.md.
The service itself needs only the Python standard library. On the Mac, install
Xcode, Appium with the XCUITest driver, Android platform-tools and ffmpeg
(brew install ffmpeg). Android live view and recording need ffmpeg, and so does the
iOS live view when it has to fall back to screenshots.
CONTROLLER_TOKEN=$(python3 -c 'import secrets; print(secrets.token_urlsafe(32))') \
APPIUM_ORG_ID=<Xcode team id> \
APPIUM_SIGNING_ID="Apple Development" \
APPIUM_WDA_BUNDLE_ID=com.blackjackfruit.WebDriverAgentRunner \
python3 device_controller.py
# -> 0.0.0.0:8090; without CONTROLLER_TOKEN it exits with a message saying how to set oneCheck it with curl http://localhost:8090/health (no token needed there) and
curl -H "X-Controller-Token: <token>" http://localhost:8090/devices. It starts Appium itself if
nothing is listening. The first iOS session may build WDA, so it can take a while.
To start it at login, fill in the placeholders in
packaging/launchd/com.blackjackfruit.monkeytester-mobile-controller.plist, including
CONTROLLER_TOKEN in its EnvironmentVariables (the service refuses to start while it
still says REPLACE_WITH_TOKEN). Copy it to ~/Library/LaunchAgents/, run chmod 600 on
it (the token is in plain text), and run launchctl load on it. The signing identity
must be usable from that LaunchAgent (keychain unlocked).
The LaunchAgent runs device_controller.py from this checkout, so this README is there at
runtime: GET /docs serves it as an HTML page (token required) and /health reports
"docs": "/docs". A copy without README.md next to device_controller.py omits the docs
field instead of advertising a broken page.
| Variable | Default | Meaning |
|---|---|---|
CONTROLLER_PORT |
8090 |
HTTP port |
CONTROLLER_BIND |
0.0.0.0 |
Bind address. Anything other than loopback requires CONTROLLER_TOKEN |
CONTROLLER_TOKEN |
unset | Shared secret. Clients send it as X-Controller-Token. Required unless the bind is loopback |
CONTROLLER_MAX_UPLOAD_MB |
2048 |
Largest app upload. Bigger uploads get 413 |
APPIUM_URL |
http://127.0.0.1:4723 |
Appium. The controller spawns Appium only on a loopback host, with --address set to it, so the unauthenticated Appium never listens on the LAN |
AAPT2_BIN |
highest-versioned $ANDROID_HOME/build-tools/*/aapt2 |
aapt2, used to read an installed .apk's package name and label for app_id/app_name (see "Installing apps"). Without it Android installs still work and answer app_id: null |
Traffic is plain HTTP and the token is stored in plain text. See SECURITY.md for the threat model and what is not covered.
-
iPhone: connected by USB, trusted, unlocked and awake, with Developer Mode on.
-
iOS simulators: every available simulator in Xcode is listed and booted on first use. A simulator runs an unsigned WebDriverAgent, so it needs none of the signing settings.
-
Android emulators: every AVD in
~/.android/avdis listed and booted headless on first use. Install the emulator and an arm64 system image, then create an AVD:sdkmanager --install emulator "system-images;android-36;google_apis;arm64-v8a" avdmanager create avd -n MT_Pixel_9_API_36 -d pixel_9 \ -k "system-images;android-36;google_apis;arm64-v8a"
Simulators and emulators this service booted are shut down after 15 idle minutes (
VIRTUAL_IDLE_SHUTDOWN_SECS); at mostVIRTUAL_MAX_BOOTED(3) run at once. -
Android: USB, or wireless debugging paired with this Mac (
adb pair). Pairing is per host, so a pairing with another machine doesn't carry over. Paired phones advertised over mDNS are connected automatically every 30 s. SetADB_CONNECTto a comma-separatedIP:PORTlist to add others. -
iOS live view:
GET /devices/<id>/streamproxies WebDriverAgent's MJPEG stream (about 15 frames a second). Each iPhone or simulator uses one local port fromIOS_MJPEG_PORT_BASE(9100) upwards, so keep that range free on the Mac. See API.md, "Live stream".
POST /devices/<id>/apps takes the raw file as the body: an .apk (or a zip holding one) for
Android, an .ipa (or a zip holding a .app) for an iPhone or simulator. On success it answers:
{"ok": true, "platform": "android", "app_id": "com.example.app", "app_name": "Example"}app_idis the id to launch or uninstall the app with, read from the uploaded file: iOSCFBundleIdentifierfrom the.app'sInfo.plist; Androidaapt2 dump packagename.app_nameis iOSCFBundleDisplayName(elseCFBundleName), or Android's defaultapplication-labelfromaapt2 dump badging.- Either is
nullwhen it can't be read: no aapt2 (install Android build-tools or setAAPT2_BIN), aapt2 failing, a badInfo.plist, or an id that isn't a valid bundle id. That never fails the install; the controller logs one line saying why.
Launch the app afterwards with POST /devices/<id>/actions
{"action": "launch", "bundle_id": "<app_id>"}. See API.md.
See the top of device_controller.py for every environment variable.
For how it works inside, see ARCHITECTURE.md.
python3 -m unittest discover testsThese need ffmpeg with libx264, and use tests/fake_adb.py in place of a phone. iOS
paths that require Appium use mocks, so they don't prove a real phone behaves the same way.