0

I am working on a project where hkstd id generates and this id used by further HTTP Request Sampler. This hkstd id is generated by java code which developer has provided. My scenario is that I have to call first this java code which will return the id and then i will use this in my HTTP request. I have tried to call this Java code in Beanshell Sampler and in JSR223 Sampler but no result though this sampler passes the result without any error. When I try to put log/System.out then it prints nothing. I am not sure whether my code is running/calling or not ?

Thanks in advance!

JMeter Sampler for this java code

Prateek
  • 77
  • 2
  • 7

2 Answers2

1

Remove class and main method and just enter your code, e.g.:

import org.apache.commons.codec.digest.DigestUtils;

String contextRoot = "root";
String csrfToken = "a";
String url = "http://www.google.com/root?ta=b&_hkstd=1234c=d";

String urlWithQueryString = removeParamsAndHost(url, contextRoot);
String token = getHashValueOfUrl(urlWithQueryString, csrfToken);
String urlWithToken = findurlWithToken(url, token);

private static String findurlWithToken(String url, String token) {
    StringBuilder builder = new StringBuilder(removeParams(url));
    if (builder.indexOf("?") == -1) {
        builder.append("?");
    } else {
        builder.append("&");
    }
    builder.append("_hkstd").append("=").append(token);
    ;
    return builder.toString();

}

private static String removeParamsAndHost(String url, String contextRoot) {
    if (url.indexOf("_hkstd") != -1) {
        return url.substring(url.indexOf(contextRoot), url.indexOf("_hkstd") - 1);
    } else {
        return url.substring(url.indexOf(contextRoot));
    }
}

private static String removeParams(String url) {
    if (url.indexOf("_hkstd") != -1) {
        return url.substring(0, url.indexOf("_hkstd") - 1);
    } else {
        return url;
    }
}

public static String getHashValueOfUrl(String url, String csrfToken) {
    return DigestUtils.md5Hex(url + csrfToken);
}

You can add a JMeter variable you can use later using vars.put:

vars.put("url", urlWithToken );
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

If you want to run this Java class as it is:

  1. Compile the source code provided by the developer and put the resulting .jar file under JMeter Classpath
  2. Restart JMeter to pick the .jar up
  3. Add JSR223 Sampler to your Test Plan
  4. Put the following code into "Script" area:

    AppendHtstd.main()
    
  5. That's it, your class will be executed by JMeter

    enter image description here

See Apache Groovy - Why and How You Should Use It article to learn more about custom scripting in JMeter tests

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks Dmitri! There is one constraint here as I have pass some values to the Java code also like: Cookies/URL/contextRoot here: String contextRoot = "root"; String csrfToken = ; String url = "url"; Is this possible to pass the values here at runtime by following your approach? – Prateek Mar 04 '19 at 09:27
  • You can amend your Java code to read the values from [`args`](https://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java) and return the generated string – Dmitri T Mar 04 '19 at 09:32
  • Sorry for the kind of silly question but from where I will pass the parameters to this file after modification? One more doubt do I need to call like this: var hkstd= AppendHtstd.main() so that return string stored in the variable. – Prateek Mar 04 '19 at 10:05