Android Applications Testing - Helpful Tools
Syed Rehan Ahmed
Manager SQA | Test Automation | Test Architect | OTT | Fintech | Ride Hailing | MS Project Management | ISTQB-CTFL? Certified
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?
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
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
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
adb shell pm list packagesLists all installed packagesadb shell pm list packages -3Lists all installed 3rd-party packages
#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
# 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
- 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,
领英推荐
$ adb shell svc wifi disable
$ adb shell settings put global airplane_mode_on 1
#Check battery status
$ adb shell dumpsys battery
#Simulate low battery
$ adb shell dumpsys battery set level 15
#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,
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,
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
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
Lead| Sr. Software Quality Assurance Engineer | Manual & Automated QA
3 个月Very helpful!