[英]Unable to make code work in Jmeter´s Beanshell PreProcessor
我正在使用 Apache Jmeter 的內置 WebDriver Sampler(使用 Javascript)進行一些 web 抓取。
在測試開始之前,我需要執行一定數量的函數來構建一個匹配某些前端驗證的特定用戶 ID。
為此,我在 WebDriver 采樣器提供的每個腳本塊中聲明並使用這些函數。
因為,我需要運行的那些函數只需要在整個測試計划中執行一次(就像它開始時一樣),我想知道是否有辦法在 BeanShell 預處理器中使用這些函數。 由於我的代碼有效但最初是在 Javascript 上制作的,所以我嘗試將其翻譯成 Java 以便能夠在 Beanshell 中使用它們。
運行測試時,我不斷收到以下錯誤:
ERROR o.a.j.u.BeanShellInterpreter:
Error invoking bsh method: eval Sourced file: inline evaluation of: ``//String DICTIONARY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String[] DICTIONARY = {"A"," . . . '' : Typed variable declaration : Typed variable declaration : Typed variable declaration
我原來的 javascript 代碼如下(這個代碼在 WebDriver Sampler 的代碼塊中使用時有效):
var DICTIONARY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
function generateRandomString(length, dictionary) {
var result = '';
for ( var i = 0; i < length; i++ ) {
result += dictionary.charAt(Math.floor(Math.random() * dictionary.length));
}
return result;
}
function countChars(character, sentence){
var count = 0
for(var i=0; i<sentence.length; i++) {
if (sentence[i] === character) count++
}
return count
}
function hasConsecutivesChars(sentence, length) {
var charCounter = {}
for (var i = 0; i < sentence.length; i++) {
var currentChar = sentence[i]
if (countChars(currentChar, sentence) >= length) {
return true
}
}
return false
}
function generateRandomStringWithoutConsecutivesChars(length, maxConsecutiveChars, dictionary, maxTries) {
var isValidString = false
var randomString = ''
var tries = 0;
while(!isValidString && tries <= maxTries) {
randomString = generateRandomString(length, dictionary)
var isValidString = !hasConsecutivesChars(randomString, maxConsecutiveChars)
tries++;
}
return randomString
}
function randomIntFromInterval(min, max) { // mínimo y máximo incluidos
return Math.floor(Math.random() * (max - min + 1) + min)
}
var nombreUsuario = "SELENIUM_" + generateRandomStringWithoutConsecutivesChars(5, 3, DICTIONARY, 100) + randomIntFromInterval(1, 9);
下面是翻譯成 java 並在 BeanShell 預處理器(失敗的那個)中實現的相同代碼:
String[] DICTIONARY = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
String generateRandomString(int length, String[] dictionary) {
String result = "";
for ( int i = 0; i < length; i++ ) {
//result += dictionary.charAt(Math.floor(Math.random() * dictionary.length));
result += dictionary[Math.floor(Math.random() * 26)];
}
return result;
}
int countChars(String character, String sentence){
int count = 0;
for(int i=0; i<5; i++) {
//if (sentence[i] == character){
if (sentence.charAt(i) == character){
count++;
}
}
return count;
}
boolean hasConsecutivesChars(String sentence, int length) {
for (int i = 0; i < 5; i++) {
String currentChar = sentence.charAt(i);
if (countChars(currentChar, sentence) >= length) {
return true;
}
}
return false;
}
String generateRandomStringWithoutConsecutivesChars(int length, int maxConsecutiveChars, String[] dictionary, int maxTries) {
boolean isValidString = false;
String randomString = "";
int tries = 0;
while(!isValidString && tries <= maxTries) {
String randomString = generateRandomString(length, dictionary);
boolean isValidString = !hasConsecutivesChars(randomString, maxConsecutiveChars);
tries++;
}
return randomString;
}
int randomIntFromInterval(int min, int max) { // mínimo y máximo incluidos
return Math.floor(Math.random() * (max - min + 1) + min);
}
String id = generateRandomStringWithoutConsecutivesChars(5, 3, DICTIONARY, 100) + randomIntFromInterval(1, 9);
vars.put("id", id);
log.info(id);
旁注:您會注意到翻譯后的代碼與其原始 javascript 對應的代碼完全相同。 這些是我所做的一些更改,因為我無法訪問 Java 中的字符串長度,就像我在 javascript 中所做的那樣。
另外,請原諒我生疏的英語,這不是我的母語。
謝謝大家!
首先,您知道JMeter附帶的 RandomStringUtils class 嗎?
如果您仍然想重新發明輪子,請注意,自 JMeter 3.1 以來,您應該使用 JSR223 測試元素和 Groovy 語言編寫腳本,因此請考慮切換到JSR223 預處理器和如下代碼:
String[] DICTIONARY = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
String generateRandomString(int length, String[] dictionary) {
String result = "";
for (int i = 0; i < length; i++) {
//result += dictionary.charAt(Math.floor(Math.random() * dictionary.length));
result += dictionary[Math.floor(Math.random() * 26) as int];
}
return result;
}
int countChars(String character, String sentence) {
int count = 0;
for (int i = 0; i < 5; i++) {
//if (sentence[i] == character){
if (sentence.charAt(i) == character as char) {
count++;
}
}
return count;
}
boolean hasConsecutivesChars(String sentence, int length) {
for (int i = 0; i < 5; i++) {
String currentChar = sentence.charAt(i);
if (countChars(currentChar, sentence) >= length) {
return true;
}
}
return false;
}
String generateRandomStringWithoutConsecutivesChars(int length, int maxConsecutiveChars, String[] dictionary, int maxTries) {
boolean isValidString = false;
String randomString = "";
int tries = 0;
while (!isValidString && tries <= maxTries) {
randomString = generateRandomString(length, dictionary);
isValidString = !hasConsecutivesChars(randomString, maxConsecutiveChars);
tries++;
}
return randomString;
}
int randomIntFromInterval(int min, int max) { // mínimo y máximo incluidos
return Math.floor(Math.random() * (max - min + 1) + min);
}
String id = generateRandomStringWithoutConsecutivesChars(5, 3, DICTIONARY, 100) + randomIntFromInterval(1, 9);
vars.put("id", id);
log.info(id);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.