皮皮虾

Appium获取android的toast问题
被这个问题困扰了很久,网上给出的方法也基本GG不能用,况且现在我已经开始使用网易的airtest的poco测试框架...
扫描右侧二维码阅读全文
09
2018/07

Appium获取android的toast问题

被这个问题困扰了很久,网上给出的方法也基本GG不能用,况且现在我已经开始使用网易的airtest的poco测试框架,想找一个通用的方法来同时运用到appium和airtest上,首先想到的是利用ocr识别的方式,事实证明这是可行的。但是我不建议使用python的 ocr库,对中文的识别率太差,像瞎了一样。

 

我的实现思路是: 

  1. 弹出toat
  2. 截图
  3. 上传到百度
  4. 百度返回截图中的所有文字信息
  5. 判断toast的提示文字是否存在于百度返回的数据中

这里有个问题就是不同机型的反应速度不同,弹出的toast的时间也不同,这个问题还没有好的方法解决。

实例:

我要识别下图中的toast:

首先,将截图上传至百度ocr:

 


import requests

def OCR(img):
    host = 'https://aip.baidubce.com/oauth/2.0/token?'
    headers = {
    'Content-Type':'application/json; charset=UTF-8'
    }
    data = {
        'grant_type': 'client_credentials',
        'client_id': '填自己的百度申请',
        'client_secret': '填自己的百度申请'
    }
    json = requests.post(host, headers=headers, data=data)
    r1 = json.json()
    itoken = r1['access_token']
    host = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=%s' % itoken
    headers = {
        'Content-Type': 'application/json; charset=UTF-8'
    }
    data = {
        'image': '%s' % img
    }
    json = requests.post(host, headers=headers, data=data)
    return json.text

img是base64编码后的,拿到返回值后,直接包含比较即可,如下图返回的数据:


{
	"errno": 0,
	"msg": "success",
	"data": {
		"log_id": "8039567037755707087",
		"direction": 0,
		"words_result_num": 11,
		"words_result": [
			{
				"words": "7:17PM",
				"probability": {
					"variance": 0.000541,
					"average": 0.978762,
					"min": 0.935626
				}
			},
			{
				"words": "Q1766117745",
				"probability": {
					"variance": 0.013189,
					"average": 0.963019,
					"min": 0.599857
				}
			},
			{
				"words": "登录",
				"probability": {
					"variance": 0,
					"average": 0.999951,
					"min": 0.999906
				}
			},
			{
				"words": "q‖Wer",
				"probability": {
					"variance": 0.040579,
					"average": 0.835989,
					"min": 0.498475
				}
			},
			{
				"words": "u",
				"probability": {
					"variance": 0,
					"average": 0.794249,
					"min": 0.794249
				}
			},
			{
				"words": "as d f h",
				"probability": {
					"variance": 0.007597,
					"average": 0.945991,
					"min": 0.795038
				}
			},
			{
				"words": "合",
				"probability": {
					"variance": 0,
					"average": 0.961466,
					"min": 0.961466
				}
			},
			{
				"words": "登录密码错误",
				"probability": {
					"variance": 0,
					"average": 0.999754,
					"min": 0.999369
				}
			},
			{
				"words": "n mx",
				"probability": {
					"variance": 0.029953,
					"average": 0.792049,
					"min": 0.618979
				}
			},
			{
				"words": "符123囚",
				"probability": {
					"variance": 0.021209,
					"average": 0.926885,
					"min": 0.635622
				}
			},
			{
				"words": "中尖",
				"probability": {
					"variance": 0.101298,
					"average": 0.674946,
					"min": 0.356673
				}
			}
		]
	}
}

可以看到我们的toast文字已经包含在了返回的数据中,则可断言测试用例通过

Last modification:November 19th, 2018 at 09:29 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment