Android Automation - Getting Started

abstract This section helps you quickly get started with Android Automation. To automate an Android Devices/Simulators using Sahi Pro Flowcharts, refer Android Automation - Getting Started.


Pre-requisites

Play Protect Warning

On the Android device, Google Play Protect scans for apps that are not known to Play Protect and warns the user during installation.

When running a script on the Android device or connecting to it through the Android Viewer for the first time, Sahi first installs 2 apps on the device to facilitate Android testing. Since these apps are for testing purpose and are not from the Play Store, Google Play Protect can display the following warning.

Warning: Send app for a security check?

To prevent the warning message from appearing again, do the following steps.
  1. Open the Play Store app on the device.
  2. At the top right, tap the Profile icon.
  3. Tap Play Protect from the list.
  4. Play Protect screen opens. At the top right, tap the Settings icon.
  5. The following screen opens.
  6. Tap the second option, Improve harmful app detection to switch it OFF. The screen now looks like this.
  7. Connecting the Android Viewer again or running the script will not show the Play Protect warning again.

Download and Install Sahi Pro Mobile Add-on

You need to install Sahi Pro Mobile Add-on before you can proceed with the Quick Tutorial

Quick Tutorial

  1. Connect the Android Device to the system.
  2. Install the Sample Application <SAHI_INSTALLATION_FOLDER>/userdata/android/testapps/AndroidApp.apk on the device.
    • Open a Command Prompt(Windows) or Terminal Shell(Mac, Linux etc.)
    • Type cd <SAHI_INSTALLATION_FOLDER>/userdata/android/testapps and press enter.
    • Type adb install AndroidApp.apk and press enter to install the app.
  3. Start Sahi Dashboard and click on the Android button.
  4. This brings up Android Viewer in system's default browser.
  5. Now select the device from the Device Info dropdown
    Specify Application Package Name:com.sahipro.SahiTestApp
  6. Now click on Connect Session button.
  7. This will start com.sahipro.SahiTestApp application in Android device. The device screen is also visible on the Android Viewer.
  8. Launch the Controller by press ALT key and then double click on the Android Viewer window.
  9. Specify a script name (test_sample_app.sah) in Controller and click on Record button.
  10. Now on the Android Viewer click the Sample Application button.
  11. This will bring up the Login screen.
  12. Click on the textbox below User Name. This makes the element editable in the Viewer.
    Type "test" in textbox and press enter.
  13. Click on the textbox below Password. This brings up the textbox on the screenshot where you can specify the Password.
    Type "secret" in textbox and press enter.
  14. Click on the Login button. This will brings up the Add Books screen.
  15. Click on the textbox right side of Ruby. This brings the textbox on the screenshot where you can specify the number of books.
  16. Click on the textbox right side of Java. This brings the textbox on the screenshot where you can specify the number of books.
  17. Click on the textbox right side of Python. This brings the textbox on the screenshot where you can specify the number of books.
  18. Click on the Add Total button. This will sums up the cost of all three books.
  19. Press the CTRL key and hover the mouse over the textbox below Total.
    Click on the Assert button which will generate the valid assertions for that textbox.
  20. Click on the Append button which will append the assertions in the script.
  21. Stop the recording of the script by clicking on the Stop button from Controller.
  22. Check the recorded script by clicking on Editor from controller. The script looks like this.
    _setMode("ANDROID");
    _mActivateApplication("com.sahipro.SahiTestApp", true);
    _click(_button("Sample Application"));
    _setValue(_textbox("com.sahipro.SahiTestApp:id/username"), "test");
    _setPassword(_textbox("com.sahipro.SahiTestApp:id/pwd"), "MgkKEQBU");
    _click(_button("Login"));
    _setValue(_textbox("com.sahipro.SahiTestApp:id/RubyQuantity"), "1");
    _setValue(_textbox("com.sahipro.SahiTestApp:id/JavaQuantity"), "2");
    _setValue(_textbox("com.sahipro.SahiTestApp:id/PythonQuantity"), "3");
    _click(_button("Add Total"));
    _assertExists(_textbox("com.sahipro.SahiTestApp:id/Price"));
    _assertVisible(_textbox("com.sahipro.SahiTestApp:id/Price"));
    _assertEqual("1750", _getValue(_textbox("com.sahipro.SahiTestApp:id/Price")));
  23. Now come back to Sahi controller and click on Playback tab. Select the recorded script and select the Start Mode:Android
  24. Now click on the Play button. This will start the script execution. You can see the steps being executed on the actual device/simulator.
    infoWhen the script is being played back from the Controller, you CANNOT see the playback happening on the Android Viewer. The Android Viewer does not get refreshed during playback.

    You can see the result of the executed script in the Controller itself.
  25. For checking the logs of the executed script, click on the Logs link from Controller. This will open a script report of that script.

Further Reading

Android Automation in Detail
Android Accessor APIs
Mobile APIs
Working with Multiple Modes in a Single Script