前面的代码没有将验证码解析方法引入的测试框架中,我们将它引入集成。
目前的代码还是有BUG的,一时半会没调通。继续进行下面的,通过后面的数据驱动测试来解决BUG。
GitHub代码commits id:e302ad5
#!/bin/usr/env python3
# -*- coding: utf-8 -*-
# --------------------------------------
# ProjectName: MySpace
# Author: crisimple
# CreateTime: 2019/6/17 22:11
# FileName: get_code.py
# Description: 获取校验码
# Question:
# --------------------------------------
from PIL import Image
from code_package.sdk.ShowapiRequest import ShowapiRequest
import time
from code_package.base.find_element import FindElement
class GetCode:
def __init__(self, driver):
self.driver = driver
# 获取图片
def get_code_image(self, file_name):
self.driver.save_screenshot(file_name)
code_element = self.driver.find_element_by_id("code_image")
left = code_element.location['x']
top = code_element.location['y']
right = code_element.size['width'] + left
height = code_element.size['height'] + top
im = Image.open(file_name)
img = im.crop((left, top, right, height))
img.save(file_name)
time.sleep(10)
# 解析图片验证码
def code_online(self, file_name):
self.get_code_image(file_name)
# 解析验证码图片中的文字(用第三方的图片验证码识别接口 ShowApiRequest)
r = ShowapiRequest("http://route.showapi.com/184-4","48120","12c017278c0845c2bcda177212d2d2ac" )
r.addBodyPara("img_base64", "")
r.addBodyPara("typeId", "35")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
r.addFilePara("image", file_name) # 文件上传时设置
res = r.post()
text = res.json()["showapi_res_body"]["Result"]
return text