赞
踩
很多外包公司都有一套自动化测试平台,可以销售给甲方(银行保险之类公司);而像一些互联网大厂通过自己技术人员进行开发,一般实在appium基础上再进行深度开发。AI自动化思路是结合appium加入图像识别以及ocr,在八大定位元素基础上,增加图像识别以及文本识别(ocr),实现自动化测试。
以下是工作app中登录实战,使用了百度飞浆进行ocr识别,然后计算出坐标位置,进行点击操作,然后再输入用户名
- from appium import webdriver
- import time
- from config import setting
- from paddleocr import PaddleOCR
-
- desired_caps = dict()
- desired_caps['platformName'] = 'Android' # 可以写成android
- desired_caps['platformVersion'] = '12' # 11.1.0等都可以写成11
- desired_caps['deviceName'] = '测试手机型号' # 设备名字可以随便写,但是不可以为空
- desired_caps['appPackage'] = '测试app包'
- desired_caps['appActivity'] = '你测试app的MainActivity'
- desired_caps['noReset'] = True # 打开app时不清除数据
- desired_caps['udid'] = "你的测试设备"
- # desired_caps['udid'] = "80fe4bfb"
- # desired_caps['udid'] = "9WDNW21107036993"
- # desired_caps['automationName'] = "UiAutomator2"
- desired_caps['unicodeKeyboard'] = True
- desired_caps['resetKeyboard'] = True
- #启动appium服务
- # cmd = r'start appium -a 127.0.0.1 -p 4723 -bp 4724'
- # pr = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
- # pr.wait(timeout=3)
- driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
- time.sleep(10) # 等待20秒"
- img_folder = setting.OCR_DIR
- screen_save_path = img_folder + '登录.png'
- # # time1 = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
- # # screen_save_path = img_folder + time1 + '.png'
- driver.get_screenshot_as_file(screen_save_path)
- ocr = PaddleOCR()
- result = ocr.ocr(screen_save_path)
- point = []
- print(result)
- for line in result[0]:
- if line[1][0] == '用户名':
- print(line[0])
- # left, top, right, bottom = line[0]
- # print(top)
- a = 0
- b = 0
- for line1 in line[0]:
- a = a + line1[0]
- b = b + line1[1]
- # print(a)
- # print(b)
- c = a // 4
- e = b // 4
- point.append(c)
- point.append(e)
- x, y = point
- time.sleep(2)
- # action.tap(x=x, y=y).perform()
- driver.tap([(x, y)], 100)
- time.sleep(3)
- driver.switch_to.active_element.send_keys("编程老怪") # 输入文本
- time.sleep(20)
- driver.quit()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。