Android Applications Testing - Helpful Tools
https://www.scnsoft.com/software-testing/android-app-testing-specifics

Android Applications Testing - Helpful Tools

For the Mobile Apps Testers, there are areas where we need to go beyond the functional testing part, as its just one part of the process. There are several areas where testers can take leverage from different tools specially with Android applications, there are quite alot things that android provide with its command line interface, lets discuss few of these command-line utilities and their use cases, along with some other third party tools that make the testers daily testing tasks more efficient and effective, we can take help of these tools in streamlining our manual testing approach as well as in our automation as well, depending upon the use case we have.

Android Debug Bridge (ADB)

Lets start with the command line utility that android provides, by the name of ADB, it is the Command Line tool which helps the engineers to communicate the android application through command line. So, it will be quite helpful and it opens the doors to access the areas that is quite impossible to touch with the GUI, you can manipulate things, get logs etc.

How to connect with ADB?

  1. Install Android SDK: Download the Android SDK and set up the adb tool.
  2. Enable Developer Options on Your Device:Go to Settings > About Phone > Tap "Build number" 7 times to unlock Developer Options.Go to Settings > Developer Options > Enable "USB Debugging."
  3. Connect Your Device: Use a USB cable to connect your device to your computer.
  4. Verify Connection: Run adb devices in your terminal or command prompt to check if your device is recognized.

So now you have things set up and ready to use, we can explore few of the areas which can help in our testing lifecycle, there are alot of things that can be achieved but lets explore few things in testing perspective,

Interesting Use cases

  • Install and unistalling the application from command line, you can automate the process with the help of bash script, so whenever new version available you can do it through your script, the basic command for installing/uninstalling is,

adb install /path/to/your/app.apk
$ adb install app-v1.2.apk

$ adb uninstall com.example.app        

Sometimes we use simulators for the testing, adb help in this areas as well, to list connected devices and install the app on some specific one,

$ adb devices
List of devices attached
emulator-5554 device
emulator-5555 device
0.0.0.0:6520  device

# To install on emulator-5555
$ adb -s emulator-5555 install com.example.apk

# To install on 0.0.0.0:6520
$ adb -s 0.0.0.0:6520 install com.example.apk        

Bash Script,

#!/bin/bash

# Uninstall the existing app
adb uninstall com.example.app

# Install the new APK
adb install /path/to/your/app-v1.2.apk

echo "App installation completed for testing."        

You can also issue a package manager command directly from adb without entering a remote shell. For example:

adb shell pm uninstall com.example.app        

  • Broadcast events, one of the use case is where you want to test the campaigns without actually downloading the app from playstore, it simulates a situation where a user installs the app through a specific marketing campaign link. The app would normally receive this broadcast when installed via the Play Store, with the referrer parameters helping the app attribute the installation to a specific marketing campaign, its basically used for Testing Campaign Attribution, developers and QA engineers can use this utility to ensure that their app correctly processes and tracks installation sources and marketing campaign data, also helps in debugging, If there are issues with how the app processes referrer data, this command allows you to quickly and easily simulate the receipt of referrer data without needing to go through the entire Play Store installation process.

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.example.app/com.example.app.analytics.GoogleAnalyticsReceiver --es "referrer" "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name";        

So, if you are running a marketing campaign, you want to make sure that the UTM parameters are correctly logged when a user installs the app via a link in that campaign. By running this command, you can see if the app properly processes and logs the utm_source, utm_medium, utm_term, utm_content, utm_campaign values

  • Extract installed package names, one of the use case is you can get the list of packages that are part of the device and later can use this information for several things like certain application is already installed in the system, you want to unistall some specific package and need a name of it, etc.

adb shell pm list packagesLists all installed packagesadb shell pm list packages -3Lists all installed 3rd-party packages        

  • Take screenshots or record a video, one of the use case is you can take a certain screenshot and you can record some specific scenario, and later share it with developer for any bug or something, save it as proof of the implemented functionality is fine etc.

#Take a screenshot

# Start an ADB shell session
adb shell

# Capture a screenshot and save it to the device's storage
shell@ $ screencap /sdcard/screen.png

# Exit the ADB shell session
shell@ $ exit

# Pull the screenshot file from the device to the local machine
$ adb pull /sdcard/screen.png

#Record a video

$ adb shell
shell@ $ screenrecord --verbose /sdcard/demo.mp4
(press Control + C to stop)
shell@ $ exit
$ adb pull /sdcard/demo.mp4        


  • Extracting Logs, one use case is in diagnosing issues or if there are any performance related things you can get it extracted in some particular location then later do your analysis on it,

# Start printing log messages
adb logcat

# Display current log buffer sizes
adb logcat -g

# Set log buffer size to 2MB
adb logcat -G 2M

# Clear all log buffers
adb logcat -c

# Enable verbose logging for all messages
adb logcat *:V

# Dump logs to a file named logs.txt
adb logcat -f logs.txt
----------------------------------------------------------------------------
#Example
$ adb logcat -G 16M
$ adb logcat *:V > output.log        

  • Monkey Testing & Stress Testing, one of the most effective use case is application performance, adb provides a tools called monkey which as per the name do some random testing it creates pseudo-random user events such as clicks, touches, or gestures, but it can be use with focused areas to achieve the application under some load by initialising background processes and see the behavior of the application under severe load, lets consider two cases with this utility,

- first do the monkey testing, where we want to see if theres any crash or something happened if we provide a certain level of stress,

$ adb shell monkey -p your.package.name -v 500        

here -v is increases the verbosity level of the output logs, this flag makes the command output more detailed information about each event it generates, which is helpful for debugging, and 500 is for specifying the number of pseudo-random events to be generated, so the Monkey tool will generate 500 random events for the specified application, we can change this number and deduce different outputs based on it.

- second one is to do the stress testing, to evaluate how the app is performing under or with heavy system load, by running background processes or open the multiple apps to increase the system load and then test the app performance,

 adb shell monkey -p com.example.app -c android.intent.category.LAUNCHER 100        

here -c android.intent.category.LAUNCHER specifies the category of intents that the Monkey will consider. In this case, android.intent.category.LAUNCHER targets the app's main launcher activity, this ensures that the Monkey starts the app as if it were being launched from the home screen and 100 is the number of events the Monkey should generate, 100 event will be generated, the event in this context is the launch of the app.

We can think of many scenarios and do use this Monkey utility based on our need, also with ADB overall we can do much more as well, like,

  • Network Behavior Testing, disable/enable Wi-Fi, or enable/disable Airplane mode,

$ adb shell svc wifi disable
$ adb shell settings put global airplane_mode_on 1        

  • Battery Performance Testing,

#Check battery status
$ adb shell dumpsys battery

#Simulate low battery
$ adb shell dumpsys battery set level 15        

  • Crash Recovery Testing, simulate app crashes or force-stop the app, then test how it recovers when relaunched,

#Force-stop an app
$ adb shell am force-stop com.example.app

#Start an app
$ adb shell am start -n com.example.app/.MainActivity        

Memory Leakage - Leak Canary

One of the most problematic and tricky issue with mobile applications is Memory Leakage issue, which is basically android app hold on some object and maybe that object is destroyed or no longer needed, which lead to slowing the application down, maybe crashing the application or throwing the OutOfMemoryError exception. For the testers its necessary to get account of these memory leakage issues and one of the way to get these issues is with the help of Leak Canary tool, its easy to integrate, automatically detects the memory leaks and provides the alerts during the development phase.

Integration

dependencies {
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.x'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:2.x'
}        

With your android project you can add the Leak Canary Dependency in your debug builds, and thats it, it initialize on its own usually and start monitoring your application, eventually notify you as well in case of any detection.

How LeakCanary works?

Once LeakCanary is installed, it automatically detects and report memory leaks, in 4 steps:

First Step: Detecting retained objects, so LeakCanary when you build your project after integrating its dependency into your Android app and automatically monitors the memory usage, when an object is no longer needed but is still being referenced which cause the memory leakage it detects it and you will see in your application it stops it for a while and gather the data,

Second Step: Dumping the heap, When LeakCanary suspects a memory leak, it forces a heap dump. A heap dump is a snapshot of the app's memory at a particular point in time,

Third Step: Analyzing the heap, LeakCanary analyzes the heap dump to identify references that are preventing the garbage collector from freeing up memory, It pinpoints the exact object and class that are causing the memory leak, which we can later fix or as a tester you can report to the relevant developer,

Usually you get a notification if theres and leak but leak canary installs an app in your device as well where you can refer the leaks if theres any and again report it to relevant dev accordingly,

In the Leaks app you will see the Leaks and its detail along with the class detail and something,

Fourth and Final Step: Categorizing leaks, The tool generates a detailed report showing the chain of references leading to the leaked object, making it easier to trace back the cause of the leak.

Developers can customize LeakCanary's behavior, such as when to trigger leak detection or how to handle specific memory issues, It also provides options to ignore certain types of leaks that might be false positives or irrelevant to the current testing context.

Charles Proxy

We can use it to debug the network traffic between the android device and the server, it helps quite a lot in debugging issues and also help by modifying the responses and get different scenrios simulated, one of the use case is when you try to see the error case handling, the installation is quite simple,

  • You need to download the Charles Proxy and install it on your machine
  • Configure your android device to bypass the traffic to some dedicated IP and port, which is your machine's actually lets say 127.0.0.1:8888
  • Install Charles SSL certificate on your android device, you can get it from your Charles, once its installed its ready to use, you can now see your network calls, modify responses etc.

Roboelectric

Roboelectric is one of the good contender for the integration layer of the android applications, as it runs in the JVM it provides faster results, I have discussed in detail for this in one of my other article do refer below to dig down further on this topic,

https://www.dhirubhai.net/pulse/android-tdd-roboelectric-syed-rehan-ahmed/?trackingId=0bXYvtQ1THO3mAwe9WI7Qg%3D%3D

Key Takeaways

These tools and techniques offer testers the ability to go beyond traditional testing, enabling them to ensure a higher quality product that performs well under various conditions, with powerful command line with ADB, memory management with leak canary, debugging and mocking with Charles, all help in automating and increase the efficiency of our testing


FAIZAN BASHIR

Manager Software Quality Assurance & Test Automation | Test Automation Engineer, Software Quality Assurance practitioner | Cards & Payments | FinTech (CMS, WMS, PGW, DB) | Agile Scrum Master

3 个月

Love this

Babar Riaz

Lead| Sr. Software Quality Assurance Engineer | Manual & Automated QA

3 个月

Very helpful!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了