Request in JSR223 Sampler works correctly, but here are Sampler result and Response data with 0 and empty values:
Metrics in Sample result in red rectangle are equal to 0.
No response data (both headers and body) are available to see.
Is it possible to get somehow real numbers instead of 0 0 0 0 0 0 for metrics in Sampler result tab and to have visible response in Response data tab?
JSR223 Sampler code:
As per reference documentation, you can call those methods:
SampleResult.connectEnd();
SampleResult.latencyEnd();
SampleResult.sampleEnd();
SampleResult.setResponseData(driver.getPageSource(), "UTF-8");
You can use SampleResult javadocs to see all method or subclasses or custom class of yours.
Note SampleResult is a variable name that JMeter binds into element, not the class, so:
don't prefix it with org.apache.jmeter.samplers.SampleResult, just use SampleResult
don't import org.apache.jmeter.samplers.SampleResult
Edit (after you added your JSR223 Sampler code):
To fill latency and connect time, use:
SampleResult.setConnectTime()
SampleResult.setLatency()
As you can get values from Webdriver#executeScript() using such code that exploits Timing API:
long pageLoadTime= (Long)js1.executeScript("return (window.performance.timing.loadEventEnd-window.performance.timing.responseStart)");
long latency= (Long)js1.executeScript("return (window.performance.timing.responseStart-window.performance.timing.navigationStart)");
long endtoEndRespTime= (Long)js1.executeScript("return (window.performance.timing.loadEventEnd-window.performance.timing.navigationStart)");
long connectTime= (Long)js1.executeScript("return (window.performance.timing.connectEnd -window.performance.timing.connectStart)");
Connect time and latency - you need to get it from the Navigation Timing API using WebDriver.executeScript() function, once done you can use SampleResult.setConnectTime() and SampleResult.setLatency()
Response can be obtained using WebDriver.getPageSource() function so you should be able to do something like:
SampleResult.setResponseData(driver.getPageSource())
SampleResult.setBytes(driver.getPageSource().length())
Remove import of org.apache.jmeter.samplers.SampleResult as it comes as a pre-defined shortcut
Related
is it possible to wait for XPath or CSS selector to display in Jmeter?
I am using HTTP request to send API call and I have assertion as CSS selector path but due to API slowness , I would like to implement a waitfor method for the specific element in UI.
JMeter doesn't execute any Post-Processors or Assertions or Listeners until it gets response from the server therefore you don't need to do anything. See Execution order user manual chapter for more details.
However if I misunderstood your requirement and you want to repeat a HTTP request until XPath or CSS extractor return the value you're looking for you can put the request under the While Controller and put the condition of your choice there:
In the above case:
On iteration 0 of the While Controller ${myVariable} doesn't have any value (CSS Selector Extractor hasn't been executed yet)
Iterations from 1 to 5 - ${myVariable} has value of foo which doesn't match While Controller's condition so it loops over
Iteration 6 - ${myVariable} value becomes bar and the While Controller exits the loop.
Just in case, textual representation of the jexl3() function used to compare variable with some value:
${__jexl3("${myVariable}" != "bar",)}
Looping test occurrence based on the data count retrieved from the JDBC request and also as input data for the HTTP request
I have test scenario where i need to use the DB output as the input criteria for the HTTP request. Based on the DB output count( from the first request) i need to loop the HTTP request and it data accordingly
I tried the logical Loop Count by passing the count variable from run time as ${TEST_ID_#}, still its not working.
I tried the logical Loop Count by passing the count variable from run time as ${TEST_ID_#}, still its not working.
Debug Sampler Output
You can extract the counter using Post Processor [either Regular Expression Extractor or JSON Extractor etc.]
Once you have extracted that count, now place a Loop controller as a parent of HTTP request.
For example. I am using User Defined Variable for loop Count:
Any reason for using ${TEST_ID_#} variable? If your Debug Sampler screenshot is full and correct you should be using ${KEY_ID_#} instead.
Also it might be a better idea to use ForEach Controller instead of the Loop Controller, the relevant configuration would be something like:
References:
How to Use ForEach Controller in JMeter
Using Regular Expressions in JMeter
How to capture response data into a list and to use those in next thread using BeanShell post processor?
Example: the response data was having:
mobile number:1
mobile number:2
mobile number:3
mobile number:n
I want to capture all mobile numbers and want to use in next thread.
How to do that? Can any one tell?
Using Beanshell for scripting is some form of a performance anti-pattern therefore I'll provide solution using JSR223 Test Elements and Groovy language
Given you have response data like:
mobilenumber:1 mobilenumber:2 mobilenumber:3
Add JSR223 PostProcessor as a child of the request which returns the above data and put the following code into "Script" area:
props.put('list',prev.getResponseDataAsString().split(" ").collect())
The above code will split the response data by spaces and store the result into an ArrayList structure
In the next Thread Group you will be able to access the values like:
def list = props.get('list')
Demo:
I am using two soap/xml request samplers wherein response of one is to be used in request of the other. The issue is that the response of Sampler1 contains multiple occurrences of "a:" which has to be replaced by "eas1:" which can be used in Sampler2. Kindly suggest a solution.
I tried using beanshell postprocessor but could not come to any positive result.
Add JSR223 PostProcessor as a child of the Sampler1
Put the following code into "Script" area
def response = prev.getResponseDataAsString()
def request = response.replaceAll('a:', 'eas1:')
vars.put('request', request)
Use ${request} in the "Body Data" section of the Sampler2
References:
prev is a shorthand to SampleResult class instance which provides access to the parent Sampler result
vars is a shorthand to JMeterVariables class instance, it provides read/write access to JMeter Variables
String.replaceAll() method reference
Groovy is the New Black - guide to Groovy scripting in JMeter
I am trying to use beanshell post processor so I have started with simple http request where I am hitting the google home page. "${url}"=="https://www.google.co.in". After http request page I am adding the BeanShellPostProcessor where in Script area I am using ctx variable and some other code which is like :
int threadNum = ctx.getThreadNum();
String code = prev.getResponseCode();
String message = prev.getResponseMessage();
log.info(threadNum);
log.info("This line has been written by Beanshell Post Processor");
so I have two concerns -
i.The way I am using beanshell is this right ?
ii.Where is the console for beanshell processor in jmeter?
like sample request result can be viewed in listener. I tried with BeanShellListner, but it doesn't show any data.
Also I have kept the "log viewer" on.
You are using it almost right, the only error is that you cannot write an integer to the log file, you need to cast it to String first like:
log.info(String.valueOf(threadNum));
You won't be able to see the result of pre and post processors anywhere apart from jmeter.log file. In case of PostProcessor you can modify parent sampler response data via prev.setResponseData() method where prev is a shorthand to SampleResult class instance
I would also recommend considering switching to JSR223 Elements and Groovy language as this way you will get better performance, out of box support of XML, JSON, etc., and other Groovy's "syntax sugar". See Groovy Is the New Black article for more detailed explanation.