多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

基于java的selenium之验证码识别-自动化测试

更新时间:2023-01-29 09:30:54 作者:多测师 浏览:82

  /*准备工具:

  * 1.下载安装tesseract:http://sourceforge.net/projects/tesseract-ocr/

  * 2.将tesseract.exe命令保存为bat文件,bat内容为

  * @echo off

  tesseract.exe yzm.png 1 -l

  exit

  *获取验证码步骤:

  *1、保存动态页面中的图片文件到本地;

  download(URL地址, "code.png");

  2、java运行tesseract批量处理bat文件,产生TXT文件保存到本地;

  String batPath = "cmd /c start G:\\uxin1\\selenium_xin\\code.bat";

  runTesseractBat(String batPath)

基于java的selenium之验证码识别-自动化测试

  3、读取TXT文件;

  String filepath="G:/uxin1/selenium_xin/code.txt";

  readTextFile(filePath)

  */

  public class analysisIdentifyingCode {

  /*

  * 根据url下载验证码图片并保存到指定的地址

  * @strUrl 图片下载地址

  * @strPath 保存路径

  */

  public void download(String strUrl,String strPath) {

  FileOutputStream utStream = null;

  try {

  //打开链接

  URL url = new URL(strUrl);

  HttpURLConnection conn = (HttpURLConnection) url.openConnection();

  conn.setConnectTimeout(5 * 1000);

  //通过输入流获取图片数据

  InputStream inStream = conn.getInputStream();

  //得到图片的二进制数据,以二进制封装得到数据,具有通用性

  byte[] data =readInputStream(inStream);

  //new一个文件对象用来保存图片,默认保存当前工程根目录

  File imageFile = new File(strPath);

  //创建输出流

  outStream = new FileOutputStream(imageFile);

  //写入数据

  outStream.write(data);

  //关闭输出流

  outStream.close();

  } catch (Exception e) {

  e.printStackTrace();

  } finally {

  try {

  if (outStream != null) {

  outStream.close();

  }

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  }

  /*

  * java运行tesseract批量处理bat文件,产生TXT文件保存到本地

  * @batPath batPath 存放的二进制图片路径

  *

  */

  public void runTesseractBat(String batPath) throws InterruptedException{

  String cmd = batPath;

  Thread.sleep(2000);

  try {

  Runtime.getRuntime().exec(cmd); }

  catch (IOException e) {

  // TODO Auto-generated catch block

  Log.error("运行tesseract批量处理bat文件异常", e);

  }

  }

  /*

  * 读取得到的txt文档读取获取到的文本内容

  * @batPath batPath 存放的二进制图片路径

  *

  */

  public static String readTextFile(String filePath) {

  String IdentificateTxt = null;

  String lineTxt = null;

  try {

  String encoding = "GBK";

  File file = new File(filePath);

  if (file.isFile() && file.exists()) { // 判断文件是否存在

  InputStreamReader read = new InputStreamReader(

  new FileInputStream(file), encoding);// 考虑到编码格式

  BufferedReader bufferedReader = new BufferedReader(read);

  while ((lineTxt = bufferedReader.readLine()) != null) {

  Log.info(lineTxt);

  IdentificateTxt = lineTxt;

  }

  read.close();

  } else {

  Log.error("----找不到指定的文件");

  } } catch (Exception e) {

  Log.error("读取文件内容出错",e);

  }

  return IdentificateTxt;

  }

  /*

  * 读取的文件流

  * @batPath batPath 存放的二进制图片路径

  *

  */

  public static byte[] readInputStream(InputStream inStream) throws Exception{

  ByteArrayOutputStream utStream = new ByteArrayOutputStream();

  //创建一个Buffer字符串

  byte[] buffer = new byte[1024];

  //每次读取的字符串长度,如果为-1,代表全部读取完毕

  int len = 0;

  //使用一个输入流从buffer里把数据读取出来

  while( (len=inStream.read(buffer)) != -1 ){

  //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度

  outStream.write(buffer, 0, len);

  }

  //关闭输入流

  inStream.close();

  //把outStream里的数据写入内存

  return outStream.toByteArray();

  }

  }

  以上内容为大家介绍了自动化测试中基于java的selenium之验证码识别,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部