1. 程式人生 > 其它 >Jmeter驗證碼圖片登入

Jmeter驗證碼圖片登入

Jmeter使用test4j進行圖片驗證碼識別、登入

1. 下載test4j

https://sourceforge.net/projects/tess4j/

2. 修改Jmeter外部依賴資料夾

(1)修改D:\Applications\apache-jmeter-5.4.1-3\bin目錄下jmeter.properties檔案

plugin_dependency_paths=../dependencies/lib;

 (2)test4j壓縮包解壓,進行以下操作:

    ①將XXXX\Tess4J\lib目錄下所有所有檔案複製到D:\Applications\apache-jmeter-5.4.1-3\dependencies\lib目錄下;

    ②將XXXX\Tess4J\dist目錄下所有檔案複製到D:\Applications\apache-jmeter-5.4.1-3\dependencies\lib目錄下。

3. 編寫Jmeter及指令碼

(1)建立執行緒組

(2)新增變數

(3)新增cookie

(4)新增獲取驗證碼請求並識別驗證碼

①新增獲取驗證碼請求

 ②新增檢視結果樹並執行檢視請求結果

③新增BeanShell PostProcessor,並編寫指令碼識別驗證碼圖片內容

import java.io.*;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract; import java.text.SimpleDateFormat; import java.util.Date; // 設定日期格式,如2022-05-17-12-08-11-548 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS"); // 將驗證碼儲存到本地檔案中 String imgPath="D:/captcha/" + df.format(new Date()) + "-captcha.jpg"; byte[] responseBody = prev.getResponseData(); File imageFile
= new File(imgPath); OutputStream out = new FileOutputStream(imageFile); out.write(responseBody); out.flush(); out.close(); // 讀取圖片數字 File imageFile1 = new File(imgPath); ITesseract instance = new Tesseract(); // Tess4j 解壓檔案裡的 tessdata 目錄下 instance.setDatapath("D:/__Download/Google Download/Tess4J/tessdata"); // 英文庫識別數字比較準確 instance.setLanguage("eng"); captcha = instance.doOCR(imageFile1).replace("\n", ""); log.info("this captcha is : " + captcha); vars.put("captcha",captcha);

④驗證識別的結果是否正確

 ⑤新增登入請求

認真核對每一項值

 ⑥新增執行緒組的檢視結果樹

⑦選擇中執行緒組檢視結果樹,執行並檢視結果

 (5)至此Jmeter驗證碼登入完成,以下是我下載並配置好的環境,有需要可阿里雲盤自取

使用解壓檔案解壓即可

https://www.aliyundrive.com/s/MjbifMZqMps
「Jmeter識別驗證碼登入程式包.exe」https://www.aliyundrive.com/s/MjbifMZqMps 點選連結儲存,或者複製本段內容,開啟「阿里雲盤」APP ,無需下載極速線上檢視,視訊原畫倍速播放。