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.

Connect Device

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 Connect Device.

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.

Parameters
$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.
Return Value
string Device instanceId. This can be passed to _selectDevice.

Modes Supported :
Raw Script
// 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"));
...

Sahi Pro Classic API :_connectDevice


Select Device

Selects the particular device instance. Further steps in the script will be directed to the selected instance.

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

Modes Supported :
Raw Script
// 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);
...

Sahi Pro Classic API :_selectDevice