# 2 hours of random map moving in Python_adb

Android car map test involves: drag the map to move it for 2 hours under the 100m scale of Gaud map,
Expected results: the image is moved normally, the map rendering is normal, and the interface is not stuck.

##### Preparation stage
1. Adjust the scale to 100m in the Gaode map App interface
2. adb shell input swipe x1 y1 x2 y2 can simulate sliding from (x1, y1) coordinate point to (x2, y2) coordinate point
3. The coordinates can be set, developer options, open the pointer position (coordinates), and view the specific coordinates (x,y) of the Photo button
4. Move the figure for 2 hours, you can set cyclic polling, and judge whether it has reached 7200s by time stamp
5. We need to realize the random shift graph.

##### Python batch script form
```# 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

# Cycle to get the current time, and subtract the time stamp with the now u time variable
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 graph, move the graph next time with an interval of 0.5s
print("Have been moved %s seconds..., Total %s seconds" % (time.time() - now_time, timeout))

os.system("pause")```

##### Python procedure oriented function form

First, two main functions are divided:
XY? Random(): coordinate random function;
move_map(): Map move function, parameter 7200s should be considered,
Functions are generally named with lowercase underscore.

```# 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:  # Cycle to get the current time, and subtract the time stamp with the now u time variable
x_1, y_1, x_2, y_2 = xy_random(500, 200, 1300, 600)  # Slide from (500, 200) 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 graph, move the graph next time with an interval of 0.5s
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. With the idea of "all things can be classified", we first abstract out several categories,
Class name is generally recommended to use "noun", so we call it "MapMover" (camel's initial capital),
In fact, this XY ﹐ random() can also be made into a class form, but it's unnecessary,
Because this function can be used by other functions later.
2. Form a good habit of class initialization (init),
During initialization, you can pass (do nothing)
3. At present, only one function (action function) of MapMover is needed,
Function is usually named "verb", so we call it "move" map "
4. Class is an abstract thing, which must be instantiated into concrete objects,
Can be called, so we instantiate and name it m obj, indicating that it is an object
5. After the instance becomes a concrete object, the object can call the move map 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:  # Cycle to get the current time, and subtract the time stamp with the now u time variable
x_1, y_1, x_2, y_2 = xy_random(500, 200, 1300, 600)  # Slide from (500, 200) 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 graph, move the graph next time with an interval of 0.5s
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 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,
It is recommended to run Python move? Map? 100.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)