首先咱們安裝Facebook的WebDriverAgent,這是一款新的iOS移動測試框架
在GitHub上找到https://github.com/facebook/W...
copy到本地,按照readme初始化
安裝過程當中,須要使用carthage來管理包依賴,使用npm打包響應的js文件node
使用homebrew安裝carthage, node,npm,python
brew install carthage brew install node brew install npm
而後咱們運行git
./Scripts/bootstrap.sh
來配置好環境github
用Xcode打開WebDriverAgent
Xcode 上設置證書,對於免費的用戶,要另外修改bundleID使其惟一
使用Product - > test 來對iPhone進行調試
在控制檯最後獲取到了iPhone的IP地址:npm
調試成功後,爲了鏈接穩定,我選擇了usb鏈接而不是直接經過Wi-Fi
所以須要端口轉發,仍是使用homebrew安裝json
# Install $ brew install usbmuxd # Start proxy $ iproxy 8100 8100
如今WebDriverAgent搭好了,
下面就是安裝python須要調用的包:
https://github.com/openatx/fa...
好的,那麼接下來就是在iPhone中打開微信跳一跳遊戲,
而後運行寫好的python腳本便可
有手動輔助的,也有自動化測試的
這裏是自動運行的腳本bootstrap
# coding: utf-8 import os import shutil import time import math import wda from PIL import Image, ImageDraw import random import json # === 思路 === # 核心:每次落穩以後截圖,根據截圖算出棋子的座標和下一個塊頂面的中點座標, # 根據兩個點的距離乘以一個時間係數得到長按的時間 # 識別棋子:靠棋子的顏色來識別位置,經過截圖發現最下面一行大概是一條直線,就從上往下一行一行遍歷, # 比較顏色(顏色用了一個區間來比較)找到最下面的那一行的全部點,而後求箇中點, # 求好以後再讓 Y 軸座標減少棋子底盤的一半高度從而獲得中心點的座標 # 識別棋盤:靠底色和方塊的色差來作,從分數之下的位置開始,一行一行掃描,因爲圓形的塊最頂上是一條線, # 方形的上面大概是一個點,因此就用相似識別棋子的作法多識別了幾個點求中點, # 這時候獲得了塊中點的 X 軸座標,這時候假設如今棋子在當前塊的中心, # 根據一個經過截圖獲取的固定的角度來推出中點的 Y 座標 # 最後:根據兩點的座標算距離乘以係數來獲取長按時間(彷佛能夠直接用 X 軸距離) # TODO: 解決定位偏移的問題 # TODO: 看看兩個塊中心到中軸距離是否相同,若是是的話靠這個來判斷一下當前超前仍是落後,便於矯正 # TODO: 一些固定值根據截圖的具體大小計算 # TODO: 直接用 X 軸距離簡化邏輯 with open('config.json', 'r') as f: config = json.load(f) # Magic Number,不設置可能沒法正常執行,請根據具體截圖從上到下按需設置 under_game_score_y = config['under_game_score_y'] # 截圖中恰好低於分數顯示區域的 Y 座標,300 是 1920x1080 的值,2K 屏、全面屏請根據實際狀況修改 press_coefficient = config['press_coefficient'] # 長按的時間係數,請本身根據實際狀況調節 piece_base_height_1_2 = config['piece_base_height_1_2'] # 二分之一的棋子底座高度,可能要調節 piece_body_width = config['piece_body_width'] # 棋子的寬度,比截圖中量到的稍微大一點比較安全,可能要調節 time_coefficient = config['press_coefficient'] # 模擬按壓的起始點座標,須要自動重複遊戲請設置成「再來一局」的座標 if config.get('swipe'): swipe = config['swipe'] else: swipe = { "x1": 320, "y1": 410, "x2": 320, "y2": 410 } c = wda.Client() s = c.session() screenshot_backup_dir = 'screenshot_backups/' if not os.path.isdir(screenshot_backup_dir): os.mkdir(screenshot_backup_dir) def pull_screenshot(): c.screenshot('1.png') def jump(distance): press_time = distance * time_coefficient / 1000 print('press time: {}'.format(press_time)) s.tap_hold(200, 200, press_time) def backup_screenshot(ts): # 爲了方便失敗的時候 debug if not os.path.isdir(screenshot_backup_dir): os.mkdir(screenshot_backup_dir) shutil.copy('1.png', '{}{}.png'.format(screenshot_backup_dir, ts)) def save_debug_creenshot(ts, im, piece_x, piece_y, board_x, board_y): draw = ImageDraw.Draw(im) # 對debug圖片加上詳細的註釋 draw.line((piece_x, piece_y) + (board_x, board_y), fill=2, width=3) draw.line((piece_x, 0, piece_x, im.size[1]), fill=(255, 0, 0)) draw.line((0, piece_y, im.size[0], piece_y), fill=(255, 0, 0)) draw.line((board_x, 0, board_x, im.size[1]), fill=(0, 0, 255)) draw.line((0, board_y, im.size[0], board_y), fill=(0, 0, 255)) draw.ellipse((piece_x - 10, piece_y - 10, piece_x + 10, piece_y + 10), fill=(255, 0, 0)) draw.ellipse((board_x - 10, board_y - 10, board_x + 10, board_y + 10), fill=(0, 0, 255)) del draw im.save('{}{}_d.png'.format(screenshot_backup_dir, ts)) def set_button_position(im): # 將swipe設置爲 `再來一局` 按鈕的位置 global swipe_x1, swipe_y1, swipe_x2, swipe_y2 w, h = im.size left = w / 2 top = 1003 * (h / 1280.0) + 10 swipe_x1, swipe_y1, swipe_x2, swipe_y2 = left, top, left, top def find_piece_and_board(im): w, h = im.size print("size: {}, {}".format(w, h)) piece_x_sum = 0 piece_x_c = 0 piece_y_max = 0 board_x = 0 board_y = 0 scan_x_border = int(w / 8) # 掃描棋子時的左右邊界 scan_start_y = 0 # 掃描的起始y座標 im_pixel = im.load() # 以50px步長,嘗試探測scan_start_y for i in range(under_game_score_y, h, 50): last_pixel = im_pixel[0, i] for j in range(1, w): pixel = im_pixel[j, i] # 不是純色的線,則記錄scan_start_y的值,準備跳出循環 if pixel[0] != last_pixel[0] or pixel[1] != last_pixel[1] or pixel[2] != last_pixel[2]: scan_start_y = i - 50 break if scan_start_y: break print("scan_start_y: ", scan_start_y) # 從scan_start_y開始往下掃描,棋子應位於屏幕上半部分,這裏暫定不超過2/3 for i in range(scan_start_y, int(h * 2 / 3)): for j in range(scan_x_border, w - scan_x_border): # 橫座標方面也減小了一部分掃描開銷 pixel = im_pixel[j, i] # 根據棋子的最低行的顏色判斷,找最後一行那些點的平均值,這個顏色這樣應該 OK,暫時不提出來 if (50 < pixel[0] < 60) and (53 < pixel[1] < 63) and (95 < pixel[2] < 110): piece_x_sum += j piece_x_c += 1 piece_y_max = max(i, piece_y_max) if not all((piece_x_sum, piece_x_c)): return 0, 0, 0, 0 piece_x = piece_x_sum / piece_x_c piece_y = piece_y_max - piece_base_height_1_2 # 上移棋子底盤高度的一半 for i in range (int (h / 3), int (h * 2 / 3)): last_pixel = im_pixel[0, i] if board_x or board_y: break board_x_sum = 0 board_x_c = 0 for j in range(w): pixel = im_pixel[j, i] # 修掉腦殼比下一個小格子還高的狀況的 bug if abs(j - piece_x) < piece_body_width: continue # 修掉圓頂的時候一條線致使的小 bug,這個顏色判斷應該 OK,暫時不提出來 if abs(pixel[0] - last_pixel[0]) + abs(pixel[1] - last_pixel[1]) + abs(pixel[2] - last_pixel[2]) > 10: board_x_sum += j board_x_c += 1 if board_x_sum: board_x = board_x_sum / board_x_c # 按實際的角度來算,找到接近下一個 board 中心的座標 這裏的角度應該是30°,值應該是tan 30°, math.sqrt(3) / 3 board_y = piece_y - abs(board_x - piece_x) * math.sqrt(3) / 3 if not all((board_x, board_y)): return 0, 0, 0, 0 return piece_x, piece_y, board_x, board_y def main(): while True: pull_screenshot() im = Image.open("./1.png") # 獲取棋子和 board 的位置 piece_x, piece_y, board_x, board_y = find_piece_and_board(im) ts = int(time.time()) print(ts, piece_x, piece_y, board_x, board_y) if piece_x == 0: return set_button_position(im) distance = math.sqrt((board_x - piece_x) ** 2 + (board_y - piece_y) ** 2) jump(distance) save_debug_creenshot(ts, im, piece_x, piece_y, board_x, board_y) backup_screenshot(ts) time.sleep(random.uniform(1, 1.1)) # 爲了保證截圖的時候應落穩了,多延遲一下子 if __name__ == '__main__': main()