Skip to content
Get Free Trial

Automating Android Apps - Details

This page describes Android Automation in detail. If you wish to get a quick start, read Android Automation - Getting Started.

  • Supports running scripts against a connected Android device or emulator using SDK 18 and above.
  • Android Viewer and Controller allow easy interaction with the connected device.
  • Scripts can be recorded from the Android Viewer and Object spy features are also available.
  • Android function syntax is similar to Sahi Pro functions for browser testing.
  • Single script can interact with different modes like Browser, Desktop, Mobile etc.
  • Does not support testing Android device or emulator below SDK 18. (Run adb shell getprop ro.build.version.sdk to verify)
  • Does not support run in parallel from the editor, if only one device is connected.

Android Viewer allows an end user to view the Android device/emulator screen on a Desktop browser. The user can launch the Controller and then identify Android elements by pressing the Ctrl (⌘ on Mac) key and hovering the mouse over the element.

This section details how to use the Android Viewer.

  1. A User can launch Android Viewer directly from Sahi Dashboard by clicking Android button.

    This brings up Android Viewer in system’s default browser. If the user wants to launch Android Viewer in a specific browser then, open that Sahi browser and then click on the Android Viewer link from the start page, this brings up Android Viewer in that browser.

  2. Device Info: shows the list of the connected device to the system
    Application Package Name: allows an end user to start an application by it’s package name which is already installed in that device. Leave it blank if you wants to start the device session in its current state of the screen.

  3. Make sure that the device is connected through a USB cable or over Wi-Fi. Click on Connect Session. If the connection is successful, you will see the device screenshot in a short while.

  4. When some action is performed from the Android Viewer or from the Controller, the screenshot will be refreshed a few times, to display the latest device state. However, if actions are performed on the device manually, the screenshot will not get refreshed. Click on the Refresh button manually to refresh the screenshot.

  5. One can interact with the device from the Android Viewer itself without launching the Controller.

  6. Launch the Controller by clicking on the Controller button.

  7. You can now start identifying elements by pressing the Ctrl (⌘ on Mac) key and hovering the mouse over elements on the Desktop screenshot. You will notice the Accessor textbox on the Controller, showing the accessor for the element. The Alternatives dropdown displays the alternatives for the accessor.

These buttons perform various actions on the Android device and these actions are recorded while recording a script. These will give quick access to Android assist functions during debugging/script recording.

  • is used to swipe up on the screen.

  • is used to swipe down on the screen.

  • is used to swipe left on the screen.

  • is used to swipe right on the screen.

  • is used to call up the menu of the app on the display.

  • will press enter in the text field.

  • is used to open search field (if present) within an application.

  • will show all recent open apps.

  • is used for navigating to the home screen.

  • simulates the back action.

Security restrictions may prohibit Android Viewer from capturing the screen shot to be displayed. In such a case, you can use the Screenshot Outline View feature to toggle the outline view.
For example, lets assume that for the below screen, Android Viewer is unable to take the screenshot. To enable the outline view, check the highlighted checkbox in Android Viewer.

After checking it, the Android Viewer will display the outline view like this.

Now you will be able to identify the elements in controller.

For configuring the Android devices/emulators, do the following:

  1. Click All Settings on the Dashboard. The All Settings window opens.

  2. Click Sahi Pro Settings. The Sahi Pro Settings page opens.

    The Sahi Pro Dashboard with the All Settings window open, Sahi Pro Settings and the All Settings button highlighted

In the menu on the left, open Browsers And Devices and click Android Device Definitions (android_devices.xml).

<androidDevices>
<!-- Moto device with 2 capacity -->
<androidDevice>
<name>Moto</name>
<displayName>Moto G5</displayName>
<deviceId>XXXX</deviceId>
<deviceId>YYYY</deviceId>
</androidDevice>
</androidDevices>
ElementValue
nameKey by which this androidDevice will be referred. This can be any value without spaces.
displayNameBetter name displayed in Android Viewer.
deviceIdId of the device, multiple Id can be added under same name.
you can get the Device Id by executing this command
adb devices

This is similar to recording a Sahi script for normal browser testing.

  1. To record a script, launch the Controller from the Android Viewer page, choose a script to record and press Record.
  2. Actions done from the Android Viewer page are recorded automatically.
    • Hovering the mouse over an editable element will overlay a textbox where you can type text. After entering the text, press Enter or click outside the textbox. This will cause the text content to be sent to the device. The relevant step (_setValue, _setPassword etc.) will get recorded automatically.
    • The user can click on any non-editable element on the Viewer. This will click the element on the device. The _click step will get recorded automatically.
  3. Steps can also be recorded from the Controller. You can use Click, Set, Assert, choose Other Actions, Test an expression from Evaluate Expression to perform an action on the Device. But you will need to record these steps explicitly by clicking on Append.
  4. Since the device screenshot gets refreshed after a Controller action is performed, the user can continue to interact with the changing screen from the Android Viewer itself. If the device screenshot is not refreshed correctly, click on the Refresh button manually to reflect the current screenshot.
  • Android Device must be connected on the same network in which Sahi Pro is running.
  • Configure the proxy in Android Device.

For recording Android application which contains Webview (Hybrid contents), open the application in Android Viewer and go to the screen which contains webview.
If the screen is displaying hybrid contents then there will be one checkbox visible below Refresh button.

  1. Check the checkbox to identify/record hybrid elements as Web.

  2. Press the Ctrl (⌘ on Mac) key and mouse over the hybrid element.

  3. Element’s accessor will be displayed in Sahi Controller.

  4. To record the hybrid app, perform the actions directly from Android Device.

  5. Sample script will look like this

    _setMode("ANDROID");
    _mActivateApplication("com.sahipro.SahiTestApp", true);
    _setAccessorIgnoreCase(true);
    _click(_button("Test Web View"));
    _setValue(_textbox("com.sahipro.SahiTestApp:id/editText"), "https://sahitest.com/demo/training");
    _click(_button("GO"));
    // _aStartWebContext will change the mode to ANDROID_WEBVIEW
    _aStartWebContext("com.sahipro.SahiTestApp");
    _setValue(_textbox("user"), "test");
    _setValue(_password("password"), "secret");
    _click(_submit("Login"));
    // _aStopWebContext will revert back the mode to ANDROID
    _aStopWebContext();

Once a script is recorded, you can run it from the Controller itself, with Run.

  1. Choose the script.
  2. Select the Start Mode:Android.
  3. You can leave the Start URL blank. This is not used.
  4. Click on Play to execute the script.
  5. Click on Logs to view the logs of the executed script.

User can run the script/suite/scenario from the Editor as well.

  1. Choose the script/suite/scenario from the Editor and click Run.
  2. Select the Start Mode:Android.
  3. No need to choose any browser.
  4. Choose a device to execute a script from Android Device dropdown.
  5. You can leave the Start URL blank. This is not used.
  6. Press Run to run the script.

User can generate an ANT target from the Editor for a script/suite/scenario and run using ANT.

  1. Choose the script/suite/scenario from the Editor and click Run.
  2. Select the Start Mode:Android.
  3. You do not need to choose any browser.
  4. Choose a device to execute a script from Android Device: dropdown.
  5. You can leave the Start URL blank. This is not used.
  6. Press Create ANT Target to generate the ANT target.

Android Accessor functions
Mobile functions