Preethi
Preethi

Reputation: 31

Jmeter: how to initialise header manager element globally

I wanted to use the same set of headers in multiple jmx files. So I wanted to initialise it once and have to use it across my jmx files.
Can anyone help me in meeting my requirement? Thanks in advance.

Upvotes: 0

Views: 899

Answers (2)

Dmitri T
Dmitri T

Reputation: 168082

You can do this as follows:

  1. Create a CSV file called headers.csv to hold your headers like:

    header-1-name,header-1-value
    header-2-name,header-2-value
    

    and store it in "bin" folder of your JMeter installation

  2. Add empty HTTP Header Manager to the top level of your Test Plan

  3. Add setUp Thread Group to your Test Plan
  4. Add JSR223 Sampler to the setUp Thread Group
  5. Put the following code into "Script" area:

    import org.apache.jmeter.protocol.http.control.Header
    import org.apache.jmeter.protocol.http.control.HeaderManager
    import org.apache.jmeter.threads.JMeterContext
    import org.apache.jmeter.threads.JMeterContextService
    import org.apache.jorphan.collections.SearchByClass
    
    SampleResult.setIgnore()
    
    def engine = ctx.getEngine()
    def testPlanTree = org.apache.commons.lang3.reflect.FieldUtils.readDeclaredField(engine, "test", true)
    
    def headerManagerSearch = new SearchByClass<>(HeaderManager.class)
    testPlanTree.traverse(headerManagerSearch)
    def headerManagers = headerManagerSearch.getSearchResults()
    headerManagers.any { headerManager ->
        new File('headers.csv').readLines().each { line ->
            def values = line.split(',')
            headerManager.add(new Header(values[0], values[1]))
        }
    }
    

    enter image description here

    1. If you want you can "externalize" points 3 and 4 via Test Fragment

Upvotes: 1

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34536

That’s not possible. To be able to apply a Header Manager to all plan, it should have the largest scope but using Include or Module controller means reduced scope.

Thanks to scope stil, you can set your Header Manager as child of test plan and it will apply to whole requests.

You could use properties and __P function to make those configurable in user.properties

Upvotes: 1

Related Questions