Access Group Variables Values Based on the Dynamic String Returned - yaml

I have PythonScript#0 task in YAML file of the pipeline which is getting the email ID based on the Invidiual CI Requestor. & I have a secret variable value stored in the variable groups with the key of same email Id as CI Requestor.
VARIABLE GROUP:
key[myemail#email.com] = value["SECURETOKEN"]
# Getting the email ID of CI requestor
email = '$(Build.RequestedForEmail)'
group_variable = f"$({email})"
print(group_variable )
# Above variable is printing the value as "$(myemail#email.com)", which I do no expect.
I have tried all string concatenation methods to get the value. such as.
group_variable = f"$({email})"
group_variable = "$({})".format(email)
group_variable = "$({0})".format(email)
I want to get the value as "SECURETOKEN" instead of "$(myemail#email.com)". Any sort of help is appreciated. Thanks.

You cannot retrieve the value of a secret value from the API. It will always be empty. In order for the Python task to access the secret it must be passed in explicitly as an environment variable or an input parameter.

Related

How to read values from a database like a csv file in jmeter

I would like to read values from a select query and use the values sequentially in a API call.
Since the database values get stored as var_1, var_2 ... var_N
So how do i increment the number for the variable?
I used a counter and pre processor to increment the number in variable
vars.put ("email", "email"+"_"+vars.get("counter"))
But the final variable is not getting replaced by the value from the select query
eg select query result from a debug sampler
email_1=hata.pd.h13u#yopmail.com
email_2=hata.pd.h13u#yopmail.com
email_3=hataiot.test13#mailinator.com
email_4=hataiot.test12#mailinator.com
--combining the variable and counter:
vars.put ("email", "email"+"_"+vars.get("counter"))
--using the variable in the API post body
{
"username":"${email}",
"password":"test1234"
}
Actual result:
POST data:
{
"username":"email_1",
"password":"test1234"
}
Expected result:
{
"username":"hata.pd.h13u#yopmail.com",
"password":"test1234"
}
TIA
${__V(email_${counter})}
Try this one, see also the documentation: http://jmeter.apache.org/usermanual/functions.html#what_can_do
Note that variables cannot currently be nested; i.e. ${Var${N}} does not work. The __V (variable) function can be used to do this: ${__V(Var${N})}. You can also use ${__BeanShell(vars.get("Var${N}")}.
Have you considered using ForEach Controller? It's super-handy for iterating the variables from extractors or JDBC test elements.
If you still want to continue with the current approach you need to change this line:
vars.put ("email", "email"+"_"+vars.get("counter"))
to this one:
vars.put ("email", vars.get("email"+"_"+vars.get("counter")))
Because you're putting into email variable stuff like email_1, email_2, etc. instead of actual ${email_1} variable value.
References:
JMeter Functions and Variables
How to Retrieve Database Data for API Testing with JMeter

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.

Variable appears to be lost when setting a property in jmeter

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

Putting Faker Gem Values to a Hash

I'm writing automated tests using Cucumber, Capybara, WebDriver, SitePrism, and Faker. I am new to this and need some help.
I have the following steps..
Given (/^I have created a new active product$/) do
#page = AdminProductCreationPage.new
#page.should be_displayed
#page.producttitle.set Faker::Name.title
#page.product_sku.set Faker::Number.number(8)
click #page.product_save
#page.should have_growl text: 'Save Successful.'
end
When (/^I navigate to that product detail page) do
pending
end
Then (/^All the data should match that which was initially entered$/) do
pending
end
In config/env_config.rb I have set up an empty hash...
Before do
# Empty container for easily sharing data between step definitions
#verify = {}
end
Now I want to hash the value generated by Faker in the Given step so I can validate it saved properly in the When step. I also want to enter the value generated by faker in the script below into a search field.
#page.producttitle.set Faker::Name.title
How do I push the values generated by faker to the #verify has?
How do I pull that value and insert it into a text field?
How do I pull that value to verify the save value equals the value generated by faker?
1. How do I push the values generated by faker to the #verify has?
A hash is simply a dictionary of key-value pairs, which you can set with hash[key] = value.
The key can be a string #verify['new_product_name'] = Faker::Name.title
The key can also be a symbol #verify[:new_product_name] = Faker::Name.title
Since the value you generate may be used multiple times within the step definition (once for storing it in the #verify hash, and once for setting the field value) I personally prefer to first store it in a local variable, and reference that where needed.
new_product_title = Faker::Name.title
#verify[:new_product_title] = new_product_title
2. How do I pull that value and insert it into a text field?
You can reference values by their key. So after you have stored the value in the hash, you could do this
#page.producttitle.set #verify[:new_product_name]
Or if you stored it in a local variable as suggested above, you would just do this
#page.producttitle.set new_product_name
3. How do I pull that value to verify the save value equals the value generated by faker?
Similarly, you can assert that a field value equals what you've stored in the hash. An example would be #page.producttitle.value.should == #verify[:new_product_name]
Putting this all together:
Given (/^I have created a new active product$/) do
#page = AdminProductCreationPage.new
#page.should be_displayed
# Create a new title
new_product_title = Faker::Name.title
# Store the title in the hash for verification
#verify[:new_product_title] = new_product_title
# Set the input value to our new title
#page.producttitle.set new_product_title
#page.product_sku.set Faker::Number.number(8)
click #page.product_save
#page.should have_growl text: 'Save Successful.'
end
When (/^I navigate to that product detail page) do
pending
end
Then (/^All the data should match that which was initially entered$/) do
#page.producttitle.value.should == #verify[:new_product_title]
end

How to use "Result Variable Name" in JDBC Request object of Jmeter

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.

Resources