Mobile Common APIs
This page lists the APIs common to both Android and iOS. These APIs have a _m prefix in camel case, example: _mSwipeLeft. These APIs work on an Android device/simulator as well as on an iOS device/simulator.
Action APIs
Section titled “Action APIs”Activate Application
Section titled “Activate Application”Modes AndroidiOS
Activates an application.
Parameters
| Name | Type | Description |
|---|---|---|
$bundleId | string | Android: Package name of the application iOS: Bundle ID of the application |
$force | booleanoptional | Flag to specify whether to start an application with clean state. If not specified, default false will be used. |
Raw script
_mActivateApplication("com.android.settings", true);_mActivateApplication("com.apple.Preferences");Sahi Pro Classic API _mActivateApplication
MLong Click
Section titled “MLong Click”Modes AndroidiOS
MLong Click simulates a user’s long click on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Mobile Element | Mobile element to long click on |
$duration | integeroptional | Duration for the long click. |
Raw script
_mLongClick(_button("Click Me"));Sahi Pro Classic API _mLongClick
MPinch Open
Section titled “MPinch Open”Modes AndroidiOS
Performs a pinch open gesture on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Mobile Element | Mobile element to perform pinch open action on. |
$percent | float | Android: Optional. The size of the pinch as a percentage of this element's size. If not specified, a value of 1.0 will be used. iOS: Needs to be specified. Scale of the pinch gesture. Has to be greater than 1 |
$speed | integer|float | Android: Optional. Speed to perform the pinch gesture, to be specified as an integer . If not specified, default value will be used specific to device. iOS: Needs to be specified. Velocity of the pinch in scale factor per second, to be specified as a float. Has to be greater than or equal to 0. |
Raw script
Android:_mPinchOpen(_image("abc"));_mPinchOpen(_image("abc"), 0.5, 2000);
iOS:_mPinchOpen(_image("abc"), 1.5, 0.8);Sahi Pro Classic API _mPinchOpen
MPinch Close
Section titled “MPinch Close”Modes AndroidiOS
Performs a pinch close gesture on the given element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Mobile Element | Mobile element to perform pinch close action on. |
$percent | float | Android: Optional. The size of the pinch as a percentage of this element's size. If not specified, a value of 1.0 will be used. iOS: Needs to be specified. Scale of the pinch gesture. Should be between 0 and 1 |
$speed | integer|float | Android: Optional. Speed to perform the pinch gesture, to be specified as an integer. If not specified, default value will be used specific to device. iOS: Needs to be specified. Velocity of the pinch in scale factor per second, to be specified as a float. Has to be lesser than 0. If a positive value is given, it will be converted to a negative value. |
Raw script
Android:_mPinchClose(_image("abc"));_mPinchClose(_image("abc"), 0.5, 2000);
iOS:_mPinchClose(_image("abc"), 0.5, -0.8);Sahi Pro Classic API _mPinchClose
Press Home
Section titled “Press Home”Modes AndroidiOS
Brings up the Home screen.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Raw script
_mPressHome();Sahi Pro Classic API _mPressHome
MSet Orientation
Section titled “MSet Orientation”Modes AndroidiOS
Rotate the device screen based on the orientation type.
Parameters
| Name | Type | Description |
|---|---|---|
$orientationType | Orientation type: can be PORTRAIT, PORTRAIT_UPSIDE_DOWN, LANDSCAPE_LEFT, LANDSCAPE_RIGHT |
Raw script
// Rotate the screen in portrait mode._mSetOrientation("PORTRAIT");
// Rotate the screen upside down in portrait mode._mSetOrientation("PORTRAIT_UPSIDE_DOWN");
// Rorate the screen to the left side._mSetOrientation("LANDSCAPE_LEFT");
// Rorate the screen to the right side._mSetOrientation("LANDSCAPE_RIGHT");Sahi Pro Classic API _mSetOrientation
Fetch APIs
Section titled “Fetch APIs”MOSVersion
Section titled “MOSVersion”Modes AndroidiOS
Android: Returns the SDK Level of the Android device, for example: 21 for Lollypop, 20 for Kitkat etc.
iOS: Returns the iOS version of the iOS device, for example: 11.2.6
Parameters
| Name | Type | Description |
|---|---|---|
None |
Returns
string | Android: SDK version of device/emulator. iOS: iOS version of device/simulator. |
Raw script
var $version = _mOSVersion();Sahi Pro Classic API _mOSVersion
MGet Orientation
Section titled “MGet Orientation”Modes AndroidiOS
Returns device orientation, for example: PORTRAIT, PORTRAIT_UPSIDE_DOWN, LANDSCAPE_LEFT, LANDSCAPE_RIGHT
Parameters
| Name | Type | Description |
|---|---|---|
None |
Returns
string | Device orientation. |
Raw script
var $orientation = _mGetOrientation();Sahi Pro Classic API _mGetOrientation
Swipe APIs
Section titled “Swipe APIs”These APIs are used to induce swipe action on an element, for example: to delete an email from the list.
Swipe Up
Section titled “Swipe Up”Modes AndroidiOS
Swipes up on the element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Mobile Elementoptional | Element to be swiped. If not specified, then it will consider the top most element to perform swipe gesture. |
$percent | floatoptional | Android: The length of the swipe as a percentage of the element's size. If not specified, a value of 1.0 will be used. iOS: It is not used. |
$speed | integeroptional | Android: Speed to perform swipe. If not specified, default value will be used specific to device. iOS: It is not used. |
Raw script
_mSwipeUp();_mSwipeUp(_mScrollView("Test"));_mSwipeUp(_aRelativeLayout("Swipe Me"), 0.5, 1000);Sahi Pro Classic API _mSwipeUp
Swipe Down
Section titled “Swipe Down”Modes AndroidiOS
Swipes down on the element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Mobile Elementoptional | Element to be swiped. If not specified, then it will consider the top most element to perform swipe gesture. |
$percent | floatoptional | Android: The length of the swipe as a percentage of the element's size. If not specified, a value of 1.0 will be used. iOS: It is not used. |
$speed | integeroptional | Android: Speed to perform swipe. If not specified, default value will be used specific to device. iOS: It is not used. |
Raw script
_mSwipeDown();_mSwipeDown(_mScrollView("Test"));_mSwipeDown(_aRelativeLayout("Swipe Me"), 0.5, 1000);Sahi Pro Classic API _mSwipeDown
Swipe Left
Section titled “Swipe Left”Modes AndroidiOS
Swipes left on the element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Mobile Elementoptional | Element to be swiped. If not specified, then it will consider the top most element to perform swipe gesture. |
$percent | floatoptional | Android: The length of the swipe as a percentage of the element's size. If not specified, a value of 1.0 will be used. iOS: It is not used. |
$speed | integeroptional | Android: Speed to perform swipe. If not specified, default value will be used specific to device. iOS: It is not used. |
Raw script
_mSwipeLeft();_mSwipeLeft(_mScrollView("Test"));_mSwipeLeft(_aRelativeLayout("Swipe Me"), 0.5, 1000);Sahi Pro Classic API _mSwipeLeft
Swipe Right
Section titled “Swipe Right”Modes AndroidiOS
Swipes right on the element.
Parameters
| Name | Type | Description |
|---|---|---|
$element | Mobile Elementoptional | Element to be swiped. If not specified, then it will consider the top most element to perform swipe gesture. |
$percent | floatoptional | Android: The length of the swipe as a percentage of the element's size. If not specified, a value of 1.0 will be used. iOS: It is not used. |
$speed | integeroptional | Android: Speed to perform swipe. If not specified, default value will be used specific to device. iOS: It is not used. |
Raw script
_mSwipeRight();_mSwipeRight(_mScrollView("Test"));_mSwipeRight(_aRelativeLayout("Swipe Me"), 0.5, 1000);Sahi Pro Classic API _mSwipeRight
WebView APIs
Section titled “WebView APIs”MStart Web Context
Section titled “MStart Web Context”Modes AndroidiOS
Changes the execution mode to ANDROID_WEBVIEW/IOS_WEBVIEW and starts the execution of the following steps as Web.
Parameters
| Name | Type | Description |
|---|---|---|
$packageName | string | Android: application package name iOS: Not required |
Raw script
Android:_mStartWebContext("com.sahipro.SahiTestApp");
iOS:_mStartWebContext();Sahi Pro Classic API _mStartWebContext
MStop Web Context
Section titled “MStop Web Context”Modes AndroidiOS
Stop the execution of following steps in Android/iOS WebView.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Sahi Pro Classic API _mStopWebContext
Mobile Browser APIs
Section titled “Mobile Browser APIs”Launch New Mobile Browser
Section titled “Launch New Mobile Browser”Modes AndroidiOS
Returns the browserInstanceId Launches a new browser instance. This instance does not share cookies with the base browser.
Once launched, steps can be directed to different browser instances via _selectBrowser
Parameters
| Name | Type | Description |
|---|---|---|
$deviceId | string | deviceId of the device on which the browser will be launched. |
$url | stringoptional | URL to load on opening new browser instance. If URL is null, the Start URL of the script is launched. |
Returns
string | Browser instanceId. This can be passed to _selectBrowser |
Raw script
// for Android_setMode("ANDROID_BROWSER");// _connectDevice will return the deviceId and use that deviceId as parameter in _launchNewMobileBrowservar $deviceId = _connectDevice();// It will launch browser with Specified URL.var $sessionId = _launchNewMobileBrowser($deviceId, "https://sahitest.com/demo/");_selectBrowser($sessionId);...
// for iOS_setMode("IOS_BROWSER");// _connectDevice will return the deviceId and use that deviceId as parameter in _launchNewMobileBrowservar $deviceId = _connectDevice();// It will launch browser with Specified URL.var $sessionId = _launchNewMobileBrowser($deviceId, "https://sahitest.com/demo/");_selectBrowser($sessionId);...Sahi Pro Classic API _launchNewMobileBrowser
Launch New Mobile Browser2
Section titled “Launch New Mobile Browser2”Modes AndroidiOS
Returns the browserInstanceId.
Launches a new browser instance. This instance does not share cookies with the base browser.
Once launched, steps can be directed to different browser instances via _selectBrowser
Parameters
| Name | Type | Description |
|---|---|---|
$deviceId | stringoptional | deviceId of the device on which the browser will be launched, If deviceId is null, the Start DeviceId of the script is used. |
$url | stringoptional | URL to load on opening new browser instance. If URL is null, the Start URL of the script is launched. |
Returns
string | Browser instanceId. This can be passed to _selectBrowser |
Raw script
// For Android Browser playback_setMode("ANDROID_BROWSER");var $sessionId = _launchNewMobileBrowser2();_selectBrowser();...
// For iOS Browser playback_setMode("IOS_BROWSER");var $sessionId = _launchNewMobileBrowser2();_selectBrowser();...Sahi Pro Classic API _launchNewMobileBrowser2
Other APIs
Section titled “Other APIs”Device Info
Section titled “Device Info”Modes AndroidiOS
Returns device information as an associative array. Array contains following information:
- name: Name of the device specified in
userdata/config/(android_devices.xml or ios_devices.xml) - displayName: Display name of the device specified in
userdata/config/(android_devices.xml or ios_devices.xml) - deviceName: Device name
- deviceId: Device Id.
- version: Device OS version
Parameters
| Name | Type | Description |
|---|---|---|
None |
Returns
object | Device object |
Raw script
var $deviceInfo = _deviceInfo();var $displayName = $deviceInfo["displayName"];// orvar $displayName = $deviceInfo.displayName;Sahi Pro Classic API _deviceInfo

