[Android] Introduction to automated testing of andorid camera [1]

1 Overview

There are many things that python can do. What I will introduce today is to cooperate with Shell commands to realize the automated testing of andorid.
Today's small goal is to automatically turn on the camera.
Require:

  • 1. The number of tests can be set
  • 2. The test equipment can be set

System requirements:
linux: adb is required, and shell can be used.
The same is true for direct use of the shell.
Python essentially invokes the shell. The advantage of using python is that it can better build and classify management, and can sort out relevant key content, that is, data cleaning.
Ado.

2 ADB

The essential principle of adb calling the camera is to call the Activity.
We can invoke the camera according to the actual needs, and both the replacement project and the camera can use this command to check the corresponding activity in order to invoke the corresponding apk. The same is true for other non-camera apks.
Use the following command to see the activity of the current focus window. If we need to stress test the camera, we need to open the camera and enter the following command at the same time.

adb shell dumpsys window | findstr mCurrentFocus   #windows 命令
adb shell dumpsys window | grep mCurrentFocus      #linux 命令

Then call the camera with the contents of the above dump. The calling command format is as follows:

adb shell am start -W  apkName/启动页的actvity

Commands related to taking pictures and returning to home are also issued through the keycode in android.

adb shell input keyevent 3 				#返回home
adb shell input keyevent 27				#photo

If we use Shell, we can get the relevant device through the following content, that is, the test device. For the sake of inheritance, you can manually enter the relevant test equipment yourself.

adb devices

3 Test preparation

Set the first parameter written to be the test device.
For the awk command, see what I wrote before. Getting Started with AWK Commands

if [ ! -n "$1" ]
then
    echo "没有参数输入走默认设备"
    devices=$(adb  devices|awk '{print $1}'|sed -n '2p')    #获取adb device信息第一个设备
else
    echo "有参数输入,参数为"$1
    devices=$1
fi

echo "adb设备为"$devices

In addition, the number of tests needs to be prepared.
Of course, you can also write an infinite loop, and you can stop it whenever you don’t want to test.
Set the second parameter to be written as the number of tests.

# 判断参数是否为空
# 测试次数
if [ ! -n "$2" ]
then
    echo "无参数输入测试次数为10"
    testCount=10    #测试次数设定为10
else
    echo "有参数输入,参数为"$2
    testCount=$2
fi

echo "测试次数为"$2

The relevant test script content is as follows:

test(){
    
                          
    while ((count<testCount));do
    {
    
    
        let count++
        echo "测试次数 = "$count
        adb -s $devices shell input keyevent  82                                       #设备解锁
        adb -s $devices shell am start -WapkName/启动页的actvity  #打开camera apk 这个根据自己设备的系统camera apk来定
         sleep 2 #延时2s
        adb -s $devices shell input keyevent 3   #回到home
        sleep 2 #延时2s
        }
    done
}

The above content is sorted and saved into a .sh suffix and then chmod +x adds permission to run. The format of the entire run is as follows:

./测试脚本.sh 测试设备  测试次数

4 expansion

So far, the camera can be turned on automatically, but the actual application scenarios are often more complicated. When we do automation, we also need to record a certain state and information in the automation process. Moreover, the above method requires a certain shell foundation.
So I encapsulated some content later, and kept some content exposed to non-R&D personnel.
We can do this.
Make a config.txt file, which configures the apk we need to test, the number of tests, and the device to be tested.
Why I keep emphasizing on test equipment. Often we need to plug in many devices on one machine, and perform various tasks at the same time.
If you do not control the tested equipment, it will often affect other equipment.
The content of its config.txt is as follows. Note that (#) is used as a comment. I read the file and parse it through (#) to judge whether the current line is an interpretation line. The interpretation line skips to read the next line, and then finds the key word, and then filter out the content corresponding to the keyword, and the parsing is successful. (Spaces don't matter, I will take action, I have done the operation of deleting spaces)

#adb shell dumpsys window | findstr mCurrentFocus   #windows 命令
#adb shell dumpsys window | grep mCurrentFocus      #linux 命令
#mtk:
#通过以上的命令获取到你要测试的camera apk。需要先打开对应的camera
apk:   com.mediatek.camera/com.mediatek.camera.CameraLauncher

#测试次数
testCount:     1000

#测试设备
deviceSN:    测试设备

Then write a read.py file to read the contents of the config.txt file and parse it out

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@File    : main.py
@Author  : 没有手牌
@Time    : 2022/12/30 16:32
"""
import os
import argparse
import fileinput
import re
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
import sys
import time

filename = "config.txt"

def readConfig():
    count = 0
    for line in fileinput.input(filename):
        count +=1
        if line.find("#") == 0:
            continue
        elif line.find("apk") == 0:
            apk = line.split('apk:')[1]
            apk = apk.strip()                               #去掉空格
            apk = apk.replace("\r","")                      #去掉换行符
        elif line.find("testCount") == 0:
            testCount = line.split('testCount:')[1]
            testCount = testCount.strip()                   #去掉空格
            testCount = testCount.replace("\r","")          #去掉换行符号
        elif line.find("deviceSN") == 0:
            deviceSN = line.split('deviceSN:')[1]
            deviceSN = deviceSN.strip()                   #去掉空格
            deviceSN = deviceSN.replace("\r","")           #去掉换行符号

    print("调用apk名字为 \t",apk)
    print("测试次数\t",testCount)
    print("测试设备为\t",deviceSN)
    return apk,testCount,deviceSN

def main():
    readConfig()

#主函数入口
if __name__ == '__main__':
    main()

Then we need to use shell-related commands in python
to read this article in detail.
Python has many ways to operate the shell. Packages that need to be imported first

import os

The first way is to use os.system("command") directly, which returns 0, indicating that the command is executed successfully. The obvious disadvantage is that the returned value cannot be saved.

devices = os.system("adb devices")

The second way is to use os.popen("command")

What os.popen() returns is the object of file read, and the output of the execution can be seen through the operation of reading read() on it.

devices = os.popen("adb devices")
print("deviecs = ",devices.read())

The result of the printout is

deviecs =  List of devices attached
L7Z5AABQAILZPBTO        device

With cooperation, modify it to the following content, and you can get the corresponding adb device. Currently, you can only get the top one, and you can improve it according to the logic.

devices = os.popen("adb  devices|awk '{print $1}'|sed -n '2p'")
print("deviecs = ",devices.read())

Note: popen mainly involves operations on files, but operations such as sleep and taking pictures in some shells do not need to return a value or use system to operate.
When I actually took pictures, I found this problem. popen does not take effect, only system takes effect.

Guess you like

Origin blog.csdn.net/qq_38753749/article/details/128684230