Writing in the front
Android-catcher is a Python library packaged by UIAutomator2. The functions of android-catcher include automatic UI tests for Android devices and collection of mobile phone performance data. It is suitable for capturing CPU, memory, frame rate and other information in various test scenarios such as list sliding and video recording, facilitating subsequent analysis. Making address: https://github.com/CharmingW/android-catcher
The installation
Install Python
The scripts for the automated tests are written in Python 3. To run the scripts, you need to install Python 3
Install the Android-Catcher dependency
Open the script directory and run the following command to install dependencies
pip install -r requirements.txt
Copy the code
Usage
How to use UIAutomator2
After installing UIAutomator2, you only need to run the following command to initialize the device and install uiAutomator2 on the device
python -m uiautomator2 init
Copy the code
If the following information is displayed, the installation is successful
Script File Description
The main files in the script library root directory are
- Info. Py: Mobile phone performance information collection script, which defines the parent class Info, has implemented the subclass CPUInfo(CPU information), MemInfo(memory information), FPSInfo(frame rate information), NetInfo(network traffic information), users can derive from Info to achieve their own collection requirements
- task.py: test scenario script, which defines the superclass Task, because there is no fixed test scenario, so the user needs to subclass and rewrite Task
Task#execute
From the definition of test scenarios, custom way may refer to: https://github.com/openatx/uiautomator2 - Info_task. py: a script that combines test scenarios with information collection and is not required by users
- Utils. Py: utility method scripts
- _main_. Py: entry script for task running. Run this script when you want to collect information about a specified period without specific test scenarios
Parameters that
- -s: Specifies the device ID. This parameter is mandatory
adb devices
To obtain - -a: Indicates the applicationId of the process to be tested. This parameter is mandatory
- -f: The sampling interval is optional. The unit is second. It is recommended that the sampling interval be longer than 0.1 seconds
- -d: This parameter is optional. The sampling duration is 10s by default
- -i: Specifies the information to be collected. You can set multiple parameters. Currently, four parameters are optional
CPU, MEM, FPS, NET
, separated by “,”,”, such as-i cpu,mem,fps,net
- -o: output directory of the collected information. For example, “.” indicates the directory where the script resides. The default value is “.”.
Generating file description
The collected information is stored in the four subdirectories cpu_STATS, mem_STATS, FPS_STATS, and net_STATS of the specified output directory based on the information type. The file name is device ID_ApplicationID_Version Number _ Test scenario name _ timestamp. For example, cpu_D3C2EDaa_video.like_recordVideo_1.9.9_1524122928.csv.csv shows the actual effect
The output file is CSV file. The results of direct opening and Excel opening are shown in the following figure
task.period = "idle"
Copy the code
Generate a graph similar to the following
None User-defined test scenario usage
It is applicable to scenarios where the script is collected for a period of time after running without specific test. The duration can be specified by configuration parameters. Users can operate mobile phones at will during the process. Run the _main_.py script file directly on the command line and specify parameters. For example, I want to collect CPU and memory information of the application whose applicationId is video.like within 10s, the sampling interval is 200ms, and the output directory is the current directory. You can then execute the following command in the directory where the script resides
python _main_.py -sDevice number-a video.like -f 0.2 -d 10 -i mem,cpu -o .
Copy the code
After the script runs, you can see the file generated in the root directory as shown below
Note: The -d parameter is required to specify the duration of collection. Otherwise, the script runs for 10s by default, and no -t parameter is required. The default test scenario is called Random
Customize how test scenarios are used
The _main_. Py script cannot be directly invoked for custom test scenarios. You need to create a new script, inherit task.py# task and rewrite the Task#execute method, and implement the logic of custom test scenarios in Task#execute, as shown in the following figure:
Here we create a script called start_app.py and run the command:
python start_app.py -sDevice number-aThe process of-f0.1-I CPU, MEM-O.Copy the code
Then the corresponding APP can be started, and the CPU information and memory information can be collected at a sampling interval of 100ms, and output to the current directory. Note that the -d parameter is missing, because the collection duration is based on the duration of the test task. You must set parameters according to the instructions, otherwise the data cannot be collected. If you want to collect customized information, The info #get_start_info and info #get_end_info methods can be rewritten by inheriting info.py# info. You can refer to the four implemented information collection methods and finally add them through the Task#add_info method.
After you customize the test scenario, call the _main_#main method and pass in the test scenario instance. The name of the test scenario will be part of the output file name. It is best to use a name that accurately represents the test scenario. For example, if an APP records a video test scenario named RecordVideo, the information collected can be made into a chart by Excel. The following is the CPU ratio and memory changes of the test scenario for the complete video recording
Write in the last
These are some of the uses of the library. Due to my working experience in Python, THERE may be a lot of inconsiderate or imperfect places when writing this library. If you have the ability, you can directly modify the library to achieve more customized functions. In addition, I hope you can use it, find more problems, welcome issue, welcome star, Welcome to put forward any new requirements and ideas for use. We will continue to improve them in the future. Thank you! Github address: github.com/CharmingW/a…