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


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

    2.使用OCR自动识别

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

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

    怎么使用呢?

    进入安装后的目录:

    tesseract.exe test.png test -1

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

    1. <html>
    2. <head>
    3. <title>Table test by Young</title>
    4. </head>
    5. <body>
    6. </br>
    7. <h1> Test </h1>
    8. <img src="http://csujwc.its.csu.edu.cn/sys/ValidateCode.aspx?t=1">
    9. </br>
    10. </body>
    11. </html>

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

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

    1. /**
    2. * This method for screen shot element
    3. *
    4. * @param driver
    5. * @param element
    6. * @param path
    7. * @throws InterruptedException
    8. */
    9. public static void screenShotForElement(WebDriver driver,
    10. WebElement element, String path) throws InterruptedException {
    11. File scrFile = ((TakesScreenshot) driver)
    12. .getScreenshotAs(OutputType.FILE);
    13. try {
    14. Point p = element.getLocation();
    15. int width = element.getSize().getWidth();
    16. int height = element.getSize().getHeight();
    17. Rectangle rect = new Rectangle(width, height);
    18. BufferedImage img = ImageIO.read(scrFile);
    19. BufferedImage dest = img.getSubimage(p.getX(), p.getY(),
    20. rect.width, rect.height);
    21. ImageIO.write(dest, "png", scrFile);
    22. Thread.sleep(1000);
    23. FileUtils.copyFile(scrFile, new File(path));
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. }

    截取完元素,就可以调用Tesseract-OCR生成text

    1. // use Tesseract to get strings
    2. Runtime rt = Runtime.getRuntime();
    3. rt.exec("cmd.exe /C tesseract.exe D:\\Tesseract-OCR\\test.png D:\\Tesseract-OCR\\test -1 ");

    接下来通过java读取txt

    1. /**
    2. * This method for read TXT file
    3. *
    4. * @param filePath
    5. */
    6. public static void readTextFile(String filePath) {
    7. try {
    8. String encoding = "GBK";
    9. File file = new File(filePath);
    10. if (file.isFile() && file.exists()) { // 判断文件是否存在
    11. InputStreamReader read = new InputStreamReader(
    12. new FileInputStream(file), encoding);// 考虑到编码格式
    13. BufferedReader bufferedReader = new BufferedReader(read);
    14. String lineTxt = null;
    15. while ((lineTxt = bufferedReader.readLine()) != null) {
    16. System.out.println(lineTxt);
    17. }
    18. read.close();
    19. } else {
    20. System.out.println("找不到指定的文件");
    21. }
    22. } catch (Exception e) {
    23. System.out.println("读取文件内容出错");
    24. e.printStackTrace();
    25. }
    26. }

    整体代码如下:

    1. 1 package com.dbyl.tests;
    2. 2
    3. 3 import java.awt.Rectangle;
    4. 4 import java.awt.image.BufferedImage;
    5. 5 import java.io.BufferedReader;
    6. 6 import java.io.File;
    7. 7 import java.io.FileInputStream;
    8. 8 import java.io.IOException;
    9. 9 import java.io.InputStreamReader;
    10. 10 import java.io.Reader;
    11. 11 import java.util.concurrent.TimeUnit;
    12. 12
    13. 13 import javax.imageio.ImageIO;
    14. 14
    15. 15 import org.apache.commons.io.FileUtils;
    16. 16 import org.openqa.selenium.By;
    17. 17 import org.openqa.selenium.OutputType;
    18. 18 import org.openqa.selenium.Point;
    19. 19 import org.openqa.selenium.TakesScreenshot;
    20. 20 import org.openqa.selenium.WebDriver;
    21. 21 import org.openqa.selenium.WebElement;
    22. 22
    23. 23 import com.dbyl.libarary.utils.DriverFactory;
    24. 24
    25. 25 public class TesseractTest {
    26. 26
    27. 27 public static void main(String[] args) throws IOException,
    28. 28 InterruptedException {
    29. 29
    30. 30 WebDriver driver = DriverFactory.getChromeDriver();
    31. 31 driver.get("file:///C:/Users/validation.html");
    32. 32 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    33. 33 WebElement element = driver.findElement(By.xpath("//img"));
    34. 34
    35. 35 // take screen shot for element
    36. 36 screenShotForElement(driver, element, "D:\\Tesseract-OCR\\test.png");
    37. 37
    38. 38 driver.quit();
    39. 39
    40. 40 // use Tesseract to get strings
    41. 41 Runtime rt = Runtime.getRuntime();
    42. 42 rt.exec("cmd.exe /C tesseract.exe D:\\Tesseract-OCR\\test.png D:\\Tesseract-OCR\\test -1 ");
    43. 43
    44. 44 Thread.sleep(1000);
    45. 45 // Read text
    46. 46 readTextFile("D:\\Tesseract-OCR\\test.txt");
    47. 47 }
    48. 48
    49. 49 /**
    50. 50 * This method for read TXT file
    51. 51 *
    52. 52 * @param filePath
    53. 53 */
    54. 54 public static void readTextFile(String filePath) {
    55. 55 try {
    56. 56 String encoding = "GBK";
    57. 57 File file = new File(filePath);
    58. 58 if (file.isFile() && file.exists()) { // 判断文件是否存在
    59. 59 InputStreamReader read = new InputStreamReader(
    60. 60 new FileInputStream(file), encoding);// 考虑到编码格式
    61. 61 BufferedReader bufferedReader = new BufferedReader(read);
    62. 62 String lineTxt = null;
    63. 63 while ((lineTxt = bufferedReader.readLine()) != null) {
    64. 64 System.out.println(lineTxt);
    65. 65 }
    66. 66 read.close();
    67. 67 } else {
    68. 68 System.out.println("找不到指定的文件");
    69. 69 }
    70. 70 } catch (Exception e) {
    71. 71 System.out.println("读取文件内容出错");
    72. 72 e.printStackTrace();
    73. 73 }
    74. 74 }
    75. 75
    76. 76 /**
    77. 77 * This method for screen shot element
    78. 78 *
    79. 79 * @param driver
    80. 80 * @param element
    81. 81 * @param path
    82. 82 * @throws InterruptedException
    83. 83 */
    84. 84 public static void screenShotForElement(WebDriver driver,
    85. 85 WebElement element, String path) throws InterruptedException {
    86. 86 File scrFile = ((TakesScreenshot) driver)
    87. 87 .getScreenshotAs(OutputType.FILE);
    88. 88 try {
    89. 89 Point p = element.getLocation();
    90. 90 int width = element.getSize().getWidth();
    91. 91 int height = element.getSize().getHeight();
    92. 92 Rectangle rect = new Rectangle(width, height);
    93. 93 BufferedImage img = ImageIO.read(scrFile);
    94. 94 BufferedImage dest = img.getSubimage(p.getX(), p.getY(),
    95. 95 rect.width, rect.height);
    96. 96 ImageIO.write(dest, "png", scrFile);
    97. 97 Thread.sleep(1000);
    98. 98 FileUtils.copyFile(scrFile, new File(path));
    99. 99 } catch (IOException e) {
    100. 100 e.printStackTrace();
    101. 101 }
    102. 102 }
    103. 103
    104. 104 }

     最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

     文档获取方式:点击右边链接领取:软件测试全套资料分享   

  • 相关阅读:
    MoSE论文中Sequential Synthetic Dataset生成代码(时间序列多任务学习数据集)
    云原生爱好者周刊:使用 Cilium 和 Grafana 实现无侵入可观测性
    Docker 入门看这一篇就够了,万字详解!
    Promise的学习
    [附源码]java毕业设计冷链物流管理系统论文
    es重启临时关闭自动分片
    遍历数组的10个高阶函数
    MLX90640 红外热成像仪测温传感器模块开发笔记(十) 成果展示-红眼睛相机
    怎么将自己拍摄的视频静音?详细步骤教会你~
    Harmony装饰器
  • 原文地址:https://blog.csdn.net/yjt2045263063/article/details/134080283