"Automated test" successfully signed in using Java+selenium to complete verification code

Source: Internet
Author: User
Tags log4j

This is my first time to send a blog, if there is a problem, please advise!

This time is to help solve, if in peacetime automation encountered a verification code fill in the case, how we successfully login.

    1. Ideas:

First we copy the verification code and save it as a picture, and then use TESSERACT-OCR this optical software to identify the TXT file, and then we just read the contents of the TXT file can be

2. Use the tool:

Eclipse, Selenium, Firefox, log4j, autoit_3.3.14.1, TESSERACT-OCR

3. Installing the Software

I. Eclipse installation, download and install directly online

II. Selenium and log4j, these two are a jar package, only need to download down can, and then we build the project when we can create a file lib saved up

    

How do I use these two jar packages? Project à right-click build path à configure acquisition path à tap add Jarj

III. Firefox installation, it is recommended to install it by default. In case you need to add a line to the code to find the Firefox path.

Iv.. autoit_3.3.14.1 installation, why install this? is to solve the window's own operation, for example, save, because I need to save as a picture, so this can help me solve. Installation, I was straight to the next

V. Tesseract-oc, the same installation, but need to restart the computer, it will write the path

    1. Steps to resolve the verification code

Public Static void Baocuntupian (webdriver driver) throws Awtexception, interruptedexception{

Right-click on the browser's properties

Actions action = New actions (driver);

Action. Contextclick (Driver.findelement (by. ID("ImageButton1")). Build (). Perform ();

System. out. Print ("Right-click to open Browser properties \ n");

Robot Robot = new Robot ();

Thread. Sleep (1000);

Robot.keypress (keyevent. Vk_down);

System. out. Print ("Move to view image");

Thread. Sleep (1000);

Robot.keypress (keyevent. Vk_down);

System. out. Print ("Move to copy image" +

"\ n");

Thread. Sleep (1000);

Robot.keypress (keyevent. Vk_down);

System. out. Print ("Move to copy image address \ n");

Thread. Sleep (1000);

Robot.keypress (keyevent. Vk_down);

System. out. Print ("Move to save image as \ n");

Thread. Sleep (1000);

Robot.keypress (keyevent. Vk_enter);

Thread. Sleep (1000);

System. out. Print ("Click on the image pop-up save \ n");

Robot.keyrelease (keyevent. Vk_down);//Release down arrow, otherwise entries before this will work

Thread. Sleep (1000);

Try {

Runtime. GetRuntime (). EXEC ("C:\\test11.exe");//Run Save

System. out. Print ("Save picture succeeded \ n");

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

This code is interpreted in two parts: right-click Popup property and save picture

We will point the position of the mouse to the picture of the CAPTCHA, then right click

Then we need to find "save image as"

This code is a description of how we found and saved it.

Then, the play out of the Save as the operation is no longer Java can operate, we need to use another VB-like scripting language, so our AutoIt debut.

Use AutoitInfo.exe to locate

Then open our scripting tool SciTE.exe

PS: In fact, the following code I do not really understand

This section of code is to clean up if I want to exist the picture path exists with the same name, if the same name first delete picture

This code is the name of our saved image named A.png, and then save to the C drive.

After we have written this code, we then use CTRL+F7 to make it an EXE file. If the virus is reported, please do not ignore it, to ensure that there is no virus.

Well, then we need to call our script. My script name is Test11.exe, saved on this machine's C drive

OK, we've got the location of the picture saved right now. Then we need to use TESSERACT-OCR this software to identify our image content, using the code is a DOS command,

The command is as follows: Tesseract.exe c:/a.png C:/A-PSM 6

The a.png is identified and saved to the C-drive a.txt

Some people use Tesseract.exe c:/a.png c:/a-l to solve, I do not know why need to use-PSM 6 to solve

We will save this command as a. bat format

Then, we use the Java runtime. GetRuntime (). exec () This method can be called.

Since it is saved as a file format, then we need to read the contents of the file, read the file contents of the code, I do not tell the

Public Static String txt2string (file file) {

String result = "";

Try {

BufferedReader br = new bufferedreader (new FileReader);//Constructs a BufferedReader class to read the file

String s = null;

while ((s = br.readline ()) =null) {//Use the ReadLine method to read one line at a time

result = result + "\ n" +s;

}

Br.close ();

}catch(Exception e) {

E.printstacktrace ();

}

return result;

}

Main method invocation

File File = new file ("C:/a.txt");//Read files

System. out. println (txt2string(file) + "a");

The problem: The orc recognition rate is still very low, the chance of login success has not exceeded 90%, need to improve

Solution: Train the Orc recognition rate (time to launch similar articles)

"Automated test" successfully signed in using Java+selenium to complete verification code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.