安卓ui自动化测试 验证码自动识别输入 登录

 

 

 这里的文字 是有ID位置的,并且图片是背景图片,所以不需要图片文字识别操作。

 

# -*- encoding=utf8 -*-
__author__ = "admin-fly"

from airtest.core.api import *
auto_setup(__file__)

from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)


#poco(text="CF-MY-Sta").click()
stop_app('sta.onion.cosyfans')

start_app('sta.onion.cosyfans')

sleep(2)
poco("android.widget.ImageView").click()
poco("android.widget.ImageView").click()
sleep(1)
poco("android.widget.LinearLayout").offspring("android:id/content").offspring("sta.onion.cosyfans:id/nav_view").offspring("sta.onion.cosyfans:id/nav_mycosy").child("sta.onion.cosyfans:id/icon").click()
poco("sta.onion.cosyfans:id/btn_login").click()

poco("sta.onion.cosyfans:id/edit_phone").set_text("")
poco("sta.onion.cosyfans:id/edit_phone").set_text("191234000")
sleep(1)
poco("sta.onion.cosyfans:id/edit_pass").set_text("")
poco("sta.onion.cosyfans:id/edit_pass").set_text("qwe123")

a1=poco("sta.onion.cosyfans:id/tv_code").attr('text')
log("需要点击得验证码得数值为:")
print(a1)
b1=poco("sta.onion.cosyfans:id/tv_code").get_text()
print(b1)
#获取 待验证的验证码 数字
V_code1=a1[1]
V_code2=a1[2]
V_code3=a1[3]

#需要点击的验证码的数值
t01=poco("sta.onion.cosyfans:id/tv_code1").attr('text')
t02=poco("sta.onion.cosyfans:id/tv_code2").attr('text')
t03=poco("sta.onion.cosyfans:id/tv_code3").attr('text')
t04=poco("sta.onion.cosyfans:id/tv_code4").attr('text')
print(t01)
t1=t01[0]
t2=t02[0]
t3=t03[0]
t4=t04[0]

#拿第1个字符去跟图片上的文字比较,匹配则 选中
if V_code1==t1:
poco("sta.onion.cosyfans:id/tv_code1").click()
elif V_code1==t2:
poco("sta.onion.cosyfans:id/tv_code2").click()
elif V_code1==t3:
poco("sta.onion.cosyfans:id/tv_code3").click()
else:
poco("sta.onion.cosyfans:id/tv_code4").click()

#拿第2个字符去跟图片上的文字比较,匹配则 选中
if V_code2==t1:
poco("sta.onion.cosyfans:id/tv_code1").click()
elif V_code2==t2:
poco("sta.onion.cosyfans:id/tv_code2").click()
elif V_code2==t3:
poco("sta.onion.cosyfans:id/tv_code3").click()
else:
poco("sta.onion.cosyfans:id/tv_code4").click()

#拿第3个字符去跟图片上的文字比较,匹配则 选中

if V_code3==t1:
poco("sta.onion.cosyfans:id/tv_code1").click()
elif V_code3==t2:
poco("sta.onion.cosyfans:id/tv_code2").click()
elif V_code3==t3:
poco("sta.onion.cosyfans:id/tv_code3").click()
else:
poco("sta.onion.cosyfans:id/tv_code4").click()

#登录操作
poco("sta.onion.cosyfans:id/btn_login").click()


poco(text="My Cosy").click()

#验证登录是否成功
assert_exists(Template(r"tpl1620789782432.png", record_pos=(0.19, -0.867), resolution=(1080, 2160)), "以进入个人中心作为登录得断言结果")

上面的这些 因为是有ID位置的 并且是有文本文字的,它只是嵌入了背景图片,所以不需要图片文字识别,以上的方法就可以 直接 自动输入登录了!。。。。。。 

 

关于嵌入图片中的文字 识别的一些看法:

验证码本意是为了加强安全防控,规避恶意登录。所以对于需要图片识别的验证码 去识别破解,识别率不高。

PS:对于文字嵌入 图片中,并且文字的位置是动态变化的 这种图片文字识别验证的话 存在一个识别率的问题,并且位置动态变化,识别这个定位也不准确。

在识别之前对图片进行了 对比度、色彩度等加强处理,发现识别的效果也不明显。

图片识别采用了 百度云API中的文字识别,基本每次只能识别2-3个文字, 我做了一个循环去识别也无法满足要求,并且百度云API请求次数还有限制。

如果非要利用图片识别破解,那必须需要写一个识别算法的方法,然后拿到原图进行识别,并且为了提升识别率,再循环调用算法 匹配正确则停止调用。

这种图片文字识别的 不好解决,只能通过 请求 登录接口 进入系统主页,拿到浏览器cookies信息,再刷新页面,这样进入系统主页 绕过验证码。

 

posted @ 2021-05-12 15:11  BKY007-谢志飞  阅读(1021)  评论(0)    收藏  举报