在进行自动化测试,遇到验证码的问题,怎么办?

面对自动化测试中的验证码问题,可以尝试让开发去除验证码或者采用OCR识别。OCR识别如Tesseract-OCR,虽然识别率有限,但能处理简单验证码。通过截图页面元素并定位坐标来实现。同时分享了软件测试相关的面试题、职业选择及工作经验。
摘要由CSDN通过智能技术生成

1.找开发去掉验证码或者使用万能验证码

2.使用OCR自动识别

使用OCR自动化识别,一般识别率不是太高,处理一般简单验证码还是没问题

这里使用的是Tesseract-OCR,下载地址:https://github.com/A9T9/Free-Ocr-Windows-Desktop/releases

怎么使用呢?

进入安装后的目录:

tesseract.exe test.png test -1
图片

准备一份网页,上面使用该验证码

<html>
<head>
<title>Table test by Youngtitle>
head>
<body>
 br>
<h1> Test h1>
 <img src="http://csujwc.its.csu.edu.cn/sys/ValidateCode.aspx?t=1">
 br>
body>
html>

要识别验证码,首先得取得验证码,这两款采取对 页面元素部分截图的方式,首先获取整个页面的截图

然后找到页面元素坐标进行截取

/**
     * This method for screen shot element
     * 
     * @param driver
     * @param element
     * @param path
     * @throws InterruptedException
     */
    public static void screenShotForElement(WebDriver driver,
            WebElement element, String path) throws InterruptedException {
   
        File scrFile = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        try {
   
            Point p = element.getLocation();
            int width = element.getSize().getWidth();
            int height = element.getSize().getHeight();
            Rectangle rect = new Rectangle(width, height);
            BufferedImage img = ImageIO.read(scrFile);
            BufferedImage dest = img.getSubimage(p.getX(), p.getY(),
                    rect.width, rect.height);
            ImageIO.write(dest, "png", scrFile);
            Thread.sleep(1000);
            FileUtils.copyFile(scrFile, new File(path));
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值