当前位置:网站首页>What should I do if I encounter the problem of verification code during automatic testing?
What should I do if I encounter the problem of verification code during automatic testing?
2022-07-25 21:58:00 【Xiaowu knock code】
1. Find a developer to remove the captcha or use the universal captcha
2. Use OCR Automatic identification
Use OCR Automatic identification , Generally, the recognition rate is not too high , It's still no problem to handle general simple verification codes
What we use here is Tesseract-OCR, Download address :https://github.com/A9T9/Free-Ocr-Windows-Desktop/releases
How to use it ?
Enter the installed Directory :
tesseract.exe test.png test -1
Prepare a web page , The verification code is used above
<html>
<head>
<title>Table test by Young</title>
</head>
<body>
</br>
<h1> Test </h1>
<img src="http://csujwc.its.csu.edu.cn/sys/ValidateCode.aspx?t=1">
</br>
</body>
</html>
To identify the verification code , First, get the verification code , These two paragraphs are right The way of screenshots of page elements , First get a screenshot of the entire page
Then find the coordinates of page elements to intercept
/**
* 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();
}
}
Intercepted element , You can call Tesseract-OCR Generate text
// use Tesseract to get strings
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C tesseract.exe D:\\Tesseract-OCR\\test.png D:\\Tesseract-OCR\\test -1 ");
Next through java Read txt
/**
* This method for read TXT file
*
* @param filePath
*/
public static void readTextFile(String filePath) {
try {
String encoding = "GBK";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
// Judge whether the file exists
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// Considering the encoding format
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
}
read.close();
} else {
System.out.println(" The specified file could not be found ");
}
} catch (Exception e) {
System.out.println(" Error reading file contents ");
e.printStackTrace();
}
}
The overall code is as follows :
1 package com.dbyl.tests;
2
3 import java.awt.Rectangle;
4 import java.awt.image.BufferedImage;
5 import java.io.BufferedReader;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.io.Reader;
11 import java.util.concurrent.TimeUnit;
12
13 import javax.imageio.ImageIO;
14
15 import org.apache.commons.io.FileUtils;
16 import org.openqa.selenium.By;
17 import org.openqa.selenium.OutputType;
18 import org.openqa.selenium.Point;
19 import org.openqa.selenium.TakesScreenshot;
20 import org.openqa.selenium.WebDriver;
21 import org.openqa.selenium.WebElement;
22
23 import com.dbyl.libarary.utils.DriverFactory;
24
25 public class TesseractTest {
26
27 public static void main(String[] args) throws IOException,
28 InterruptedException {
29
30 WebDriver driver = DriverFactory.getChromeDriver();
31 driver.get("file:///C:/Users/validation.html");
32 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
33 WebElement element = driver.findElement(By.xpath("//img"));
34
35 // take screen shot for element
36 screenShotForElement(driver, element, "D:\\Tesseract-OCR\\test.png");
37
38 driver.quit();
39
40 // use Tesseract to get strings
41 Runtime rt = Runtime.getRuntime();
42 rt.exec("cmd.exe /C tesseract.exe D:\\Tesseract-OCR\\test.png D:\\Tesseract-OCR\\test -1 ");
43
44 Thread.sleep(1000);
45 // Read text
46 readTextFile("D:\\Tesseract-OCR\\test.txt");
47 }
48
49 /**
50 * This method for read TXT file
51 *
52 * @param filePath
53 */
54 public static void readTextFile(String filePath) {
55 try {
56 String encoding = "GBK";
57 File file = new File(filePath);
58 if (file.isFile() && file.exists()) {
// Judge whether the file exists
59 InputStreamReader read = new InputStreamReader(
60 new FileInputStream(file), encoding);// Considering the encoding format
61 BufferedReader bufferedReader = new BufferedReader(read);
62 String lineTxt = null;
63 while ((lineTxt = bufferedReader.readLine()) != null) {
64 System.out.println(lineTxt);
65 }
66 read.close();
67 } else {
68 System.out.println(" The specified file could not be found ");
69 }
70 } catch (Exception e) {
71 System.out.println(" Error reading file contents ");
72 e.printStackTrace();
73 }
74 }
75
76 /**
77 * This method for screen shot element
78 *
79 * @param driver
80 * @param element
81 * @param path
82 * @throws InterruptedException
83 */
84 public static void screenShotForElement(WebDriver driver,
85 WebElement element, String path) throws InterruptedException {
86 File scrFile = ((TakesScreenshot) driver) 87 .getScreenshotAs(OutputType.FILE); 88 try { 89 Point p = element.getLocation(); 90 int width = element.getSize().getWidth(); 91 int height = element.getSize().getHeight(); 92 Rectangle rect = new Rectangle(width, height); 93 BufferedImage img = ImageIO.read(scrFile); 94 BufferedImage dest = img.getSubimage(p.getX(), p.getY(), 95 rect.width, rect.height); 96 ImageIO.write(dest, "png", scrFile); 97 Thread.sleep(1000); 98 FileUtils.copyFile(scrFile, new File(path));
99 } catch (IOException e) {
100 e.printStackTrace();
101 }
102 }
103
104 }
Finally, thank everyone who reads my article carefully , The following online link is also a very comprehensive one that I spent a few days sorting out , I hope it can also help you in need !

These materials , For those who want to change careers 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful ……
If you don't want to grow up alone , Unable to find the information of the system , The problem is not helped , If you insist on giving up after a few days , You can click the small card below to join our group , We can discuss and exchange , There will be various software testing materials and technical exchanges .
Typing is not easy , If this article is helpful to you , Click a like, collect a hide and pay attention , Give the author an encouragement . It's also convenient for you to find it quickly next time .
Self study recommendation B Stop video :
Zero basis transition software testing : Self taught software testing , Got the byte test post offer, Is the B The best video station !
Advanced automation testing : Huawei has landed , Salary increase 20K,2022 Most suitable for self-study python Automated test tutorial , Spend it yourself 16800 Bought , Free sharing

边栏推荐
- [fan Tan] in detail: lower control, upward management, upward drawing cake.
- Guys, how can Flink SQL submit tasks in per job mode?
- 在腾讯干软件测试3年,7月无情被辞,想给划水的兄弟提个醒...
- 测试工作不受重视,你换位思考了吗?
- I/o case practice
- 如何快速搭建图片服务器[通俗易懂]
- 性能调试 -- Chrome Performance
- How to use RS485 half duplex chip correctly
- Jmeter--- set proxy recording request
- Redis master-slave architecture lock failure problem (master-slave)
猜你喜欢
![[leetcode ladder] linked list · 021 merge two ordered linked lists](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[leetcode ladder] linked list · 021 merge two ordered linked lists

Performance debugging -- chrome performance

New maixhub deployment (v831 and k210)

【饭谈】那些看似为公司着想,实际却让人无法理解的事(二:面试时的软素质“眼缘”)

GPON introduction and Huawei OLT gateway registration and configuration process

GPON介绍及华为OLT网关注册配置流程

Shopify sellers: share some tips for social media marketing!

I'm also drunk. Eureka delayed registration and this pit!

Create EDA - why should I learn EDA
![[database] conceptual design, logical design, relational database design theory](/img/4d/be7ab21c98fc1bf4b63e4abe22d9fc.png)
[database] conceptual design, logical design, relational database design theory
随机推荐
I/O案例实操
这次龙蜥展区玩的新花样,看看是谁的 DNA 动了?
Redis内存淘汰机制?
Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
JS timer and swiper plug-in
动画曲线天天用,你能自己整一个吗?看完这篇你就会了!
Redis master-slave architecture lock failure problem (master-slave)
[fan Tan] in detail: lower control, upward management, upward drawing cake.
Sofa weekly | open source person - Niu Xuewei, QA this week, contributor this week
I/o case practice
The reisson distributed lock renewal failed due to network reasons, resulting in the automatic release of the lock when the business is still executing but the lock is not renewed.
[51Nod1676 无向图同构]无向图哈希[通俗易懂]
【Flink】FLink RocksDBListState 报错 You cannot add null to a ListState
Optimization analysis of storage structure and IO performance of openharmony littlefs file system
zigbee开发板(nxpzigbee开发)
The file cannot be saved (what if the folder is damaged and cannot be read)
Creation and destruction of function stack frames
How will Web3 change the future of people?
How to quickly build a picture server [easy to understand]
测试工作不受重视,你换位思考了吗?