Skip to content

Latest commit

 

History

History
139 lines (106 loc) · 5.52 KB

dotnet.md

File metadata and controls

139 lines (106 loc) · 5.52 KB

Quick start: .NET

Start a session on a device, launch Settings, tap a row, take a screenshot, and quit — from .NET. Starts with Device.Builder().Platform(...).App(...).Start(); ends with device.Quit(), or leaving the using block.

Before this page: install mobium and prepare a device.

1. What you need

The .NET SDK 8 or later. The package targets netstandard2.0, so it also runs on .NET Framework 4.6.1+.

2. Install the client

Not on NuGet yet. Until the first release it installs from a local package built from a clone; after it, dotnet add package Mobium needs nothing else.

git clone https://github.com/mobiumdev/mobium.git ~/mobium
dotnet pack ~/mobium/clients/dotnet/Mobium -o ~/mobium-packages
dotnet new console -o quickstart && cd quickstart
dotnet add package Mobium --version 0.1.0 --source ~/mobium-packages

3. The code

Save this as Program.cs in the project folder, replacing the one dotnet new generated — it is examples/dotnet/Program.cs.

// Mobium quick start: start a session, drive Settings, quit.
//
//   MOBIUM_PLATFORM=android dotnet run    # or ios
using System;
using System.Collections.Generic;
using System.Linq;
using Mobium;

// Settings is on every emulator, simulator and phone, with nothing to install.
var platforms = new Dictionary<string, (string App, string Row, string Next)>
{
    ["android"] = ("com.android.settings", "Network & internet", "text=Airplane mode"),
    ["ios"] = ("com.apple.Preferences", "General", "label=About,role=button"),
};
var platform = Environment.GetEnvironmentVariable("MOBIUM_PLATFORM") ?? "android";
var p = platforms[platform];

var builder = Device.Builder().Platform(platform).App(p.App);
var serial = Environment.GetEnvironmentVariable("MOBIUM_DEVICE");
if (serial != null) builder.OnDevice(serial);

// 1. Start the session: the driver is started on the device and Settings is
//    launched. The using block quits when it ends, even on an exception.
using (var device = builder.Start())
{
    var s = device.Session!;
    Console.WriteLine($"session on {s.Device} ({s.Platform}, {s.Driver})");

    // 2. Map the screen: every element you can act on, each with a @ref.
    var elements = device.Map();
    foreach (var e in elements.Take(5)) Console.WriteLine("  " + e);

    // 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.
    var row = elements.First(e => e.Label.StartsWith(p.Row, StringComparison.Ordinal));
    device.Tap(row.Ref);
    device.WaitFor(p.Next);
    Console.WriteLine($"opened {p.Row}");

    // 4. Take a screenshot.
    device.Screenshot($"quickstart-{platform}.png");
    Console.WriteLine($"saved quickstart-{platform}.png");
}
// 5. The using block has quit: the device's session is closed.
Console.WriteLine("session ended");

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 dotnet run

What it printed on an Android 17 emulator:

waiting for the UiAutomator2 server to start...
session on emulator-5556 (android, uiautomator2)
  @e1 Search Settings (button)
  @e2 main_content_scrollable_container (list)
  @e3 Google Services & preferences (button)
  @e4 Network & internet Mobile, Wi‑Fi, hotspot (button)
  @e5 Connected devices Bluetooth, pairing (button)
opened Network & internet
saved quickstart-android.png
session ended
After start After the tap
Settings, as start left it The screen the tap opened

iOS

MOBIUM_PLATFORM=ios dotnet run

What it printed on an iOS 27.0 simulator:

waiting for WebDriverAgent to start...
the simulator is on its home screen: a new session starts WebDriverAgent, whose runner takes the foreground and leaves it to the home screen, not to the app that was in front — app_launch brings an app back...
session on B2580DC0-1C76-4B2E-ADD3-802A58033078 (ios, wda)
  @e1 com.apple.settings.sidebar.collectionView (list)
  @e2 Apple Account, Sign in to access your iCloud data, the App Store, Apple services, and more. (button)
  @e3 Optimizing Search and Siri (button)
  @e4 General (button)
  @e5 Accessibility (button)
opened General
saved quickstart-ios.png
session ended
After start After the tap
Settings, as start left it The 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

  • A using block around Start() quits when it ends, even on an exception. Around Connect() it only closes the connection.
  • 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.