app自动化测试---自动化测试框架搭建之使用腾讯文字识别

1.说明

 在app测试中,app中一些元素我们无法定位获取到,此时就需要截图,通过文字识别的方式,获取我们所需要的内容

 app自动化测试---用户登录自动化    app自动化测试---自动化测试框架搭建  中,我们已经使用自动化完成了登陆功能,现在将对登陆失败的情况做进一步处理

 

 

2.腾讯ocr文字识别

腾讯云图片识别:https://cloud.tencent.com/act/event/ocrdemo

Python- SDK 使用教程:https://cloud.tencent.com/document/sdk/Python

(1)安装第三方依赖包

# 安装依赖包
pip install tencentcloud-sdk-python

(2)点击查看 API文档

        找到自己要调用的接口,填入自己使用中需要的参数,生成对应的代码

 (3)封装代码

          utils/ocr_handler.py

"""
  图片文字识别类
"""
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
from config import image_base64,SecretId,SecretKey

class OCR_Handler:

    def image_ocr_text(self,imageBase64):
        try:
            cred = credential.Credential(SecretId, SecretKey)    # SecretId, SecretKey 从配置文件中获取对应的值
            httpProfile = HttpProfile()
            httpProfile.endpoint = "ocr.tencentcloudapi.com"

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = ocr_client.OcrClient(cred, "ap-beijing", clientProfile)

            req = models.GeneralBasicOCRRequest()
            # 参数均为非必填参数
            params = {
                "ImageBase64": imageBase64             # 图片/PDF的 Base64 值
                # "ImageUrl": "",                      # 图片/PDF的 Url 地址
            }
            req.from_json_string(json.dumps(params))

            resp = client.GeneralBasicOCR(req)
            # print('识别出来的文字内容:',resp.to_json_string())
            return  resp.to_json_string()

        except TencentCloudSDKException as err:
            print('ocr图片识别:',err)


# 单个方法调试代码
# ocr = OCR_Handler()
# text = ocr.image_ocr_text(image_base64)
# jsonobj = json.loads(text)   # type : dict
# text1 = jsonobj['TextDetections'][2]['DetectedText']
# print(text1)

   config.py

"""
  项目配置文件
"""
# ocr 腾讯云秘钥
SecretId = "AK......IuGL"
SecretKey = "pNf......VnnW"

# 调试ocr文字识别的时候使用
image_base64 = "AANSU......ErkJggg=="

 

 

 

posted @ 2021-06-30 14:42  Z_sun  阅读(242)  评论(0编辑  收藏  举报