In Thread Group #1 I have a Regular Expression with Match Number set to -1 and I want to use the complete variable in Thread Group #2.
I am currently able to share normal variables using props.put but I am not able to share the complete array to then obtain the values using __V function on Thread Group #2.
Is this feasible?
Just add JSR223 PostProcessor (make sure it goes after Regular Expression Extractor) and use the following code:
vars.entrySet().each { var ->
if (var.getKey().startsWith('foo')) {
props.put(var.getKey(), var.getValue())
}
}
In another Thread Group you can convert properties into variables using the same approach:
props.entrySet().each {prop ->
if (prop.getKey().startsWith('foo')){
vars.put(prop.getKey(),prop.getValue())
}
}
Just replace foo with your variable reference name and that would be it.
You can find information regarding Groovy scripting in JMeter in Apache Groovy - Why and How You Should Use It guide if needed.
Related
Is there any way to call mule "flow" instance using groovy ? like When I use the groovy script and it will call the another flow, that means ( calls the another flow -- > sets the variable value and get back with the results). = Expected Behaviour.
Requirement :
Like, Mule Flow A is running independently and keep on incrementing variable value.
Mule flow B wants to access the Flow A variable (Incremental value) ( only variable ) using groovy or python Script.
Note: Script should not execute flow A for getting variable value.
Is there method like to get instance of variable in groovy ? like
flow=registry.lookupByName('A-flow').getInstance(VariableName) ?
// Need to get only variable value.
Groovy Script to call a Mule flow:
import org.mule.runtime.api.message.Message;
import org.mule.runtime.core.api.event.CoreEvent;
import org.mule.runtime.core.api.event.EventContextFactory;
flow=registry.lookupByName('A-flow').grep();
msg = Message.builder().value(payload).build();
event =CoreEvent.builder(EventContextFactory.create(flow, org.mule.runtime.dsl.api.component.config.
DefaultComponentLocation.fromSingleComponent("add-location"))).message(msg).build();
result =flow.process(event);
Mule flow B wants to access the Flow A variable with out deploying or
starting the flow A (as it is running independently ) using groovy or
python Script.
Variables live inside a Mule Event, which is really what contains the status of an execution triggered from a flow. However even if you get a list of all active events you may not know which one is the one you are interested. It is also a bad practice trying to use Mule internals inside an application.
Instead you should share the value of the variable you are interested using a standard method for Mule, like an object store, a queue or database. It really depends on what you are trying to do and the design of your application what is the method that will fit best.
What Groovy language construct, syntax or control structure is used in the following code fragment in a Jenkinsfile?
stage('Stage 1') {
steps {
// One or more steps
}
}
i.e. What are blocks in Jenkinsfile, in terms of pure Groovy language?
what is 'steps'? or stage?
Is it calling a function? or definition? or a function call with anonymous (lambda) argument?
Inherent in this question is another question:
Question 2:
Is a Jenkinsfile, a code fragment in groovy language?
In other words, 1. Does a Jenkinsfile follow all syntax and control structures of pure Groovy? (perhaps by an implicit library import-ed or #include d silently in beginning),
As opposed to being a DSL: 2. Jenkinsfile being almost a groovy source file augmented with new Jenkins-specific constructs not originally in Groovy, e.g. Jenkins using a preprocessing.
Which of the above two hold?
Related:
https://www.jenkins.io/doc/book/pipeline/syntax/
https://www.jenkins.io/doc/book/pipeline/getting-started/#directive-generator
What is a Jenkins Stage in terms of Groovy? (despite similar title, the ask is different. The related comments are inconclusive).
In Jenkins (or Gradle) 2 main features are used:
Groovy (java) idiomatic structures like loops, switches, chain of command etc
DSLBuilder facility based on Closures to allow for nesting and invoking of domain-specific methods as they were a part of Groovy itself.
So, if you write something like
stage('Stage 1') {
steps {
// One or more steps
}
}
it translates internaly to roughly:
jenkinsContext.stage('Stage 1') {
jenkinsContext.steps {
// One or more steps
}
}
so it is way clearer to write and read. Here the Closures - the {...} blocks - represent nesting or grouping of your code.
In this block you can also see Groovy's way of calling methods where the last argument is a Closure. The code above could be rewritten as:
jenkinsContext.stage 'Stage 1', { // here no brackets around args
jenkinsContext.steps( { // here with normal java-style brackets
// One or more steps
} )
}
In jenkins you can mix and match DSL calls with Groovy structures:
[ 'Stage 1', 'Stage 2' ].each{
stage( it ) {}
}
or even have your DSL names generated dynamically:
[ 'Stage 1':'stage', 'step 2':'steps' ].each{ value, name ->
"$name"( value ) {}
}
would create the DSL (as example ONLY!):
stage( 'Stage 1') {}
steps( 'Step 2' ) {}
So, Jenkins pipeline syntax is Groovy + Jenkins DSL
Looks like it's primarily Groovy, so if you're looking for simply syntax highligting adding something like the following does the job.
<!-- language: lang-groovy -->
There are caveats however documented in the online docs, so maybe this implies it's not written in pure groovy but some specialized/constrained form of it?
The basic statements and expressions which are valid in Declarative Pipeline follow the same rules as Groovy’s syntax with the following exceptions:
The top-level of the Pipeline must be a block, specifically: pipeline { }.
No semicolons as statement separators. Each statement has to be on its own line.
Blocks must only consist of Sections, Directives, Steps, or assignment statements.
A property reference statement is treated as a no-argument method invocation. So, for example, input is treated as input().
https://www.jenkins.io/doc/book/pipeline/syntax/#declarative-pipeline
TestPlan Thread Group HTTP Request1 ->Regular Expression Extractor - Return 10 Results - URLs -- Single Thread
ForEach Controller - Using variable from extractor - Successfully Loops through above results HTTP Request2 ->Regular Expression Extractor - Return 10 Results
This above is under 1 thread
I want to have ForEach Controller under different thread --run multiple therads and use the URLs extracted from 1 thread HTTP Sampler -- I tried to use these two approaches
https://www.blazemeter.com/blog/knit-one-pearl-two-how-use-variables-different-thread-groups
How do I pass a variable from one Thread Group to another in JMeter
but somehow now managed it to work
Please help
ForEach Controller will not work with JMeter Properties, it is designed to work only with JMeter Variables so if you want to pass them between different Thread Groups you will need to do some scripting.
Add JSR223 PostProcessor after the Regular Expression Extractor and put the following code into "Script" area
vars.entrySet().each { var ->
if (var.getKey().startsWith('foo')) {
props.put(var.getKey(), var.getValue())
}
}
replace foo with what you have as a Reference Name in the Regular Expression Extractor. The above code will convert all variables which names start with foo into the relevant JMeter Properties
Add Test Action sampler to the second Thread Group (you don't need to measure time of properties to variables conversion, do you)
Add JSR223 PreProcessor as a child of the Test Action sampler
Put the following code into "Script" area
props.entrySet().each {prop ->
if (prop.getKey().startsWith('foo')){
vars.put(prop.getKey(),prop.getValue())
}
}
the above code convert JMeter Properties into JMeter Variables so you will be able to use them in the ForEach Controller in the second Thread Group. Again, replace this foo with the reference name of your own variable.
See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests
Is there a possibility to have variables that depend on the listener where there lay in?
So I want to execute two samplers which are working with a JSR223 Assertion. I use a groovy code which asks for the value of the variable "name". If I execute Sampler A it should say "Tom" and if i execute Sampler B it should say "Paul".
It has to be possible to execute both at the same time.
You have sampler shorthand in the JSR223 Assertion which stands for this or that Sampler. So you can check its name by calling sampler.getName() function which will basically execute underlying AbstractTestElement.getName() method. The relevant code would look like:
if (sampler.getName().equals('Sampler A')) {
log.info('Tom')
}
else if (sampler.getName().equals('Sampler B')) {
log.info('Paul')
}
You can set a JMeter Variable from Groovy code using vars shorthand which in its turn stands for JMeterVariables class instance like:
vars.put('foo', 'bar')
Once done you will be able to refer created variable as ${foo} where required - it will have the value of bar
Check out Scripting JMeter Assertions in Groovy - A Tutorial article for more details.
I am wondering how we can pass variables between two step definitions files.
I found this How to share variables across multiple cucumber step definition files with groovy but their structure is different from mine, because I am not using classes in step definition.
The following is my two step definition files.
Feature File 1
Scenario: Consumer registration
When I try to register with my details with "memberNo" mem no.
Then I should be able to get success response
stepDef1
When(~'^I try to register with my details with "([^"]*)" mem no.$') { String memdNo ->
sMemdNo = memNo + getRanNo()
// more code here
}
Feature File 2
Scenario: Event Generation
When I activate my account
Then I can see the file having "logName" event
stepDef2
Then(~'^I can see the file having "([^"]*)" event$') { String logName ->
eventFile = GetLogtData(logName , sMemdNo )
// more code here
}
So, as per the above I want to get the value of sMemdNo from stepDef1 and use it in stepDef2.
I will recommend that you use the World, to store global variables needed across step definitions.
You can see an example here: cucumber-jvm-groovy-example.
You can combine the World with a Factory and/or dependency injection pattern.
To use variables between steps you can add the variable at the top of the steps file (groovy or java), and the variable used in one step will have the value available for other variable.
Example
Result