GetInstalledPackageByName Method

Applies to TestComplete 15.68, last modified on October 03, 2024
The information below concerns legacy mobile tests that work with mobile devices connected to the local computer. For new mobile tests, we recommend using the newer cloud-compatible approach.

Description

The GetInstalledPackageByName method returns information about the package specified by its name.

Declaration

PackageManagerObj.GetInstalledPackageByName(Name)

PackageManagerObj An expression, variable or parameter that specifies a reference to a PackageManager object
Name [in]    Required    String    
Result PackageObject

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Name

Specifies the name of the desired package. The name should be specified in a Java-style notation, as it is described in the AndroidManifest.xml file. For instance: com.example.myapp.

Result Value

The PackageObject object that describes the found package. If no package with the specified name was found among the packages installed on the device, the method posts a warning to the test log and returns an empty object (null in JavaScript, JScript, C#Script and C++Script, None in Python, Nothing in VBScript, nil in DelphiScript).

Example

The following code checks whether a package was already installed on the device. On success it posts package description to the test log, otherwise it deploys the needed package:

JavaScript

function CheckAndDeploy()
 {
   Mobile.SetCurrent("Nexus 7");
   let PackManagerObj = Mobile.Device().PackageManager;
   // Retrieve package info from the device
   let PackObj = PackManagerObj.GetInstalledPackageByName("com.example.myapp");
   // Verify if the package was found
   if (!strictEqual(PackObj, null))
    {
      // Display the package information
      Log.Message("The package name is: " + PackObj.Name);
      Log.Message("The package local path is: " + PackObj.PCPath);
      Log.Message("The package device path is: " + PackObj.DevicePath);
    }
   else
    {
      // Deploy the package
      PackObj = PackManagerObj.GetPackageFromAPK("D:\\Work\\MyPackage.apk");
      PackManagerObj.InstallPackage(PackObj);
    }
}

JScript

function CheckAndDeploy()
 {
   Mobile.SetCurrent("Nexus 7");
   var PackManagerObj = Mobile.Device.PackageManager;
   // Retrieve package info from the device
   var PackObj = PackManagerObj.GetInstalledPackageByName("com.example.myapp");
   // Verify if the package was found
   if (PackObj != null)
    {
      // Display the package information
      Log.Message("The package name is: " + PackObj.Name);
      Log.Message("The package local path is: " + PackObj.PCPath);
      Log.Message("The package device path is: " + PackObj.DevicePath);
    }
   else
    {
      // Deploy the package
      PackObj = PackManagerObj.GetPackageFromAPK("D:\\Work\\MyPackage.apk");
      PackManagerObj.InstallPackage(PackObj);
    }
}

Python

def CheckAndDeploy():
  Mobile.SetCurrent("Nexus 7")
  PackManagerObj = Mobile.Device().PackageManager
  # Retrieve package info from the device
  PackObj = PackManagerObj.GetInstalledPackageByName("com.example.myapp")
  # Verify if the package was found
  if (PackObj != None):
    # Display the package information
    Log.Message("The package name is: " + PackObj.Name)
    Log.Message("The package local path is: " + PackObj.PCPath) 
    Log.Message("The package device path is: " + PackObj.DevicePath)
  else:
    # Deploy the package
    PackObj = PackManagerObj.GetPackageFromAPK("D:\\Work\\MyPackage.apk")
    PackManagerObj.InstallPackage(PackObj)

VBScript

Sub CheckAndDeploy
  Call Mobile.SetCurrent("Nexus 7")
   Set PackManagerObj = Mobile.Device.PackageManager
   ' Retrieve package info from the device
   Set PackObj = PackManagerObj.GetInstalledPackageByName("com.example.myapp")
   ' Verify if the package was found
   If Not PackObj Is Nothing Then
     ' Display the package information
     Call Log.Message("The package name is: " + PackObj.Name)
     Call Log.Message("The package local path is: " + PackObj.PCPath)
     Call Log.Message("The package device path is: " + PackObj.DevicePath)
   Else
     ' Deploy the package
     Set PackObj = PackManagerObj.GetPackageFromAPK("D:\Work\MyPackage.apk")
     PackManagerObj.InstallPackage(PackObj)
   End If
End Sub

DelphiScript

procedure CheckAndDeploy;
var PackManagerObj, PackObj;
begin
   Mobile.SetCurrent('Nexus 7');
   PackManagerObj := Mobile.Device.PackageManager;
   // Retrieve package info from the device
   PackObj := PackManagerObj.GetInstalledPackageByName('com.example.myapp');
   // Verify if the package was found
   if (PackObj <> nil) then
    begin
      // Display the package information
      Log.Message('The package name is: ' + PackObj.Name);
      Log.Message('The package local path is: ' + PackObj.PCPath);
      Log.Message('The package device path is: ' + PackObj.DevicePath);
    end
   else
    begin
      // Deploy the package
      PackObj := PackManagerObj.GetPackageFromAPK('D:\Work\MyPackage.apk');
      PackManagerObj.InstallPackage(PackObj);
    end;
end;

C++Script, C#Script

function CheckAndDeploy()
 {
   Mobile["SetCurrent"]("Nexus 7");
   var PackManagerObj = Mobile["Device"]["PackageManager"];
   // Retrieve package info from the device
   var PackObj = PackManagerObj["GetInstalledPackageByName"]("com.example.myapp");
   // Verify if the package was found
   if (PackObj != null)
    {
      // Display the package information
      Log["Message"]("The package name is: " + PackObj["Name"]);
      Log["Message"]("The package local path is: " + PackObj["PCPath"]);
      Log["Message"]("The package device path is: " + PackObj["DevicePath"]);
    }
   else
    {
      // Deploy the package
      PackObj = PackManagerObj["GetPackageFromAPK"]("D:\\Work\\MyPackage.apk");
      PackManagerObj["InstallPackage"](PackObj);
    }
}

See Also

PackageManager Object
Installing Packages on Devices (Legacy)

Highlight search results