# Mobile apps

> Send iOS, Android, React Native and Flutter screenshots to VisualCI, from XCUITest, swift-snapshot-testing, Espresso, Paparazzi, Maestro, Detox and Flutter golden tests.

Source: https://visualci.com/docs/screenshots/mobile/
All of VisualCI's documentation: https://visualci.com/llms.txt

VisualCI compares the PNG files your mobile tests already write. The tests save screenshots and pass, the CLI
uploads them, and the review happens in VisualCI instead of in committed reference images.

```sh
visualci upload --platform ios-26.1/iphone-16 build/screenshots
```

Each file's path inside the directory, without `.png`, is the snapshot's name.

## Platform tags

A screenshot from an iPhone 16 on iOS 26.1 never matches one from an iPad or another OS version pixel for pixel.
Give each OS version and device its own baseline with `--platform`, and upload each set separately:

```sh
visualci upload --platform ios-26.1/iphone-16 screenshots/iphone
visualci upload --platform ios-26.1/ipad-air screenshots/ipad
visualci upload --platform android-35/pixel-8 screenshots/android
```

Pin the simulator or emulator in CI (the device and OS version, and on Android the system image), and change the tag
when you change them: the first build on a new tag starts its own baseline instead of showing every screen as
changed. Screenshots from a laptop won't match CI's, so compare CI with CI.

Status bars change every minute. Override them before taking screenshots: `xcrun simctl status_bar booted override
--time 9:41 --batteryLevel 100` on iOS, and System UI demo mode on Android (`adb shell settings put global
sysui_demo_allowed 1`, then its `com.android.systemui.demo` broadcasts). Turn off animations, or wait for them to
finish.

## iOS

### XCUITest

Write each screenshot to a folder you upload afterwards:

```swift
func snapshot(_ name: String) {
    let dir = URL(fileURLWithPath: ProcessInfo.processInfo.environment["SCREENSHOTS_DIR"] ?? NSTemporaryDirectory())
    let png = XCUIScreen.main.screenshot().pngRepresentation
    try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
    try? png.write(to: dir.appendingPathComponent("\(name).png"))
}

func testCheckout() {
    let app = XCUIApplication()
    app.launch()
    app.buttons["Checkout"].tap()
    snapshot("checkout/summary")
}
```

UI tests run inside the simulator, which sees the Mac's file system, so `SCREENSHOTS_DIR` can be a path in the
checkout. Pass it with `TEST_RUNNER_SCREENSHOTS_DIR=$PWD/screenshots xcodebuild test ...`: xcodebuild hands
`TEST_RUNNER_`-prefixed variables to the tests without the prefix.

### swift-snapshot-testing

[swift-snapshot-testing](https://github.com/pointfreeco/swift-snapshot-testing) writes each image to a
`__Snapshots__` folder next to the test file. Stop committing those folders, and have CI record every snapshot so the
files are the current screens:

```sh
SNAPSHOT_TESTING_RECORD=all xcodebuild test -scheme SnapshotTests -destination 'platform=iOS Simulator,name=iPhone 16,OS=26.1' || true
find Tests -type d -name __Snapshots__ | while read dir; do
  visualci upload --platform ios-26.1/iphone-16 --prefix "$(basename "$(dirname "$dir")")/" "$dir"
done
```

The library fails a test whenever it records a snapshot, so the step above ignores the result. Keep the snapshot
tests in their own scheme or test plan, so that ignoring it doesn't hide other failures. Each snapshot is named by
the folder that holds the test file, the test file's name and the image's (`AppTests/CheckoutTests/testCheckout.1`).

## Android

### Paparazzi and Roborazzi

[Paparazzi](https://github.com/cashapp/paparazzi) and [Roborazzi](https://github.com/takahirom/roborazzi) render
screens on the JVM without an emulator. Their record tasks write the images and pass:

```sh
./gradlew :app:recordPaparazziDebug
visualci upload --platform paparazzi app/src/test/snapshots/images

./gradlew :app:recordRoborazziDebug
visualci upload --platform roborazzi app/build/outputs/roborazzi
```

Don't run their `verify` tasks in CI: VisualCI does the comparing. The images don't depend on a device, so a
platform tag per tool is enough.

### Espresso

Take screenshots on the emulator, then pull them off it:

```kotlin
fun snapshot(name: String) {
    val bitmap = InstrumentationRegistry.getInstrumentation().uiAutomation.takeScreenshot()
    val dir = File(InstrumentationRegistry.getInstrumentation().targetContext.getExternalFilesDir(null), "screenshots")
    File(dir, "$name.png").apply { parentFile?.mkdirs() }.outputStream().use {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, it)
    }
}
```

```sh
./gradlew :app:connectedDebugAndroidTest
adb pull /sdcard/Android/data/com.example.app/files/screenshots screenshots
visualci upload --platform android-35/pixel-8 screenshots
```

## Maestro

[Maestro](https://maestro.mobile.dev) flows take screenshots with `takeScreenshot`. The path is relative to the folder
you run Maestro from:

```yaml
appId: com.example.app
---
- launchApp
- tapOn: "Checkout"
- takeScreenshot: screenshots/checkout
```

```sh
maestro test flows/
visualci upload --platform android-35/pixel-8 screenshots
```

The same flows run on iOS simulators; upload those with an iOS tag.

## React Native: Detox

[Detox](https://wix.github.io/Detox/) saves screenshots as artifacts. Take them by name, and keep only the manual
ones:

```js
it('shows the checkout summary', async () => {
  await element(by.id('checkout')).tap()
  await device.takeScreenshot('checkout-summary')
})
```

```sh
detox test --configuration ios.sim.release --take-screenshots manual --artifacts-location artifacts/
find artifacts -name '*.png' -exec sh -c 'mkdir -p screenshots && cp "$1" "screenshots/$(basename "$1")"' _ {} \;
visualci upload --platform ios-26.1/iphone-16 screenshots
```

Detox puts each test's artifacts in its own folder, so the copy step flattens them into one folder named by the
screenshots' names. Give each screenshot a name that's unique across the suite.

## Flutter golden tests

`flutter test --update-goldens` writes every golden file and passes, so it's the command to run in CI:

```sh
flutter test --update-goldens
visualci upload --platform flutter-linux test/goldens
```

Golden images depend on the fonts and the operating system that render them, so run them on the same CI image every
time and tag them with it. Stop committing the golden files once VisualCI holds the baselines.

## Then

Send the build from your CI provider's guide ([GitHub Actions](https://visualci.com/docs/ci/github-actions.md),
[CircleCI](https://visualci.com/docs/ci/circleci.md), [Other CI providers](https://visualci.com/docs/ci/other.md)), with one `upload` per platform. Changed
screens hold the pull request's VisualCI check until someone, or a [coding agent](https://visualci.com/docs/agents.md), approves them.
