Go capturing template variables and names - go

I am experimenting with Go's templates.
I am curious: is it possible to do the following: {{$myVariable := randomStringFunc | saveFunc}}
Where:
$myVariable - randomly chosen variable name
randomStringFunc - function that generates random strings
TRICKY: saveFunc - a function that saves the variable name and its value
I have looked into capture-or-assign-golang-template-output-to-variable but I am not sure how and if it can help me achieve my goal.
EDIT
In the end I would like to have a mapping between the variable name that is defined in the template and the value that is assigned to it:
var variableMapping map[string]string
After template execution the content of variableMapping should be something like:
{
"$myVariable:"randomString1",
"$anotherVariable":"5",
"$thirdVariableInMyTemplate":"false"
}

Related

Fetch value from XML using dynamic tag in ESQL

I have an xml
<family>
<child_one>ROY</child_one>
<child_two>VIC</child_two>
</family>
I want to fetch the value from the XML based on the dynamic tag in ESQL. I have tried like this
SET dynamicTag = 'child_'||num;
SET value = InputRoot.XMLNSC.parent.(XML.Element)dynamicTag;
Here num is the value received from the input it can be one or two. The result should be value = ROY if num is one and value is VIC if num is two.
The chapter ESQL field reference overview describes this use case:
Because the names of the fields appear in the ESQL program, they must be known when the program is written. This limitation can be avoided by using the alternative syntax that uses braces ( { ... } ).
So can change your code like this:
SET value = InputRoot.XMLNSC.parent.(XMLNSC.Element){dynamicTag};
Notice the change of the element type as well, see comment of #kimbert.

Pass Variable inside the another variable in Jmeter

I Have a scenario like need to pass a random variable example ${__Random(1,25,)} in inside one more variable like
Globel variable:
Test = TestResults
${__Random(1,25,)}
Sample Request Data:
${Test(randome variable)}, ${Test(randome variable)},${Test(randome variable)},-------- ${Test(randome variable)}
Sample expected response:
TestResults1,TestResults10,TestResults5,----------- TestResults20
How should I pass this scenario?
Use the V function
${__V(TestResults${__Random(1,25,)})}
V (variable) function returns the result of evaluating a variable name expression. This can be used to evaluate nested variable references
You can replace TestResults with ${yourVarName} if you want to dynamically use additional variable

How can I capitalize one variable and save it as a new variable in sass/scss?

My scss file provides an area where users can change certain variables. For example:
$variant-color-multi: "multicolor";
I would like this variable to be capitalized and saved to a new variable. For example:
$variant-color-multi-capitalized: "Multicolor";
That way if the value of $variant-color-multi is changed to "bunt" for example, then $variant-color-multi-capitalized would automatically output "Bunt".
I tried using the string functions below to manipulate my original variable, but that wasn't working. I'm a newbie to sass.
$variant-color-multi-first: slice($variant-color-multi, 1, 1);
$variant-color-multi-first-captial: to-upper-case($variant-color-multi-first);
$variant-color-multi-rest: slice($variant-color-multi, 2, -1);
$variant-color-multi-capitalized: $variant-color-multi-first-captial + $variant-color-multi-rest;

Generate and store random variables

I am trying to create some dynamic user defined variables, use them within http requests in JMETER and also save them to file. Basically I'm testing the creation of accounts and would like to save the accounts I've created.
The problem is when I use User Defined Variables and then set the values as below, it only generated the random strings once and in subsequent loops it uses the same data and fails as email already exists:
FIRSTNAME1 Bob${__RandomString(10,abcdefghijklmnopqrstuvwxyz,)}
LASTNAME1 Surname${__RandomString(10,abcdefghijklmnopqrstuvwxyz,)}
EMAIL1 Bob${__RandomString(10,abcdefghijklmnopqrstuvwxyz,)}#emailaddres.com
To save this to file I use:
name1 = vars.get("EMAIL1");
name2 = vars.get("FIRSTNAME1");
name3 = vars.get("LASTNAME1");
f = new FileOutputStream("C://test/Register_new_user_Jmeter.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
p.println(name1 + "," + name2 + "," + name3);
f.close(
How do I set this up so I can generate random strings, use them to create new accounts and also save the info to file? Thanks
Yes, User Defined Variables are used for defining once (static) variables, use other component, especially User Parameters for dynamic values.
If a runtime element such as a User Parameters Pre-Processor or Regular Expression Extractor defines a variable with the same name as one of the UDV variables, then this will replace the initial value, and all other test elements in the thread will see the updated value.
User Parameters are creating variables same as User Defined Variable, but can override pervious values
If there are more threads than values, the values get re-used. For example, this can be used to assign a distinct user id to be used by each thread. User variables can be referenced in any field of any JMeter Component.

Passing two variables in a ForEach controller in jmeter

I am running a ForEach controller in which I want the controller to run the service underneath it for changes in sets of latitudes and longitudes. example-
Input variable prefix: latitude
Output variable name: Latitude
I want to run the controller for changes in both "latitude" and "longitude". I tried doing this-
Input variable prefix:latitude, longitude
but it does not work. Is there any other way to pass two variables in ForEach controller?
Unfortunately you cannot do it using ForEach Controller, but you can work it around using __V() and __counter() function combination.
For example you have 4 JMeter Variables:
latitude_1=40.7128° N
longitude_1=74.0059° W
latitude_2=32.0853° N
longitude_2=34.7818° E
And you want to iterate them both using ForEach Controller. In that case the relevant configuration would be:
Input variable prefix: latitude
Output variable name: anything meaningful, i.e. current_latitude
You can refer matching longitude value using the following expression:
${__V(longitude_${__counter(,)})}
Demo:
See Here’s What to Do to Combine Multiple JMeter Variables for detailed explanation of where did the above expression come from.
I encountered same problem and google a lot to find the best possible solution but
did not get success. I am sharing my work-around (and I am not saying it's a great solution ) -
Mention your Input variable name (ex - inputVar) as well as Output variable name (outputVar) on ForEach Controller
(lets say http://website.com/folder_A/folder_B/123_latitude/456_longitude) and you like to take care these two : latitue and longtitude
Use regular expression extractor :
Name of created variable : inputVar
Expression : http://website.com/folder_A/folder_B/(.*)
Template : $1$
match No : -1 // (yes -1 , this will give you complete group)
Handle with regular expression (just search it out )
so for above example : that would be -
http://website.com/folder_A/folder_B/${outputVar}
http://website.com/folder_A/folder_B/${outputVar} URL will be able to print / give you latitue and longtitude in your response code

Resources