“Selfie tutorial 47” Python_ ADB reboots the device 100 times

Time:2020-11-29

Do Android mobile phone / TV / car system stability test,
The system needs to be rebooted for 100 times to ensure that it will not get stuck during startup,
Or the home interface is abnormal after getting up.
There are also tests involving the first use of the boot activation interface to ensure that each system restart,
As long as the device is not activated, the activation interface must pop up every time.

Preparation stage
  1. The command of ADB reboot or ADB shell reboot can realize the soft restart of mobile phone
  2. ADB wait for device can wait, waiting for the mobile phone to restart the ADB connection
  3. os.system You can call the above ADB command-line tools
  4. 100 times, a for loop statement is required.
Python batch script form

Python only needs to use os.system () function,
It can quickly call all kinds of command line commands.

# coding=utf-8

import os

for i in range(1, 101):
    os.system("adb wait-for-device")
    os.system("adb reboot")
    Print ("system round% s soft restart... Please wait!"% I)

os.system("pause")

os.system It is suggested to add “pause”, which means to suspend the execution of the procedure,
Wait for the user to view the script execution results to avoid closing the script execution CMD window automatically after running.

Python oriented procedural function form

Find a small code block that can be encapsulated into a function block and encapsulate it with a function
It is generally recommended to use “verb” to name a function, so we call it “reboot”,
After encapsulation, the reboot() function can be called.

# coding=utf-8

import os


Def reboot(): Encapsulation
    for i in range(1, 101):
        os.system("adb wait-for-device")
        os.system("adb reboot")
        Print ("system round% s soft restart... Please wait!"% I)


Reboot() ා call
os.system("pause")

The above is the simplest function, that is, the code block is packaged with a function.
Another form is as follows:

# coding=utf-8

import os


Def reboot (cycle): Encapsulation
    for i in range(1, cycle + 1):
        os.system("adb wait-for-device")
        os.system("adb reboot")
        Print ("system round% s soft restart... Please wait!"% I)


Reboot (100) ා call and pass parameters
os.system("pause")

These are more advanced functions in the form of functions with parameter inputs,
It can control the number of restarts. If you want to restart 100 times, you can directly transfer the parameter 100.

Python object oriented class form
  1. Based on the idea that everything can be classified, a class is abstracted,
    It is generally recommended to use “noun” for class name, so we call it “rebooter”,
    Represents the reboiler, and generally uses hump type (capital letter) to standardize the naming of classes.
  2. Develop good class initialization (__ init__ )The habit of,
    During initialization, you can pass.
  3. For this rebouter class, only one restart function (action function) is required,
    It is generally recommended to use “verb” to name a function, so we call it “reboot”.
  4. Class is an abstract thing, which must be instantiated into a concrete object,
    So we instantiate and name it R_ Obj, indicating an object
  5. After instantiation into a specific object, the object can call the reboot function.
# coding=utf-8

import os


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

    def reboot(self, cycle):
        for i in range(1, cycle):
            os.system("adb wait-for-device")
            os.system("adb reboot")
            Print ("system round% s soft restart... Please wait!"% I)


R_ Obj = rebooter() ා instantiate an object
R_ obj.reboot (100) call a function with an object.

os.system("pause")

Code running mode

Make sure the Android device is connected to the computer via USB,
The three implementation forms of the above code can be run directly,
For example, save it as ADB_ reboot_ 100.py and put it on the desktop,
Every time you need a screenshot, double-click to run ADB_ reboot_ 100.py,
You are ready to start executing the script.

Script optimization direction

In the actual automation test, it is not only so simple, but also needs to consider:

  1. After the device is found, consider intercepting the kernel log started by the system with the command demsg
  2. After the system is up, it is also necessary to consider how to intercept the log of APP layer by using ADB logcat.
  3. After the system is up, it is also necessary to consider the capture of home interface image by ADB shell screencap.
  4. Each round needs to name a folder, and the name of the log in it needs to have a timestamp.
  5. Soft restart is to restart by command,
    Hard restart is to restart after power failure by pulling the battery or unplugging and cutting off the power supply.
    How to use Python to achieve hard restart? It will be covered in the following course.

For more and better original articles, please visit the official website: www.zipython.com
Self portrait course (Python course of automatic test, edited by Wu Sanren)
Link to the original text: https://www.zipython.com/#/detail ?id=d42a3e82a8814691839042ba3984b26a
You can also pay attention to the wechat subscription number of “wusanren” and accept article push at any time.

Recommended Today

Webassembly and trust

Rust’s support for webassembly is the most perfect. After all, a large part of Mozilla’s original development of trust was to write servo (browser rendering engine). In addition, the two wasm runtimes, wasmer and wasmtime, which we introduced, are also written with rust. For the use of rust to write wasm module and the use […]