Thursday 2 May 2024

Calling an HTTP Sampler from a JSR223 Sampler in JMeter: A Step-by-Step Guide

 We can call one sampler (HTTP) from another sampler (JSR223) in JMeter. JMeter provides flexibility to customize and control the flow of your test plan using various components, including JSR223 samplers.

To call an HTTP sampler from a JSR223 sampler, you can use the JMeter API within the JSR223 sampler code. Here's an example of how you can achieve this: 1. Add a JSR223 Sampler to your test plan. 2. Choose the appropriate language (e.g., Groovy) for the JSR223 sampler. 3. Write your custom code in the script area of the JSR223 sampler to call the HTTP sampler using the JMeter API. Here's an example: ```groovy script import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; // Get the HTTP sampler by its name def httpSampler = ctx.getCurrentSampler().getThreadContext().getVariables().getObject("HTTPSamplerProxy") // Make sure the HTTP sampler is not null if (httpSampler != null) { // Execute the HTTP sampler def httpResult = httpSampler.sample() // You can access the response code, response message, and other details from the HTTP result int responseCode = httpResult.getResponseCode() String responseMessage = httpResult.getResponseMessage() // Process the HTTP result as needed // ... } ``` In the above example, we obtain the HTTP sampler using the `getObject()` method from the JMeter `ThreadContext` and then execute the HTTP sampler using the `sample()` method. You can access and process the response details according to your requirements. Note: Make sure you have properly configured and added the HTTP sampler to your test plan before using it within the JSR223 sampler. Remember to replace the language-specific code (`groovy` in this example) if you choose a different language for your JSR223 sampler. It's worth noting that calling one sampler from another can affect the concurrency and throughput of your test. Ensure you understand the implications and adjust your test plan accordingly. import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; // Get the HTTP sampler by its name def httpSampler = ctx.getCurrentSampler().getThreadContext().getVariables().getObject("HTTPSamplerProxy") // Make sure the HTTP sampler is not null if (httpSampler != null) { // Execute the HTTP sampler def httpResult = httpSampler.sample() // You can access the response code, response message, and other details from the HTTP result int responseCode = httpResult.getResponseCode() String responseMessage = httpResult.getResponseMessage() // Process the HTTP result as needed // ... }

Wednesday 1 May 2024

Extracting OTP from SOAP Requests in TrueClient Protocol: A JavaScript Approach

In the TrueClient protocol, you can leverage JavaScript to extract specific values from a SOAP request, such as a one-time password (OTP). To achieve this, you can use regular expressions or string manipulation techniques. Here's a general approach:

1. Capture the SOAP Request: Start by capturing the SOAP request as an object in TrueClient.

2. Extract the OTP with JavaScript: Use a JavaScript step to work with the captured SOAP request object. Utilize regular expressions or string manipulation methods like `indexOf()` and `substring()` to extract the OTP.

3. Assign OTP to a Parameter: Once the OTP is extracted, assign it to a parameter in TrueClient so you can use it elsewhere in your script.

Here's an example of how you can extract the OTP using JavaScript:

// Assume soapRequest contains the captured SOAP request
var soapRequest = lr.evalC("YourSoapRequestObjectName.body");

// Define a regular expression pattern to match the OTP
var otpPattern = /OTP: (\d+)/;

// Match the OTP pattern in the SOAP request
var match = soapRequest.match(otpPattern);

if (match && match.length > 1) {
    // Extract the OTP value
    var otp = match[1];
    
    // Assign the OTP value to a parameter in TrueClient
    lr.set("OTP_Parameter", otp);
} else {
    // Handle the case when OTP is not found
    lr.log("OTP not found in SOAP request");
}

Replace `"YourSoapRequestObjectName"` with the name of the object containing the SOAP request in your TrueClient script. Adjust the regular expression pattern (`otpPattern`) according to the format of the OTP in your SOAP request.