Sahi Documentation

Multiple Mobile Device Instances in Single Script

One can connect to multiple mobile devices from inside a script, and interact with them. These devices may be a mix of Android and iOS devices.

_connectDevice

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
NANANANA7.5.0NANA

Available for modes: Android | iOS

_connectDevice([$deviceId])

Arguments
$deviceIdstring optional Android: Id of device to connect to. If not specified, the default device is connected to.
iOS: Connection params for a device. See the Details section. If not specified, the default device is connected to.

Returns
string Device instanceId. This can be passed to _selectDevice.

Details

Connects to a Mobile Device. The Mobile device can be an Android Device/Emulator or an iOS Device/Simulator.

To connect to an Android Device/Emulator, Mode has to be set to ANDROID, using _setMode before calling this API.
To connect to an iOS Device/Simulator, Mode has to be set to IOS, using _setMode before calling this API.

Once connected, steps can be directed to different device instances via _selectDevice using deviceInstanceId returned from _connectDevice.

For an iOS Device, the connection params is of the form "platform=iOS,id=" + deviceUUID, where deviceUUID is the UUID of the iOS Device.
For an iOS Simulator, the connection params is of the form "platform=iOS Simulator,id=" + simulatorUUID, where simulatorUUID is the UUID of the iOS Simulator.

To help the end user with the correct connection params, two helper functions have been added in <SAHI_INSTALLATION_FOLDER>/userdata/scripts/sahitests/ios/helperFunctions.sah.
  • To get the connection params for an iOS Device, call connectionParamsForDevice($deviceId) where $deviceId is the Device UUID.
  • To get the connection params for an iOS Simulator, call connectionParamsForSimulator($simulatorId) where $simulatorId is the Simulator UUID.
NOTE that helperFunctions.sah needs to be included using _include with the appropriate relative path.

// Android
// Default deviceId
_setMode("ANDROID");
_connectDevice(); // Connects to the default Android Device/Emulator.
...

// Explicit Emulator Id
_setMode("ANDROID");
var $id = _connectDevice("192.168.140.101:5555");
...

// iOS
// Default deviceId
_setMode("IOS");
_connectDevice(); // Connects to the default iOS Device/Simulator.
...

// Explicit Simulator Id
_setMode("IOS");
var $id = _connectDevice(connectionParamsForSimulator("183AA1E6-609B-48AC-9392-6ABF4C31280E"));
...

// Explicit Device Id
_setMode("IOS");
var $id = _connectDevice(connectionParamsForDevice("f1cb463919c46a9b81743eae3aef5fcb8793ec53"));
...


_selectDevice

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-OnAI Assist Add-On
NANANANA7.5.0NANA

Available for modes: Android | iOS

_selectDevice([$deviceInstanceId])

Arguments
$deviceInstanceIdstring optional deviceInstanceId to forward further steps to. If not specified, steps are forwarded to the default device that the script was started with.

Returns
null

Details

Selects the particular device instance. Further steps in the script will be directed to the selected instance.
// Android
// Default Device
_setMode("ANDROID");
_connectDevice(); // Connects to the default Android Device/Emulator.
_selectDevice(); // Selects the default Android Device/Emulator.
_mActivateApplication("com.android.settings", true);
...

// Explicit Emulator Id
_setMode("ANDROID");
var $id = _connectDevice("192.168.140.101:5555");
_selectDevice($id);
_mActivateApplication("com.android.settings", true);
...

// iOS
// Default Device
_setMode("IOS");
_connectDevice(); // Connects to the default iOS Device/Simulator.
_selectDevice(); // Selects the default iOS Device/Simulator.
_mActivateApplication("com.apple.Preferences", true);
...

// Explicit Simulator Id
_setMode("IOS");
var $id = _connectDevice(connectionParamsForSimulator("183AA1E6-609B-48AC-9392-6ABF4C31280E"));
_selectDevice($id);
_mActivateApplication("com.apple.Preferences", true);
...

// Explicit Device Id
_setMode("IOS");
var $id = _connectDevice(connectionParamsForDevice("f1cb463919c46a9b81743eae3aef5fcb8793ec53"));
_selectDevice($id);
_mActivateApplication("com.apple.Preferences", true);
...