Starting Tested Android Applications

Applies to TestComplete 14.30, last modified on November 21, 2019

Android applications consist of individual activities. An activity represents a single screen with user interface elements to interact with. The Android manifest file specifies which activity to use as a default activity when starting an application.

When recording a test, you can run an application manually, for example, by touching the application icon on the screen of the device. However, this makes your tests dependent on certain UI elements and requires that additional user actions be recorded and played back.

To make your tests more stable, you can automate running Android applications and start the default activity for the needed package directly from your tests. This topic describes the approaches you can use to start your Android application in TestComplete.

Note that an Android package must be installed on the device before you run it. You can enable the Deploy to the device on start application option to command TestComplete to automatically install the package to the device before starting recording or running tests. This approach frees you from installing the needed package manually before running tests. For more information, see Installing Packages on Devices.

Running From the TestComplete IDE

The Tested Applications collection in TestComplete projects stores launch information on your applications under test. You can add your Android application packages to the collection and launch them from the TestComplete IDE during the test recording or at design time.

Note: An application is launched if its Launch application from tests property is enabled in the TestedApps editor. You can modify the value of this property in the Basic Parameters section of the TestedApps editor or from scripts.
Running Applications During Test Recording

Before you start recording a test for your Android application, add this application to the Tested Applications collection of your project. For information on how to do this, see Adding Android Applications to the List of Tested Applications.

During the test recording, do the following:

  • Choose the desired tested application from the Run Apps drop-down menu on the Mobile Screen window’s toolbar.

-- or --

  • Choose the desired tested application from the Run App | Tested Application drop-down menu of the Recording toolbar.

    To start all available tested applications, choose Run All from the drop-down menu.

If you run the tested application from the Recording toolbar, TestComplete records this action as the Run TestedApp keyword test operation or as the TestedApps.AndroidAppName.Run or TestedApps.RunAll methods. This frees you from inserting the operation or method calls manually after the recording is over.

Running Applications at Design Time

If the Tested Applications collection stores launch information on your Android application under test, to launch the application, you can do the following:

  • Open the Mobile Screen window that displays the screen of the target Android device and choose the desired application from the Run Apps drop-down menu.

-- or --

  • Right-click the desired application package in the TestedApps editor and choose Run selected from the context menu.

    To install all the packages and launch all the applications, choose Run All.

Autorunning on Recording

To command TestComplete to start an application automatically on recording, enable the Autorun application on recording option for your tested Android application. When you start recording, the default activity of the given package will start automatically on the device.

Running From Keyword Tests

To run the default activity for a package from your keyword test, call the PackageManager.LaunchPackage method using the Call Object Method or Run Code Snippet operation. For more information on parameters of the operations, see the description of these operations.

If your application under test is added to the Tested Applications collection, you can also run it by using the Run TestedApp operation.

Running From Scripts

To launch the package installed on your Android device and start the default activity for that package from script tests, use the Mobile.Device.PackageManager.LaunchPackage method. This method runs the specified application and then starts the default activity in it.

The code below demonstrates how you can use this method:

JavaScript, JScript

var PackObj = Mobile.Device().PackageManager.GetPackageFromAPK("D:\\Work\\MyPackage.apk");
// Install the package if it has not been installed yet
Mobile.Device().PackageManager.InstallPackage(PackObj);
Mobile.Device().PackageManager.LaunchPackage(PackObj);

Python

PackObj = Mobile.Device().PackageManager.GetPackageFromAPK("D:\\Work\\MyPackage.apk");
# Install the package if it has not been installed yet
Mobile.Device().PackageManager.InstallPackage(PackObj);
Mobile.Device().PackageManager.LaunchPackage(PackObj);

VBScript

Set PackObj = Mobile.Device.PackageManager.GetPackageFromAPK("D:\Work\MyPackage.apk")
' Install the package if it has not been installed yet
Mobile.Device.PackageManager.InstallPackage(PackObj)
Mobile.Device.PackageManager.LaunchPackage(PackObj)

DelphiScript

PackObj := Mobile.Device.PackageManager.GetPackageFromAPK('D:\Work\MyPackage.apk');
// Install the package if it has not been installed yet
Mobile.Device.PackageManager.InstallPackage(PackObj);
Mobile.Device.PackageManager.LaunchPackage(PackObj);

C++Script, C#Script

var PackObj = Mobile["Device"]["PackageManager"]["GetPackageFromAPK"]("D:\\Work\\MyPackage.apk");
// Install the package if it has not been installed yet
Mobile["Device"]["PackageManager"]["InstallPackage"](PackObj);
Mobile["Device"]["PackageManager"]["LaunchPackage"](PackObj);

If the Tested Applications collection stores the launch information on your Android application, you can use the TestedApps.AndroidAppName.Run method to start the application.

The code below demonstrates how you can use the method:

JavaScript, JScript

Mobile.SetCurrent("MyDevice");
TestedApps.Orders.Run();

Python

Mobile.SetCurrent("MyDevice");
TestedApps.Orders.Run();

VBScript

Mobile.SetCurrent("MyDevice")
TestedApps.Orders.Run

DelphiScript

Mobile.SetCurrent('MyDevice');
TestedApps.Orders.Run;

C++Script, C#Script

Mobile["SetCurrent"]("MyDevice");
TestedApps["Orders"]["Run"]();

Calling Applications Running in the Background

To invoke an application running in the background, you can use the ShellExecute method. Pass your application’s package name as the parameter:

JavaScript, JScript

function AndroidRunBackground()
{
  // Invoke a test application from the background
  var m = Mobile.Device("MyDevice");
  m.ShellExecute("monkey -p example.MyApp -c android.intent.category.LAUNCHER 1");
  
  // Test the application
  m.Process("MyApp").RootLayout("").Layout("buttons_layout").Button("editButton").ClickButton();
}

Python

def AndroidRunBackground():
  # Invoke a test application from the background
  m = Mobile.Device("MyDevice")
  m.ShellExecute("monkey -p example.MyApp -c android.intent.category.LAUNCHER 1")
  
  # Test the application
  m.Process("MyApp").RootLayout("").Layout("buttons_layout").Button("editButton").ClickButton()

VBScript

Sub AndroidRunBackground()
  ' Invoke a test application from the background
  Set m = Mobile.Device("MyDevice")
  m.ShellExecute("monkey -p example.MyApp -c android.intent.category.LAUNCHER 1")
  
  ' Test the application
  m.Process("MyApp").RootLayout("").Layout("buttons_layout").Button("editButton").ClickButton()
End Sub

DelphiScript

function AndroidRunBackground;
var m;
begin
  // Invoke a test application from the background
  m := Mobile.Device('MyDevice');
  m.ShellExecute('monkey -p example.MyApp -c android.intent.category.LAUNCHER 1');
  
  // Test the application
  m.Process('MyApp').RootLayout('').Layout('buttons_layout').Button('editButton').ClickButton();
end;

C++Script, C#Script

function AndroidRunBackground()
{
  // Invoke a test application from the background
  var m = Mobile["Device"]("MyDevice");
  m["ShellExecute"]("monkey -p example.MyApp -c android.intent.category.LAUNCHER 1");
  
  // Test the application
  m["Process"]("MyApp")["RootLayout"]("")["Layout"]("buttons_layout")["Button"]("editButton")["ClickButton"]();
}

See Also

Working With Tested Android Applications
LaunchPackage Method
Installing Packages on Devices
Adding Android Applications to the List of Tested Applications
About Tested Applications
Working With Tested Applications in Tests

Highlight search results