问题: 操作时弹出普通窗口以及输入密码时的随机软键盘,由于焦点没有切换过来,导致无法直接通过id,xpath定位
解决方式:
- 点击坐标.(处理普通弹出框)
假设用例设计时模拟器的width是x1,heigh是y1.
用例执行机的width是x2,height是y2.
在用例设计时定位的点击坐标是(a,b).
则在用例执行机点击的坐标应该是(a*x2/x1 , b*y2/y1)
使用self.driver.tap([(a*x2/x1, b*y2/y1)], 20)
-
使用图像识别(主要是解决安全键盘每次键位都不同)
思路:- 保存要识别的图片A
- App运行时,到了目标页面,截屏
self.driver.get_screenshot_as_file('/Users/xxx/test/jianpan.jpg')
- 对比,寻找图片A的坐标
- 点击
操作:
- 安装openCV:
brew install opencv
- 下载cv2.pyd
- 将cv2.pyd放入python路径,例如
/usr/local/lib/python2.7/site-packages
- 开始撸代码
import cv2
import numpy as np
import time
#定义找图方法,参数分别是:self, 原图,要找的图片部分,匹配度
def get_pay_keyboard_number_location(self,impath, target,fit_num): #fit_num是匹配度,如 0.95,0.85
print("start find pic")
positions = {}
start = time.time()
img_rgb = cv2.imread(impath)
teNum = "done"
template = cv2.imread(target)
h, w = template.shape[:-1]
res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
threshold = fit_num # 匹配度参数,1为完全匹配
loc = np.where(res >= threshold)
if len(loc) > 0:
positions[teNum] = zip(*loc[::-1])[0]
else:
print("Can not found number: ")
end = time.time()
print(end - start)
return positions[teNum]
然后调这个方法:
#截屏
self.driver.get_screenshot_as_file('/Users/xxx/PycharmProjects/test/jianpan.jpg')
#原图
impath = '/Users/xxx/PycharmProjects/test/jianpan.jpg'
#要找的部分图
targetPath = '/Users/xxx/PycharmProjects/test/Done.jpg'
ls = self.get_pay_keyboard_number_location(impath, targetPath,0.85)
dd = ls[0]
kk = ls[1]
m = (dd/2, kk / 2)
#点击
self.driver.tap([m],20)
网友评论