I want create a property from a variable. The variables was created by calling a variable from a xpath extraction, then using a substring to then get the last 4 characters. The substring string value is saved to a variable, then set to a property.
When I run the script, the log.info(vars.get("lastcard")); returns the value of the variable. However it then fails to save to a property, because when that property is called(${__property(lastNum)} it will display - ${lastcard}
import org.apache.jmeter.util.JMeterUtils;
import org.apache.commons.lang3;
String tesTe = vars.get("card");
String last4 = tesTe.substring(tesTe.length()-4,tesTe.length());
vars.put("lastcard", String.valueOf(last4));
log.info(vars.get("lastcard"));
${__setProperty(lastNum,${lastcard})};
Any ideas as to what is going on
You should read user manual about scripting:
ensure the script does not use any variable using ${varName}
You should use JSR223 variables vars and props to handle variables and properties. In your case change last line to:
props.put("lastNum", vars.get("lastcard"));
Also you can set variable in shorter way:
vars.put("lastcard", vars.get("card").substring(tesTe.length()-4));
There was 2 changes that need to be made to resolve the issues.
import org.apache.jmeter.util.JMeterUtils;
import org.apache.commons.lang3;
String tesTe = vars.get("card");
String last4 = tesTe.substring(tesTe.length()-4,tesTe.length());
vars.put("lastcard", last4); //Already string therefore no need to use String.valueOf()
log.info(vars.get("lastcard"));
props.put("lastNum",vars.get("lastcard")); //Setup to use props.put instead of set property
Related
I am running a query via JDBC request and I am able to get the data and place it in a variable array. The problem is I want the values of the variables to be saved to a text file. However, each variable is being given a unique number appended to it i.e. SCORED_1, SCORED_2,SCORED_3 etc. I am using a beanshell post processor to write to the text file. The problem is I unless I define A LINE Number. How can I get all results from a SQL query and dump them into a single variable without the variables separated by brackets and line separated on their own row.
import org.apache.jmeter.services.FileServer;
// get variables from regular expression extractor
ClaimId = vars.get("SCORED _9"); // I want to just use the
SCORED variable to contain all values from the array
without "{[" characters.
// pass true if want to append to existing file
// if want to overwrite, then don't pass the second
argument
FileWriter fstream = new FileWriter("C:/JMeter/apache-
jmeter-4.0/bin/FBCS_Verify_Final/Comp.txt", true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(ClaimId);
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();
We are not telepathic enough to come up with the solution without seeing your query output and the result file format.
However I'm under impression that you're going into wrong direction. Given you're talking about {[ characters it appears that you're using Result Variable Name field
which returns an ArrayList which should be treated differently
However if you switch to Variable Names field
JMeter will generate a separate variable per each result set row and it should be much easier to work with and eventually concatenate
More information:
JDBC Request
Debugging JDBC Sampler Results in JMeter
JDBC request>Enter a Variable Name> Store as string>Add a Beanshell PostProcessor and add the following script.
import org.apache.jmeter.services.FileServer;
{
FileWriter fstream = new FileWriter("C:/JMeter/apache-jmeter-4.0/bin/FBCS_Verify_Final/Comp.txt", false);
BufferedWriter out = new BufferedWriter(fstream);
Count = vars.get("SCORED_#");
Counter=Integer.parseInt(vars.get("SCORED_#"));
for (int i=1;i<=Counter;i++)
{
ClaimId = vars.get("SCORED_"+i);
out.write(ClaimId);
out.write(System.getProperty("line.separator"));
}
out.flush();
out.close();
fstream.close();
}
I have JMX structure such as,
ThreadGroup-1
Set propertyVariable-Content_0,Content_1,Content_3 etc
ThreadGroup-2
LoopController
Counter-counterNum
HTTPRequest--use PropertyValue as ${__V(__P(Content)_${CounterNum})}
above variable is not fetching value.
Got stuck on how to use __V for this case as it has property variable.
can someone explain me how to use __V when we have property variable.
You don't need __V() function here, you can access property value using __P() function directly like:
${__P(Content_${CounterNum},)}
Demo:
In case of malfunction double check that you're really setting the properties values using Debug Sampler and View Results Tree listener combination
you need to use this
${__V(MainVriableName_${CounterNo})}
1) Add Loop Controller
2) Under Loop Counter -> Add Counter (Starting value= 1, Increment = 1, Maximun value = ${MainVriableName_matchNr} and Exported Variable Name = CounterNo
3) After in the below Request(s) use ${__V(MainVriableName_${CounterNo})}
this works fine.
Problem with usage __base64Encode function to encode Random Variable.
I use Random Variable to generate unique email for user. (in each iteration of test scenario)
I want to use :
${__base64Encode(${randomvar})} which generate encoded ${randomvar}
Real example user defined variable 'babretr' defined below:
babretr=${__base64Encode(${randomMail}${timeStamp}#jmeter.soi:Kazek2017#)}
after base64 encode looks:
babretr=JHtyYW5kb21NYWlsfTIwMTcwNTE5MDg0MTI0NDcyWkBqbWV0ZXIuc29pOkthemVrMjAxN0A=
after base64 decode looks:
${randomMail}20170519084124472Z#jmeter.soi:Kazek2017#
Jmeter Random Variable ${randomMail} is not encoded.
With different variables problem does not exist.
You could add Beanshell PreProcessor on your sampler and put something like:
import java.util.Base64;
import org.apache.commons.lang3.RandomStringUtils;
String sufix = "#jmeter.soi:Kazek2017#";
String address = Base64.getEncoder().encodeToString(new String(
RandomStringUtils.random(10) + vars.get("timeStamp") + sufix).getBytes());
vars.put("encodedAddress", address);
After that, just use ${encodedAddress} in your sampler.
I have a YAML file that uses the encoding __firstname__ as a placeholder which signifies that an existing method firstname should be used, rather than the literal string in a subsequent process.
I am trying to understand the most ruby way to to do this. Basically, I need to extract the part between the underscores and send it to an object. Here is pseudocode:
variable = '__firstname__'
if variable is prefixed and suffixed with underscores
result = object.send(variable.removeunderscores)
else
result = variable
end
puts result
I was about to write this procedurally like this, but this is the type of thing that I think ruby can less clunkily if only I knew the language better.
What is a clean why to write this?
There's nothing wrong with verbose code if it's clear to read IMO.
I'd do something like this using String#start_with? and String#end_with?:
variable = '__firstname__'
if variable.start_with?("__") && variable.end_with?("__")
result = object.send(variable[2...-2])
else
result = variable
end
In JMeter I added the configuration for oracle server. Then I added a JDBC request object and put the ResultSet variable name to status.
The test executes fine and result is displayed in treeview listener.
I want to use the variable status and compare it with string but jmeter is throwing error about casting arraylist to string.
How to retrieve this variable and compare with string in While Controller?
Just used some time to figure this out and think the accepted answer is slightly incorrect as the JDBC request sampler has two types of result variables.
The ones you specify in the Variable names box map to individual columns returned by your query and these you can access by saying columnVariable_{index}.
The one you specify in the Result variable name contains the entire result set and in practice this is a list of maps to values. The above syntax will obviously not work in this case.
The ResultSet variable returned with JDBC request in JMeter are in the for of array. So if you want to use variable status, you will have to use it with index. If you want to use the first(or only) record user status_1. So you need to use it like status_{index}.
String host = vars.getObject("status").get(0).get("option_value");
print(host);
log.info("----- " + host);
Form complete infromation read the "yellow box" in this link:
http://jmeter.apache.org/usermanual/component_reference.html#JDBC_Request
Other util example:
http://jmeter.apache.org/usermanual/build-db-test-plan.html
You can use Beanshell/Groovy (same code works) in JSR233 PostProcessor to work with “Result Variable Name” from JDBC Request like this:
ArrayList results = vars.getObject("status");
for (HashMap row: results){
Iterator it = row.entrySet().iterator();
while (it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
log.info(pair.getKey() + "=" + pair.getValue());
}
}
Instead of output to log replace with adding to string with delimiters of your choice.