简体   繁体   中英

Unable to make code work in Jmeter´s Beanshell PreProcessor

I´m doing some web scraping using Apache Jmeter´s built-in WebDriver Sampler (with Javascript).

Before the test starts, I need to execute a certain amount of functions in order to build a specific user ID that matches some frontend validations.

In order to do so, I´m declaring and using those functions in each script block provided by the WebDriver sampler.

Since, those functions I need to run do only need to be executed once in the entire test plan (just as it starts), I was wondering if there was a way to use those functions in a BeanShell PreProcessor. Since my code works but was originally made on Javascript, I tried translating it to Java in order to be able to use them in Beanshell.

I keep getting the following error when I run the test:

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

My original javascript code is the following (this one works when used at WebDriver Sampler´s code block):

    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);

And here is the same code translated into java and implemented in BeanShell PreProcessor (the one that´s failing):

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);

Side Note : You´ll notice the translated code isn´t exactly the same as its original javascript counterpart. Those are some changes I made because I can´t access the string's length in Java, like i did in javascript.

Also, please excuse me for my rusty english, it´s not my mother tongue.

Thank you all in advance!

First of all are you aware or RandomStringUtils class which comes with JMeter?

If you still want to re-invent the wheel be informed that since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so consider switching to the JSR223 PreProcessor and the code like:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM