Android車載地圖測驗,涉及:高德地圖100m比例尺下,拖動地圖進行移圖操作2個小時,
預期結果:移圖正常,地圖渲染正常,不會出現卡死卡滯界面例外等情況,
準備階段
- 在高德地圖App界面,調整比例尺到100m
- adb shell input swipe x1 y1 x2 y2 , 可以模擬從(x1, y1)坐標點滑動到(x2, y2)坐標點
- 坐標可以通過設定-》開發者選項-》打開指標位置(坐標),可以查看拍照按鈕的具體坐標(x,y)值
- 移圖2小時,可以設定回圈輪詢,通過時間戳判斷是否達到了7200s
- 需要實作上下左右隨機移圖,
Python批處理腳本形式
# coding=utf-8
import os
import time
import random
timeout = 7200 # 回圈移圖2小時(7200s)
now_time = time.time() # 獲取當前時間戳,并保存到一個變數
# 回圈獲取當前時間,與now_time變數做時間戳做減法
while time.time() - now_time <= timeout:
# 從(500, 200)這個坐標點滑動到(1300,600)
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) # 移圖后,間隔0.5s,再進行下一次移圖
print("Have been moved %s seconds..., Total %s seconds" % (time.time() - now_time, timeout))
os.system("pause")
Python面向程序函式形式
先分出2個主要功能函式來:
xy_random() : 坐標隨機函式;
move_map(): 地圖移圖函式,需要考慮傳參7200s,
函式一般用小寫加下劃線的命名規則,
# 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() # 獲取當前時間戳,并保存到一個變數
while time.time() - now_time <= timeout: # 回圈獲取當前時間,與now_time變數做時間戳做減法
x_1, y_1, x_2, y_2 = xy_random(500, 200, 1300, 600) # 從(500, 200)這個坐標點滑動到(1300,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) # 移圖后,間隔0.5s,再進行下一次移圖
print("Have been moved %s seconds..., Total %s seconds" % (time.time() - now_time, timeout))
move_map(7200)
os.system("pause")
Python面向物件類形式
- 以"萬物皆可歸類"的思想, 先抽象化出幾個類來,
類名一般建議用"名詞", 所以我們命名為"MapMover"(駝峰首字母大寫),
這個xy_random()其實也可以做成類的形式,只是沒必要,
因為這個函式可以后續給其他函式公用, - 養成良好的類的初始化(init)的習慣,
初始化程序中, 可以pass(什么都不做) - 這個MapMover 類, 目前只需要一個地圖移圖的函式(動作功能)就足夠了,
函式的命名一般建議用"動詞", 所以我們命名為: "move_map" . - 類是一個抽象的事物, 必須實體化成具體的物件后,
才能進行呼叫, 所以我們實體化并命名成了m_obj, 表明是一個物件. - 實體化成具體物件后, 物件就可以呼叫move_map這個函式了.
# 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() # 獲取當前時間戳,并保存到一個變數
while time.time() - now_time <= timeout: # 回圈獲取當前時間,與now_time變數做時間戳做減法
x_1, y_1, x_2, y_2 = xy_random(500, 200, 1300, 600) # 從(500, 200)這個坐標點滑動到(1300,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) # 移圖后,間隔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")
運行方式及效果
確保Android車機設備通過USB線與電腦連接了,adb設備有效連接,
以上代碼的3種實作形式都可以直接運行,
比如保存為move_map_100.py并放在桌面,
建議python move_map_100.py運行,當然也可以雙擊運行,
運行效果如下:
更多更好的原創文章,請訪問官方網站:www.zipython.com
自拍教程(自動化測驗Python教程,武散人編著)
原文鏈接:https://www.zipython.com/#/detail?id=f5f74ce2c0b24a4d9c46c0ba95c483e4
也可關注“武散人”微信訂閱號,隨時接受文章推送,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/167825.html
標籤:Python
