Skip to main content

Quick start: Go

Start a session on a device, launch Settings, tap a row, take a screenshot, and quit — from Go. Starts with mobium.Start(ctx, mobium.WithPlatform(...), mobium.WithApp(...)); ends with device.Quit(ctx).

Before this page: install mobium and prepare a device.

1. What you need​

Go 1.24 or later.

2. Install the client​

The Go module is published through GitHub, so this works today.

mkdir quickstart && cd quickstart
go mod init quickstart
go get github.com/mobiumdev/mobium/clients/go

3. The code​

Save this as main.go in the project folder — it is examples/go/main.go.

// Mobium quick start: start a session, drive Settings, quit.
//
// MOBIUM_PLATFORM=android go run . # or ios
package main

import (
"context"
"fmt"
"log"
"os"
"strings"
"time"

mobium "github.com/mobiumdev/mobium/clients/go"
)

// Settings is on every emulator, simulator and phone, with nothing to install.
var platforms = map[string]struct{ app, row, next string }{
"android": {"com.android.settings", "Network & internet", "text=Airplane mode"},
"ios": {"com.apple.Preferences", "General", "label=About,role=button"},
}

func main() {
platform := os.Getenv("MOBIUM_PLATFORM")
if platform == "" {
platform = "android"
}
p := platforms[platform]
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()

// 1. Start the session: the driver is started on the device and Settings
// is launched.
opts := []mobium.Option{mobium.WithPlatform(platform), mobium.WithApp(p.app)}
if d := os.Getenv("MOBIUM_DEVICE"); d != "" {
opts = append(opts, mobium.WithDevice(d))
}
device, err := mobium.Start(ctx, opts...)
if err != nil {
log.Fatal(err)
}
// 5. Quit when main returns: the device's session is closed.
defer func() {
if err := device.Quit(ctx); err != nil {
log.Fatal(err)
}
fmt.Println("session ended")
}()
s := device.Session()
fmt.Printf("session on %s (%s, %s)\n", s.Device, s.Platform, s.Driver)

// 2. Map the screen: every element you can act on, each with a @ref.
elements, err := device.Map(ctx)
if err != nil {
log.Fatal(err)
}
for _, e := range elements[:min(5, len(elements))] {
fmt.Println(" ", e.Ref, e.Label, "("+e.Role+")")
}

// 3. Tap a row by its ref, then wait for the screen it opens. A row's
// label can carry its summary too ("Network & internet Mobile, Wi-Fi,
// ..."), so match its start.
ref := ""
for _, e := range elements {
if strings.HasPrefix(e.Label, p.row) {
ref = e.Ref
break
}
}
if ref == "" {
log.Fatalf("no row starting with %q", p.row)
}
if err := device.Tap(ctx, ref); err != nil {
log.Fatal(err)
}
if _, err := device.WaitFor(ctx, p.next, nil); err != nil {
log.Fatal(err)
}
fmt.Println("opened", p.row)

// 4. Take a screenshot.
name := "quickstart-" + platform + ".png"
if _, err := device.Screenshot(ctx, name); err != nil {
log.Fatal(err)
}
fmt.Println("saved", name)
}

Settings is on every Android and iOS device with nothing to install. The row and the screen after it are the only things that differ by platform.

4. Run it​

Android​

MOBIUM_PLATFORM=android go run .

What it printed on an Android 15 emulator:

waiting for the UiAutomator2 server to start...
session on emulator-5554 (android, uiautomator2)
@e1 settings_homepage_container (list)
@e2 Profile picture, double tap to open Google Account (button)
@e3 Search settings (button)
@e4 main_content_scrollable_container (list)
@e5 Network & internet Mobile, Wi‑Fi, hotspot (button)
opened Network & internet
saved quickstart-android.png
session ended
After startAfter the tap
Settings, as start left itThe screen the tap opened

iOS​

MOBIUM_PLATFORM=ios go run .

What it printed on an iOS 26.5 simulator:

waiting for WebDriverAgent to start...
session on 457C7DC2-C706-45D9-8D68-1D26953E28B1 (ios, wda)
@e1 Apple Account, Sign in to access your iCloud data, the App Store, Apple services, and more. (button)
@e2 General (button)
@e3 Accessibility (button)
@e4 Action Button (button)
@e5 Apple Intelligence & Siri (button)
opened General
saved quickstart-ios.png
session ended
After startAfter the tap
Settings, as start left itThe screen the tap opened

The first start on a device is slow: it installs the UiAutomator2 server on Android, and on a real iPhone builds WebDriverAgent. Later starts take seconds.

Notes​

  • defer device.Quit(ctx) right after a successful Start is the usual shape. A second Quit does nothing, so an explicit one before it is fine.
  • The example in the repository builds against the client beside it, through a replace line in its go.mod; your own module uses go get.
  • mobium.Connect still exists: it opens a connection without touching the device, and Close leaves the session open.
  • With more than one device of the session's platform running — two Android devices, or two among the booted simulators and attached iPhones — start refuses to guess and lists them. One Android device and one iOS device are not ambiguous: the platform picks. Name one with MOBIUM_DEVICE=<serial or UDID>, which the example passes on as the device.

Next: the rest of the tool surface, and setting up phones and simulators. Changing the client itself? DEVELOPMENT.md is the contributor's guide.