Jmeter—实现识别验证码登录
在做⾃动化测试或压⼒测试时,验证码总是⼀个问题。在以往的压⼒测试经历中,测试⼀般在独⽴的测试环境中进⾏,可以放⼼禁⽤验证码或使⽤万能验证码,这个是最实⽤的。但是,这两天我尝试了⼀个使⽤第三⽅的图形图像识别⼯具来完成验证码识别并通过Jmeter完成登录的过程,识别⼯具的识别成功率有限,因此本篇估计仅能在理论范围内适⽤。
本篇内容⼤部分内容来⾃于该作者的⽂章:blog.csdn/xreztento/article/details/48682923
总体⽬的:给Jmeter写⼀个后置处理器,⽤来将上⼀个请求响应返回的验证码图⽚识别成⽂字,并将识别内容保存为Jmeter的⼀个参数,这个参数供登录post请求进⾏登录验证,从⽽完成登录的⾃动化过程。
⼯具
(1)第三⽅图形图像识别⼯具:tesseract-ocr  下载地址:le/p/tesseract-ocr/downloads/list  基本⽆法下载,已上传到我的百度⽹盘
安装后,可以在cmd下试⼀试是否安装成功:
在cmd下输⼊命令:tesseract d:\123.jpg result -l eng  意思是将D盘下的123.jpg 识别后放在下
(2)需要⽤到的jar包:
Jmeter插件开发相关的jar包: ApacheJmeter_core.jar jorphon.jar logkit-2.0.jar  这些在Jmeter的lib中都有直接导⼊⼯程项⽬即可
图形处理相关的jar包:jai-imageio-1.1.jar  swingx-1.6.1.jar  从⽹上下的,已上传到百度云盘 jar ⽂件夹下
插件开发
⽤java IDE新建⼀个⼯程项⽬,实现两个部分,⼀个是识别图⽚,⼀个是Jmeter插件的UI部分。⼯程项⽬完成⽬录为:
st.huu;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import adata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import dia.imageio.plugins.tiff.TIFFImageWriteParam;
public class ImageIOHelper{
public static File createImage(File imageFile, String imageFormat) {
File tempFile = null;
ImageInputStream iis = null;
ImageOutputStream ios = null;
ImageReader reader = null;
ImageWriter writer = null;
try {
Iterator<ImageReader> readers = ImageReadersByFormatName(imageFormat);
reader = ();
iis = ateImageInputStream(imageFile);
reader.setInput(iis);
IIOMetadata streamMetadata = StreamMetadata();
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.CHINESE);            tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
Iterator<ImageWriter> writers = ImageWritersByFormatName("tiff");
writer = ();
BufferedImage bi = ad(0));
IIOImage image = new IIOImage(bi,ImageMetadata(0));
tempFile = tempImageFile(imageFile);
ios = ateImageOutputStream(tempFile);
writer.setOutput(ios);
writer.write(streamMetadata, image, tiffWriteParam);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(iis != null){
try {
iis.close();
} catch (IOException e) {
/
/ TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ios != null){
try {
ios.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(writer != null){
writer.dispose();
}
if(reader != null){
reader.dispose();
}
}
return tempFile;
}
private static File tempImageFile(File imageFile) {
String path = Path();
StringBuffer strB = new StringBuffer(path);
return new String().replaceFirst("jpg", "tif"));
}
//给图⽚降噪提⾼识别度
public static int isFilter(int colorInt) {
Color color = new Color(colorInt);
if ((Red() > 85 && Red() < 255)
&& (Green() > 85 && Green() < 255)
&& (Blue() > 85 && Blue() < 255)) {
}
return 0;
}
public static BufferedImage removeBackgroud(BufferedImage img)
throws Exception {
int width = Width();
int height = Height();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
html怎么实现登录验证功能
if (RGB(x, y)) == 1) {
img.setRGB(x, y, RGB());
}
}
}
return img;
}
}
ImageIOHelper.java
st.huu;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class OCR {
private final String LANG_OPTION = "-l";
private final String EOL = Property("line.separator");
private String tessPath = "D://Program Files (x86)//Tesseract-OCR";
public String recognizeText(File imageFile,String imageFormat) {
File tempImage = ateImage(imageFile,imageFormat);
File outputFile = new ParentFile(),"output" + Name());
StringBuffer sb = new StringBuffer();
List<String> cmd = new ArrayList<String>();
cmd.add(tessPath+"//tesseract");
cmd.add("");
cmd.Name());
cmd.add(LANG_OPTION);
cmd.add("eng");
ProcessBuilder pb = new ProcessBuilder();
pb.ParentFile());
cmd.set(1, Name());
pbmand(cmd);
Process process = null;
BufferedReader in = null;
int wait;
try {
process = pb.start();
// xxx.tif 1 -l eng
wait = process.waitFor();
if(wait == 0){
in = new BufferedReader(new InputStreamReader(new AbsolutePath()+".txt"),"UTF-8"));                String str;
while((str = in.readLine())!=null){
sb.append(str).append(EOL);
}
in.close();
}else{
tempImage.delete();
}
new AbsolutePath()+".txt").delete();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
/
/ TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(in != null){
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
tempImage.delete();
String();
}
}
OCR.java
st.huu;
import java.io.File;
public class TestOCR {
public static void main(String[] args) {
String path = "D://124.jpg";
System.out.println("ORC ");
try {
String valCode = new OCR().recognizeText(new File(path), "jpeg");
System.out.println(valCode);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ORC ");
}
}
TestOCR.java
st.huu;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import org.apache.jmeter.processor.PostProcessor;
import org.apache.jmeter.samplers.SampleResult;
import org.stelement.AbstractScopedTestElement;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
public class VcodeExtractor extends AbstractScopedTestElement implements PostProcessor, Serializable{ /**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
private static final Logger log = LoggerForClass();
@Override
public void process() {
// TODO Auto-generated method stub
JMeterContext context = getThreadContext();
SampleResult previousResult = PreviousResult();
if (previousResult == null) {
return;
}
log.debug("VcodeExtractor processing result");
String status = ResponseCode();
int id = ThreadNum();
//        String imageName = id + ".jpg";
String path = "D://" + id + ".jpg";
if(status.equals("200")){
byte[] buffer = ResponseData();
FileOutputStream out = null;
File file = null;
file = new File(path);
out = new FileOutputStream(file);
out.write(buffer);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
String vcode = new OCR().recognizeText(file, "jpeg");
vcode = place(" ", "").trim();
JMeterVariables var = Variables();
var.put("vcode", vcode);
//                var.put("vuser", String.valueOf(id));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
VodeExtractor.java
st.huu;
import org.apache.jmeter.processor.gui.AbstractPostProcessorGui; import org.stelement.TestElement;
public class VcodeExtractorGUI extends AbstractPostProcessorGui{ /**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
@Override
public TestElement createTestElement() {
// TODO Auto-generated method stub
VcodeExtractor extractor = new VcodeExtractor();
modifyTestElement(extractor);
return extractor;
}
@Override
public String getLabelResource() {
// TODO Auto-generated method stub
Class().getName();
}
@Override
public String getStaticLabel() {//设置显⽰名称
// TODO Auto-generated method stub
return "VcodeExtractor";
}
@Override
public void modifyTestElement(TestElement extractor) {
// TODO Auto-generated method stub
}
}
VcodeExtractorGUI.java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。