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

边栏推荐
猜你喜欢
![[dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality](/img/11/42c4674d23ee93850fb3d2de0d0932.png)
[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)

动画曲线天天用,你能自己整一个吗?看完这篇你就会了!

Ansible+cronab batch deployment patrol

Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (judgment question) June 2022

Ijcai2022 meeting! Microsoft and other tutorials on domain generalization

ORIGYN基金会正式启动$OGY Staking,引领新一轮生态利好

新版Maixhub部署(V831与K210)

6-17漏洞利用-反序列化远程命令执行漏洞

2022最新软件测试八股文,能不能拿心仪Offer就看你背得怎样了

2 lines of code to generate a solid desktop background
随机推荐
[hand tear STL] BitSet (bitmap), bloom filter
[fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
【测开方法论】测开平台pk心得-抉择
[redis underlying parsing] string type
919. Complete binary tree inserter: simple BFS application problem
Composition of dog food
自动化测试岗花20K招人,到最后居然没一个合适的,招两个应届生都比他们强吧
FAW red flag "King fried" is listed, which is safe and comfortable
JMeter websocket接口测试
Excuse me, how to deal with repeated consumption of MySQL data
【饭谈】如何设计好一款测试平台?
【饭谈】测试平台为什么有组件化?模块化?很多看不到的地方设计的很好是种浪费么?
磁盘空间的三种分配方式
新版Maixhub部署(V831与K210)
若依如何解决导出使用下载插件出现异常?
JS timer and swiper plug-in
再次来光顾
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?
Golang: MVC models
c sqlite ... ...