2 hours of random key simulation in Python_adb

Android terminal (car, mobile phone), etc,
It is necessary to consider the operation of all kinds of keys (on-board steering wheel key, mobile phone hard key) at will,
Test the response stability of the system to key events, generally for 2 hours.


Preparation stage

Generally, adb shell input keyevent + keyCode is used to simulate key events,
For example, adb shell input keyevent 24 can simulate "volume plus key",
The following is the key value table of several commonly used keys.

KeyCode Interpretation
3 Home key
4 Return key
5 Phone dialing key
24 Volume key
25 Volume reduction
26 Power button
27 Camera start key
87 Multimedia next key
88 Multimedia previous key
91 Volume mute key

Python batch script form

The characteristics of batch processing scripts are: batch sequential execution of statements

# coding=utf-8

import os
import time
import random

timeout = 7200  # 2 hours (7200s)
now_time = time.time()  # Gets the current timestamp and saves it to a variable
keycode_dict = {3: "HOME key", 4: "Return key", 5: "Phone dialing key", 24: "Volume key",
                25: "Volume reduction", 26: "Power button", 27: "Camera start key", 87: "Next key",
                88: "Last key", 91: "Mute"}

# Cycle to get the current time, and subtract the time stamp with the now u time variable
while time.time() - now_time <= timeout:
    # Randomly select a key in the dictionary
    keycode = random.choice(list(keycode_dict.keys()))
    command = "adb shell input keyevent %s" % keycode
    print("Successfully pressed%s" % keycode_dict[keycode])
    # os.system(command)
    time.sleep(0.5)  # At an interval of 0.5s, perform the next key simulation
    print("It's done %d second..., A total of tests are needed %d second" % (time.time() - now_time, timeout))

os.system("pause")

Python procedure oriented function form

See which code blocks can form a specific function,
In this case, you can have a function to input key events, input_keyevent(),
This function can receive parameter input and specify how long the key event is input.

# coding=utf-8

import os
import time
import random


def input_keyevent(timeout):
    now_time = time.time()  # Gets the current timestamp and saves it to a variable
    keycode_dict = {3: "HOME key", 4: "Return key", 5: "Phone dialing key", 24: "Volume key",
                    25: "Volume reduction", 26: "Power button", 27: "Camera start key", 87: "Next key",
                    88: "Last key", 91: "Mute"}

    # Cycle to get the current time, and subtract the time stamp with the now u time variable
    while time.time() - now_time <= timeout:
        # Randomly select a key in the dictionary
        keycode = random.choice(list(keycode_dict.keys()))
        command = "adb shell input keyevent %s" % keycode
        print("Successfully pressed%s" % keycode_dict[keycode])
        # os.system(command)
        time.sleep(0.5)  # At an interval of 0.5s, perform the next key simulation
        print("It's done %d second..., A total of tests are needed %d second" % (time.time() - now_time, timeout))


input_keyevent(7200)
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 "KeyeventInputter",
    Represents the key event input device, and generally hump type (initial capital) is used to standardize the class name.
  2. Form a good habit of class init ialization,
    During the initialization process, you can define the attribute, self.keycode'dict.
  3. Define the method (function), input ﹣ keyevent function.
  4. Class is an abstract thing, which must be instantiated into concrete objects,
    Can be called, so we instantiate and name it as k'u obj, indicating that it is an object.
  5. After the instance becomes a concrete object, the object can call the function input [keyevent].
# coding=utf-8

import os
import time
import random


class KeyeventInputter():
    def __init__(self):
        self.keycode_dict = {3: "HOME key", 4: "Return key", 5: "Phone dialing key", 24: "Volume key",
                             25: "Volume reduction", 26: "Power button", 27: "Camera start key", 87: "Next key",
                             88: "Last key", 91: "Mute"}

    def input_keyevent(self, timeout):
        now_time = time.time()  # Gets the current timestamp and saves it to a variable
        # Cycle to get the current time, and subtract the time stamp with the now u time variable
        while time.time() - now_time <= timeout:
            # Randomly select a key in the dictionary
            keycode = random.choice(list(self.keycode_dict.keys()))
            command = "adb shell input keyevent %s" % keycode
            print("Successfully pressed%s" % self.keycode_dict[keycode])
            # os.system(command)
            time.sleep(0.5)  # At an interval of 0.5s, perform the next key simulation
            print("It's done %d second..., A total of tests are needed %d second" % (time.time() - now_time, timeout))


k_obj = KeyeventInputter()
k_obj.input_keyevent(7200)
os.system()

Operation mode and effect

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, saved as random_keyevent.py and placed on the desktop,
It is recommended to run python random_keyevent.py, or double-click it.
The operation effect is as follows:


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=635502eeeae5455c9ba064411cd5617d
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 Tue, 24 Mar 2020 09:44:26 -0400 by -Zeus-