2
votes

I'm new to JMeter BeanShell PreProcessor function.

For a test I'm trying to print sample URL address. From the tutorial I have the follownig code:

org.apache.jmeter.samplers.SampleResult;

String currentURL = SampleResult.getUrlAsString();
print(currentURL)

But I get error "Attempt to resolve method: getUrlAsString() on undefined variable", how to define this variable first?

2

2 Answers

0
votes
  1. Be aware that since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any form of scripting in JMeter tests. So consider migrating to Groovy as soon as possible.
  2. If you are using a PreProcessor it means that you don't have any SampleResult as sampler has not been yet executed. So you need to use sampler shorthand which is resolved into HTTPSamplerProxy like:

    sampler.getUrl() as String
    

Demo:

JMeter Get Current Sampler URL

More information: Apache Groovy - Why and How You Should Use It