“Selfie tutorial 48” Python_ ADB random map moving for 2 hours

Time:2020-11-10

Android car map test involves: drag the map to move the map for 2 hours at the 100m scale of Gaode map,
Expected results: the moving image is normal, the map rendering is normal, and there will be no stuck interface.

Preparation stage
  1. In the Gaode map app interface, adjust the scale to 100m
  2. ADB shell input swipe X1 Y1 x2 Y2, which can simulate sliding from (x1, Y1) coordinate point to (X2, Y2) coordinate point
  3. The coordinates can be opened by setting developer options to view the specific coordinates (x, y) values of the camera button
  4. After moving the image for 2 hours, the cyclic polling can be set to determine whether 7200s is reached by the timestamp
  5. We need to move the graph randomly from top to bottom.
Python batch script form
# coding=utf-8

import os
import time
import random

Timeout = 7200 ා cycle shift graph for 2 hours (7200s)
now_ time =  time.time () ා gets the current timestamp and saves it to a variable

#Loop to get the current time, and now_ Time variables are time stamped and subtracted
while time.time() - now_time <= timeout:
    #Slide from (500, 200) to (1300600)
    x_1 = random.randint(500, 1300)
    y_1 = random.randint(200, 600)
    x_2 = random.randint(500, 1300)
    y_2 = random.randint(200, 600)
    command = "adb shell input swipe %s %s %s %s" % (x_1, y_1, x_2, y_2)
    print(command)
    os.system(command)
    time.sleep (0.5) after moving the map, the next move was carried out at an interval of 0.5 s
    print("Have been moved %s seconds..., Total %s seconds" % (time.time() - now_time, timeout))

os.system("pause")

Python oriented procedural function form

Firstly, two main function functions are divided
Xy_ Random (): coordinate random function;
move_ Map (): the map moving function, which needs to consider the transfer of parameters 7200s,
Functions are generally named in lowercase and underlined.

# coding=utf-8

import os
import time
import random


def xy_random(x1, y1, x2, y2):
    x_1 = random.randint(x1, x2)
    y_1 = random.randint(y1, y2)
    x_2 = random.randint(x1, x2)
    y_2 = random.randint(y1, y2)
    return x_1, y_1, x_2, y_2


def move_map(timeout):
    now_ time =  time.time () ා gets the current timestamp and saves it to a variable
    while  time.time () - now_ Time < = timeout: loop to get the current time, and now_ Time variables are time stamped and subtracted
        X_ 1, y_ 1, x_ 2, y_ 2 = xy_ From (500, 200, 1300, 600) to (1300600)
        command = "adb shell input swipe %s %s %s %s" % (x_1, y_1, x_2, y_2)
        print(command)
        os.system(command)
        time.sleep (0.5) after moving the map, the next move was carried out at an interval of 0.5 s
        print("Have been moved %s seconds..., Total %s seconds" % (time.time() - now_time, timeout))


move_map(7200)
os.system("pause")

Python object oriented class form
  1. Based on the idea that everything can be classified, several categories are abstracted,
    It is generally recommended to use “noun” for class name, so we named it “mapmover”,
    This XY_ In fact, random() can also be used as a class, but it is unnecessary,
    Because this function can be shared with other functions later.
  2. Develop good class initialization(init)The habit of,
    During initialization, you can pass (do nothing)
  3. For this mapmover class, only one map moving function (action function) is needed,
    It is generally recommended to use “verb” to name a function, so we call it “move”_ map” .
  4. Class is an abstract thing, which must be instantiated into a concrete object,
    So we instantiate and name it m_ Obj, indicating an object
  5. After instantiation, the object can call move_ Map this function
# coding=utf-8

import os
import time
import random


def xy_random(x1, y1, x2, y2):
    x_1 = random.randint(x1, x2)
    y_1 = random.randint(y1, y2)
    x_2 = random.randint(x1, x2)
    y_2 = random.randint(y1, y2)
    return x_1, y_1, x_2, y_2


class MapMover(object):
    def __init__(self):
        pass

    def move_map(self, timeout):
        now_ time =  time.time () ා gets the current timestamp and saves it to a variable
        while  time.time () - now_ Time < = timeout: loop to get the current time, and now_ Time variables are time stamped and subtracted
            X_ 1, y_ 1, x_ 2, y_ 2 = xy_ From (500, 200, 1300, 600) to (1300600)
            command = "adb shell input swipe %s %s %s %s" % (x_1, y_1, x_2, y_2)
            print(command)
            os.system(command)
            time.sleep (0.5) after moving the map, the next move was carried out at an interval of 0.5 s
            print("Have been moved %s seconds..., Total %s seconds" % (time.time() - now_time, timeout))


if __name__ == '__main__':
    m_obj = MapMover()
    m_obj.move_map(7200)
    os.system("pause")

Operation mode and effect

Make sure that 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 move_ map_ 100.py and put it on the desktop,
Suggest Python move_ map_ 100. Py. Of course, you can double-click to run.
The operation effect is as follows:

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=f5f74ce2c0b24a4d9c46c0ba95c483e4
You can also pay attention to the wechat subscription number of “wusanren” and accept article push at any time.

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]