How to use modulo in if condition wit global counter in jmeter - if-statement

How it is possible to use module operator in Jmeter If Controller to call request only after some amount of requests.

You can track the current iteration of Thread Group using ${__jm__Thread Group__idx} pre-defined variable
You can use __jexl3() function in the If Controller's condition to run its children only after iteration N, example syntax:
${__jexl3(${__jm__Thread Group__idx} > 9,)}
Demo:
More information: 6 Tips for JMeter If Controller Usage

It is possible with following statement in If Controller:
${__javaScript(${__counter(FALSE,)}%2 == 0)}

Related

Turning an Xpath into a list variable and use For loop

I'm very new to robot framework. This question is about creating a list variable using Xpath.
I'm trying to write a FOR LOOP in robot in order to print all items that appear on a search result page. Here is what I've done so far:
I tried to declare a list variable using Xpath:
#{product} //div[contains(#class,'ProductCard__TitleMaxLines')]
This Xpath has the number of repetitions the same as the number of items on the page, e.g. 20, so I assume that this list will contain 20 members. --> Is this how it works?
I created a For loop:
${index} Set variable 1
For ${product} in #{related_product}
Exit for loop if ${index} > 20
Log to console ${product}
${index} Evaluate ${index}+1
End
For this, I assume that it will take the first product found on the Xpath to be index=1, and the next index=2, and so on.
That's all I've done so far. Please advise if this works correctly or if there is another way that is better and more typical.
I think your question is, you have similar kind of xpath around 20 times in the webpage. If my understanding is correct, you should use the Get Webelements keyword from Selenium Library.
#{product} Get Webelements //div[contains(#class,'ProductCard__TitleMaxLines')]
The above line will create the list of webelements which having the same xpath

How to increase Variable value based on the iteration being run in Postman

I have an API request that I need to run in Postman-Collection-Runner thru multiple iterations. The API request uses Variable.
How can I make this variable to automatically increase with each iteration (or maybe set the iteration value as another Variable)?
If I understand your question correctly, you would like to assign different values to a variable in the request in different iterations which is achievable in 2 ways.
a) Using data files
https://learning.getpostman.com/docs/postman/collection_runs/working_with_data_files/
The data files could be in JSON or CSV format. Unfortunately, there is no way in Postman to tie the variable values to another variable unless you want to do it in a hacky way!
b) Pre-request & Tests scripts
1- Initialise the environment variable in the Pre-request Scripts like this:
var value = pm.environment.get("var");
if( !value) {
pm.environment.set("var", 1);
}
2- Increment the variable value in Tests
var value = pm.environment.get("var");
pm.environment.set("var", value+1);
This creates an environment variable and increments it after each iteration. depending on how you structure your collection you might need to consider flushing/resetting the environment variable to be ready for the next run
It worth mentioning that Pre-request Scripts and Tests running before and after the requests respectively, so you can write any scripts that would like to run after the request in the Tests. It shouldn't be necessarily a test!
1. Using Global pm.* functions and Variables in Pre-Request Scripts/Tests
Pre-Request script - runs before executing the request
Tests - runs after executing the request
a.
pm.variables.set("id", pm.info.iteration);
Ex: example.com/{{id}}/update gives
example.com/0/update
example.com/1/update etc...
Number of iterations is set in Collection Runner. pm.info.iteration key has the current iteration number, starting at 0.
b.
var id = +pm.globals.get("id");
pm.globals.set("id", ++id);
The variables can be in any scope - globals/collection/environment/local/data.
In Collection Runner, check the Keep Variable Values checkbox, to persist the final value of the variable in the session (here id).
Note: If the variable is accessed via individual scopes (via pm.globals.* or pm.environment.* or pm.collectionVariables.*), then the mentioned checkbox should be toggled as required. Else if accessed via local scope (pm.variables.*), the value will not be persisted irrespective of the checkbox.
Ex: Same as above
More on variables and scoping
2. Using Dynamic variables
These variables can be used in case random values are needed or no specific order is necessary.
a. $randomInt - gives a random Integer within 1 - 1000.
Ex: example.com/{{$randomInt}}/update gives
example.com/789/update,
example.com/265/update etc...
b. $timestamp - gives current UNIX timestamp in seconds.
Ex: example.com/{{$timestamp}}/update gives
example.com/1587489427/update
example.com/1587489434/update etc...
More on Dynamic variables
Using Postman 7.22.1, while answering this. New methods may come in future.

JMeter repeat Regular Expression extractor for all requests

I have a JMeter script that goes through a bunch of requests, each being different, GETs, POSTs and so on...
Each request returns a custom header from the server that has some numeric values in it. This header returns the actual processing time it took on the server side (without latency/http overhead)
I was able to add a Regular Expression Extractor to get that value from the header without any problems, however I would like this to be repeated for all the requests.
By using Debug Sampler I can see that the extractor only runs once, seems to be the last instance.
How can I have an extractor that runs all all requests and collects all the values from the header.
Bonus question. Finally I would love to be able to aggregate these values and get one average value.
Disclaimer: This other question is similar to mine but it doesn't explain how to actually do it in terms of the locations of the extractor and the debug sampler.
Track results of a regular expression extractor in JMeter
Thank you.
Just put Regular Expression Extractor at the same level as your HTTP Request samplers and it will be applied to all of them
See Scoping Rules User Manual entry for more detailed explanation.
With regards to value collection the best option is using Sample Variables property. Given you store your header value into a variable called ${foo} you can get it appended to jtl results file by adding the next line to the user.properties file:
sample_variables=foo
JMeter restart will be required to pick the property up. The other way (which doesn't require restart) is passing the property via -J command-line argument as
jmeter -Jsample_variables=foo -n -t test.jmx -l result.jtl
As the result you will get an extra column called foo in the .jtl results file and it will hold the ${foo} variable value for each Sampler. Upon test completion you will be able to open .jtl results file with MS Excel or equivalent and use AVERAGE function to get the value you're looking for.
See Apache JMeter Properties Customization Guide for more information on setting and amending various JMeter properties for Configuring JMeter according to your needs.
While Dmitri's answer is one way of doing it. But I wanted something different than each time exporting it into a file and post processing it...
I ended up doing this "manually"
By manually I mean I added a BSF Assertion with language = JavaScript and then wrote some JavaScript to do this:
Pull the value out of the header (if found)
Keep a record of total/count using variables
Updating a variable that shows the aggregate always
Add Debug Sampler to get easy access to the values after the test.
The following is the code that I used in the BSF Assertion:
var responseHeaders = prev.getResponseHeaders();
var xNodetasticRt = /x-nodetastic-rt: (\d+\.?\d*)/.exec(responseHeaders);
if (xNodetasticRt) {
var value = parseFloat(xNodetasticRt[1]);
vars.put("xNodetasticRt", value);
var total = parseFloat(vars.get("xNodetasticRt-Total"));
if (!total) {
total = 0.0;
}
total += value;
vars.put("xNodetasticRt-Total", total);
var count = parseFloat(vars.get("xNodetasticRt-Count"));
if (!count) {
count = 0;
}
count++;
vars.put("xNodetasticRt-Count", count);
vars.put("xNodetasticRt-Average", total / count);
}

SOAPUI - changing request element value

I read some articles about SOAPUI, one of them is this SoapUI getting request parameters in mock service script and I think the solution I am looking for is something using Groovy.
I have a SOAP Web Service that I want to run some testes with a dynamically changing request. This request...
<soapenv:Body>
<req:MyrRquest>
<req:number>XPTO</req:number>
</req:MyrRquest>
</soapenv:Body>
My idea is to run a loop from a starting value increasing 1 until I reach my maximum. And I would like to replace XPTO with this changing value.
Did anyone ever attempted this? What is the best way to do that?
Here is the way it can be done, by the use of groovy step.
Define a test case with two test steps:
Test Request step(soap, the one you shown)
Groovy Script Step(this is the additional one which I am proposing)
Define below three test case level custom properties like what you needed min and max times it should be repeatedly executed and provide values as per the test and keep CURRENT_VALUE same as MIN_VALUE which is one time job. Because, CURRENT_VALUE that gets incremented each time and do not want to alter MIN_VALUE each time the test runs. That way, do not have reset the value after each time test case is executed.
MIN_VALUE
MAX_VALUE
CURRENT_VALUE
Note that this cannot run individual steps i.e., the test case has to be executed in order to fulfill your need as it has to repeat the number of times, and hope that is ok for you.
In the test request, need to use the current value place holder.
Change: <req:number>XPTO</req:number>
To : <req:number>${#TestCase#CURRENT_VALUE}</req:number>
Here is the groovy script code:
//Read the test case level properties as integers
def min = context.testCase.getPropertyValue('MIN_VALUE') as Integer
def max = context.testCase.getPropertyValue('MAX_VALUE') as Integer
//Get the previous step name
def pStepName = context.testCase.testStepList[context.currentStepIndex-1].name
//min+1, because already test request is executed once
((min+1)..max).each {
//update the current value incremented by 1
context.testCase.setPropertyValue('CURRENT_VALUE', it.toString())
log.info "Running step ${pStepName} for ${it} time"
//run the previous test step
testRunner.runTestStepByName(pStepName)
}
//finally resetting current value to min value as test finishes
context.testCase.setPropertyValue('CURRENT_VALUE', min.toString())
This groovy script step basically takes care of running the first step for n-1 times, because step 1 already executed before groovy script test step where n is the total number of times required to be executed(n = max - min).
And as mentioned earlier, just run the test case.

Is there a way to access the iteration number in Postman REST Client?

I'm using postman for API testing. I'm running a large number of tests and I want to print the iteration number to the console on some of them. Is there a way to get the iteration number as an environment-like variable?
According to Postman API Reference, pm.info.iteration - is the value of the current iteration being run.
Example:
console.log(pm.info.iteration);
It is possible now! You can access the iteration variable, in the same way you access other variables like responseBody.
I don't know if there is an internal way to get the iteration number but I believe you should be able to track this number through code yourself. Here's a quick code snippet:
var value = environment.count;
value++;
postman.setEnvironmentVariable("count", value);
If you put this in the pre-request editor or the test editor of a collection that you are sure will run once per iteration it will effectively track the iteration count.
You can get the iteration number with
pm.info.iteration:Number
Is the value of the current iteration being run.
Postman Sandbox API reference
I got there like this:
const count = pm.info.iteration+1
console.log("======== LITERATION "+count+" ========");