Quantcast
Channel: Testing – Falafel Software Blog
Viewing all articles
Browse latest Browse all 68

Instrumenting Android Apps II

$
0
0

Instrumenting Android Apps II

The TestedApps Instrument button is a handy way to prepare Android apps for testing, but not if there’s no one to press the button. To automate instrumentation, write script that adds a AndroidTestedApp to the TestedApps list automatically. This allows you to react to new builds or certificate changes without manual intervention. If you have an automated build process or continuous integration, TestComplete script can locate packages and certificates, then build instrumented packages on-the-fly.

I have a simple Android app created in Android Studio that lives in my c:\apk directory.

myapp.apk in file explorer

If I add this app to TestedApps manually, the UI looks something like the screenshot:

The TestedApps screen showing manual settings

The AndroidTestedApp properties correspond to the UI for TestedApps.

  • Set AutorunOnRecording true if you want to launch the app automatically when you start recording.
  • Set Launch true if you want to run the app from tests. If you set this property false, the app cannot be launched from TestComplete keyword tests, scripts, or even by right-clicking the TestedApp and selecting Run Selected from the context menu.
  • Set DeployOnStart true if the app should be installed every time you start a test. TestComplete will delete and reinstall the application’s package each time.
  • If KeepDataAndCache is true, TestComplete will not remove the application data and cache from the device when deploying the application package. These settings correspond to the data and cache visible on the device’s stock Application Manager app in the screenshot below.

    Android App Info screen

Adding Android TestedApps in Script

To add the AndroidTestedApp using script, call the TestedApps AddAndroidApp() method and pass the path to the apk file.

var index = TestedApps.AddAndroidApp("C:\\apk\\myapp.apk");

The AddAndroidApp() method returns an index in the TestedApps list of items. Pass this index to the Items() method and get the AndroidTestedApp object. Set properties on the AndroidTestedApp and call the Instrument() method.

var app = TestedApps.Items(index);
app.AutorunOnRecording = true; 
app.DeployOnStart = true;
app.Launch = true; 
app.KeepDataAndCache = true;  

app.Instrument();

The default settings automatically create a backup of your original, un-instrumented package file.

the mayapp package file and backup in file explorer

Note: To control if a backup is created and the path to the backup file, set the BackupOriginalAPK and BackupFileName properties:

app.BackupFileName = "C:\\apk\\myapp_backup.apk";  
app.BackupOriginalAPK = true;
app.Instrument();

Is the Package Instrumented?

If you run Instrument() multiple times on the same AndroidTestedApp, it will detect that the app is already instrumented and return quickly without altering the package. If you perform additional work to create your package initially, the Instrumented property helps you avoid time consuming work preparing the package if instrumentation has already been done.

if (!app.Instrumented){
  app.Instrument();
}

What if a previous version of TestComplete instrumented the app? Checking the InstrumentedWithSupportedVersion is your last stop before running the app.

if (app.InstrumentedWithSupportedVersion){
  Log.Message(app.APKFileName + 
    " instrumentation is compatible with this version of TestComplete"); 
  app.Run(); 
}

When you run your script, the new “myapp” appears in the TestedApps list, the apk file is modified, and the playback indicator shows that your package is being installed and run on the device, all performed automatically.

Playback indicator showing "Executing InstallPackage for package falafel.myapp"

Wrap Up

You can instrument an app packages entirely in script. The AndroidTestedApp corresponds to the same capabilities in the TestedApps UI. Its properties set deployment, caching, and backup options. The Instrumented and InstrumentedWithSupportedVersion properties verify that the app can and should be run. You may have noticed that the certificate wasn’t set in this script. The debug certificate was used automatically. Tomorrow we’ll modify the script to sign the app with a custom certificate.

The post Instrumenting Android Apps II appeared first on Falafel Software Blog.


Viewing all articles
Browse latest Browse all 68

Latest Images

Trending Articles



Latest Images