Web自动化测试之图文验证码的解决方案

对于web应用程序来讲,处于安全性考虑,在登录的时候,都会设置验证码, 验证码的类型种类繁多,有图片中辨别数字字母的,有点击图片中指定的文字的,也有算术计算结果的,再复杂一点就是滑动验证的。 诸如此类的验证码,对我们的系统增加了安全性的保障,但是对于我们测试人员来讲,在自动化测试的过程中,无疑是一个棘手的问题。

1、Web 化验证码解决方案

一般在我们测试过程中,登录遇到上述的验证码的时候,有以下种解决方案:

  • 第一种、让开发去掉验证码
  • 第二种、设置一个万能的验证码
  • 第三种、通过 cookie 绕过登录
  • 第四种、自动识别技术识别验证码

2、验证码解决方案

复制代码
# coding:utf-8
import os
import subprocess
from PIL import Image
 
 
def get_captcha(driver, captcha_id, full_screen_img_path, captcha_img_path, captcha_final_path, txt_path, ocr_path):
    # 浏览器界面截图
    driver.save_screenshot(full_screen_img_path)
    # 找到验证码图片,得到它的坐标
    element = driver.find_element_by_id(captcha_id)
    left = element.location['x']
    top = element.location['y']
    right = element.location['x'] + element.size['width']
    bottom = element.location['y'] + element.size['height']
    left, top, right, bottom = int(left), int(top), int(right), int(bottom)
    img = Image.open(full_screen_img_path)
    img = img.crop((left, top, right, bottom))
    # 得到验证码图片
    img.save(captcha_img_path)
    # 打开验证码图片
    img = Image.open(captcha_img_path)
    # 颜色直方图,255种颜色,255为白色
    # 新建一张图片(大小和原图大小相同,背景颜色为255白色)
    img_new = Image.new('P', img.size, 255)
    for x in range(img.size[1]):
        for y in range(img.size[0]):
            # 遍历图片的xy坐标像素点颜色
            pix = img.getpixel((y, x))
            # print(pix)
            # 自己调色,r=0,g=0,b>0为蓝色
            if pix[0] < 20 and pix[1] < 20 and pix[2] > 50:
                # 把遍历的结果放到新图片上,0为透明度,不透明
                img_new.putpixel((y, x), 0)
    img_new.save(captcha_final_path, format='png')
 
    # 通过tesseract工具解析验证码图片,生成文本
    os.system(ocr_path)
 
    # 读取txt文件里面的验证码
    with open(txt_path, 'r') as f:
        if f.read():
            t = f.read().strip()
            # 去掉中间空格
            if ' ' in t:
                t = t.replace(' ', '')
            if t.isdigit() and len(t) == 4:
                return t
            else:
                return 'fail'
 
 
def check_resp(result, msg):
    if msg in result:
        return 'pass'
    else:
        return 'failed'
 
 
# 接口 - 识别验证码
def get_captcha(captcha_img_path, captcha_final_path, txt_path, ocr_path):
 
    # 打开验证码图片
    img = Image.open(captcha_img_path)
 
    # 新建一张图片(大小和原图大小相同,背景颜色为255白色)
    img_new = Image.new('P', img.size, 55)
    for x in range(img.size[1]):
        for y in range(img.size[0]):
            # 遍历图片的xy坐标像素点颜色
            pix = img.getpixel((y, x))
            # print(pix)
            # 自己调色,r=0,g=0,b>0为蓝色
            if pix[0] < 20 and pix[1] < 20 and pix[2] > 50:
                # 把遍历的结果放到新图片上,0为透明度,不透明
                img_new.putpixel((y, x), 0)
    img_new.save(captcha_final_path, format='png')
 
    # 通过tesseract工具解析验证码图片,生成文本,【Tesseract-OCR必须和jpg的根目录必须相同,如C盘、D盘!!!】
    os.system(ocr_path)
 
    # 读取txt文件里面的验证码
    with open(txt_path, 'r') as f:
        if r.read():
            t = f.read().strip()
            # 去掉中间空格
            if ' ' in t:
                t = t.replace(' ', '')
            # 如果是数字且长度为4,就返回数字,如果不是就返回 fail
            if t.isdigit() and len(t) == 4:
                return t
            else:
                return fail

下面是配套学习资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

软件测试面试小程序

被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!

涵盖以下这些面试题板块:

1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux

6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

资料获取方式 :

相关推荐
7***998731 分钟前
GaussDB数据库中SQL诊断解析之配置SQL限流
数据库·sql·gaussdb
小奶包他干奶奶34 分钟前
Webpack学习——Loader(文件转换器)
前端·学习·webpack
zy happy1 小时前
若依 vue3 报错:找不到模块“@/api/xxxx/xxxxx”或其相应的类型声明。。Vue 3 can not find mod
前端·javascript·vue.js
青瓷程序设计1 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
tobebetter95271 小时前
How to manage python versions on windows
开发语言·windows·python
潘小安1 小时前
Git Worktree + Claude Code:让你的开发效率翻倍的秘密武器
前端
F_D_Z1 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
meichaoWen2 小时前
【Vue3】vue3的全面学习(一)
前端·javascript·学习
Wang's Blog2 小时前
MongoDB小课堂: 文档操作核心技术指南:主键机制、CRUD操作与最佳实践
数据库·mongodb
小猪努力学前端2 小时前
在 React + React Router v7 SSR 项目里做多端适配,我踩的两个坑
前端·react.js