Self portrait tutorial 46: 100 automatic photos taken by Python ADB

Android mobile testing,
Use cases involving stability testing of Camera applications,
Automatic test of taking photos involving 100 photos is required.


Preparation stage
  1. First, clean up the old photos, which are usually stored in / scar / DCIM directory
  2. adb shell am start + camera Activity can open camera App
  3. The coordinate of adb shell input tap can simulate clicking the Photo button
  4. Coordinates can be opened by setting developer options,
    You can view the specific coordinate (x,y) value of the Photo button


How to view the package name of an application

The package name of the application, as the unique identification of the application, represents an apk.
The package name is used for application management (installation, uninstallation). How about the package name?
Take Meizu Note5 mobile phone as an example:

Related commands Interpretation
pm list package |grep camera List all the installed app package names in this phone,
After filtering, only the package names related to camera will be displayed
dumpsys package com.meizu.media.camera |grep Activity Check all kinds of information in the com.meizu.media.camera package,
Only Activity related information will be displayed after filtering




Python batch script mode
# coding=utf-8

import os
import time

# Clear old photos
os.system("adb shell rm -rf /sdcard/DCIM/*")

# Turn on the camera.
os.system("adb shell am start  com.meizu.media.camera/.CameraActivity")

# Cycle click the Photo button 100 times
for i in range(1, 101):
    os.system("adb shell input tap 551 1773")  # Click the coordinates of Camera Photo button
    print("The first%d Photos have been taken"%i)
    time.sleep(1)  # After taking one picture, take another picture every 1s

# Export the 100 photos just taken, and then manually Check the photo quality by the tester
os.system("adb pull /sdcard/DCIM .")
print("Test Over, Photos saved to %s%sDCIM" % (os.getcwd(), os.sep))

os.system("pause")

os.getcwd() is the current path to get the current python script,
os.sep is a path separator. It is recommended to use it frequently,
It can be well adapted to different operating system path separators,
In case of Windows system, it represents a \ character; in case of Linux system, it represents a / character.

Python procedure oriented function form

Only a function camera? Capture is needed to transfer how many rounds of photos can be taken.

# coding=utf-8

import os
import time

def camera_capture(cycle):
    # Clear old photos
    os.system("adb shell rm -rf /sdcard/DCIM/*")

    # Turn on the camera.
    os.system("adb shell am start  com.meizu.media.camera/.CameraActivity")

    # Cycle click the Photo button 100 times
    for i in range(1, cycle+1):
        os.system("adb shell input tap 551 1773")  # Click the coordinates of Camera Photo button
        print("The first%d Photos have been taken"%i)
        time.sleep(1)  # After taking one picture, the interval is 1s, and then take the next picture

    # Export the 100 photos just taken, and then manually Check the photo quality by the tester
    os.system("adb pull /sdcard/DCIM .")
    print("Test Over, Photos saved to %s%sDCIM" % (os.getcwd(), os.sep))

camera_capture(100)
os.system("pause")


Python object oriented class form
  1. With the idea of "all things can be classified", we first abstract a class,
    Class name is generally recommended to use "noun", so we call it "CameraCapturer",
    Represents screen catcher, and generally hump type (initial capital) to regulate the class name
  2. Form a good habit of class init ialization,
    During initialization, you can pass (do nothing)
  3. This CameraCapturer class only needs a photo function (action function) at present,
    The function is usually named "verb", so we call it "camera" capture
  4. Class is an abstract thing, which must be instantiated into concrete objects,
    Can be called, so we instantiate and name it as C obj, indicating that it is an object
  5. After instantiation, the object can call the camera capture function
# coding=utf-8

import os
import time


class CameraCapturer():
    def __init__(self):
        pass

    def camera_capture(self, cycle):
        # Clear old photos
        os.system("adb shell rm -rf /sdcard/DCIM/*")

        # Turn on the camera.
        os.system("adb shell am start  com.meizu.media.camera/.CameraActivity")

        # Cycle click the Photo button 100 times
        for i in range(1, cycle + 1):
            os.system("adb shell input tap 551 1773")  # Click the coordinates of Camera Photo button
            print("The first%d Photos have been taken" % i)
            time.sleep(1)  # After taking one picture, take another picture every 1s

        # Export the 100 photos just taken, and then manually Check the photo quality by the tester
        os.system("adb pull /sdcard/DCIM .")
        print("Test Over, Photos saved to %s%sDCIM" % (os.getcwd(), os.sep))


if __name__ == '__main__':
    c_obj = CameraCapturer()
    c_obj.camera_capture(100)
    os.system("pause")


Code operation mode

Make sure the Android device is connected to the computer through the USB cable, and the adb device is effectively connected,
The three implementation forms of the above code can be run directly, for example, save it as take ﹣ photo.py and put it on the desktop,
You can consider running Python take ﹐ photo.py or double-click it directly.


Script optimization direction
  1. You can consider automatic picture comparison. If you find that the picture is in the process of continuously shooting the agreed object,
    In the same picture, the pixel size of different pictures is too different (or the color difference is too large),
    You need to prompt exceptions for testing, and multimedia test cases will show how to implement them.
  2. In the process of photographing, consider exporting the logcat log, because there may be bug s in the process of photographing,
    Only log can better track bugs.

Package name is different from process name, which is used for process management (creation, destruction),
The package name of the application as the unique identity of the application,
Represents an apk. Package name is used for application management (install, uninstall).
Subsequent performance monitoring and so on are process level monitoring,
In the future, for example, monkey, the package name is used to specify the monkey App.

For more and better original articles, please visit the official website: www.zipython.com
Selfie course (Python course of automatic test, compiled by Wu Sanren)
Original link: https://www.zipython.com/#/detail?id=609bf757abd548e5aca2a07f2c3a55f3
You can also follow the wechat subscription number of "wusanren" and accept the article push at any time.

Tags: Python shell Android Mobile

Posted on Thu, 19 Mar 2020 09:43:48 -0400 by imnsi