当前位置:网站首页>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

边栏推荐
- Protobuf的简单使用
- Solutions to the failure of win key in ikbc keyboard
- Create EDA - why should I learn EDA
- What are the application characteristics of NTU general database gbase Bi?
- The file cannot be saved (what if the folder is damaged and cannot be read)
- Redis 使用详解
- At present, flynk CDC does not support mysql5.5. If you change the source code and release this restriction, there will be a lot of data problems?
- PE格式: 分析IatHook并实现
- 五种分配方式是否会产生内部碎片、外部碎片
- [dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality "eye edge" during interview)
猜你喜欢

Babbitt | metauniverse daily must read: the popularity of virtual people has decreased, and some "debut is the peak", and the onlookers have dispersed

Bitcoin.com:usdd represents a truly decentralized stable currency

Ijcai2022 meeting! Microsoft and other tutorials on domain generalization

Share | intelligent fire emergency management platform solution (PDF attached)

字节跳动技术面都过了,结果还是被刷了,问HR原因竟是。。。

8000 word super detailed custom structure type

如何用 Redis 实现分布式锁的?

【leetcode天梯】链表 · 876 查找链表中间结点
![[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决](/img/0b/da67b5a361a2cdfaf81568d34cf5f7.png)
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决

【汇编语言01】基础知识
随机推荐
How to quickly build a picture server [easy to understand]
狗粮的成分
Share | intelligent fire emergency management platform solution (PDF attached)
Redis内存淘汰机制?
Web3 entrepreneurship has all the elements of explosive growth of innovation
立创EDA——我为什么要学EDA
[dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality "eye edge" during interview)
[fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
YUV420 yuv420sp image format "recommended collection"
Jmeter--- set proxy recording request
Protobuf的简单使用
YUV420 YUV420sp 图像格式「建议收藏」
IJCAI2022开会了! 微软等《领域泛化Domain Generalization》教程
3. Editors (vim)
[MAIXPY]kpu: load error:2005, ERR_ READ_ File: read file failed problem solving
Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
Is there any document for synchronizing from Oracle to ODPs?
FAW red flag "King fried" is listed, which is safe and comfortable
PE格式: 分析IatHook并实现
6-18漏洞利用-后门连接