beanshell scripting in apache jmeter

11
Bean Shell Scripting in Apache JMeter NAVEENKUMAR NAMACHIVAYAM Founder – QAInsights.com http://QAInsights.com

Upload: naveenkumar-namachivayam

Post on 15-May-2015

8.758 views

Category:

Technology


0 download

DESCRIPTION

This presentation explains about Beanshell scripting in Apache JMeter. This presentation walkthroughs with examples and sample code to understand the concept better.

TRANSCRIPT

Page 1: Beanshell scripting in Apache JMeter

Bean Shell Scripting in Apache JMeter

NAVEENKUMAR NAMACHIVAYAMFounder – QAInsights.com

http://QAInsights.com

Page 2: Beanshell scripting in Apache JMeter

Introduction to Beanshell

• BeanShell is a Java-like scripting language, invented by Patrick Niemeyer.

• Pure Java

• Free

• Runs in the Java Runtime Environment and uses Java syntax

• BeanShell is small and embeddable, so you can call BeanShell from your Java

applications to execute Java code dynamically at run-time

• standalone BeanShell scripts to manipulate Java applications; working with Java

objects and APIs dynamically. 

• Runs in four modes: Command Line, Console, Applet, Remote Session Server.

• With BeanShell you can create custom outputs, perform logical operations,

manipulate variables, etc.

Page 3: Beanshell scripting in Apache JMeter

Scripting Features

• Optionally typed variables.

• Scripted methods with optionally typed arguments and return values

• Scripted objects (method closures)

• Scripted interfaces and event handlers.

• Convenience syntax for working with JavaBean properties, hashtables, and

primitive wrapper types.

• Auto-allocation of variables to emulate Java properties files.

• Extensible set of utility and shell-like commands

• Dynamic classpath management including find grained class reloading

• Dynamic command loading and user command path

• Sophisticated namespace and callstack management

• Detailed error reporting

Page 4: Beanshell scripting in Apache JMeter

Bean Shell Sampler

• In order to manipulate and work on the response data, Bean Shell sampler would

be a great element.

• To add Bean Shell sampler, right click, Add > Sampler > Bean Shell Sampler.

Page 5: Beanshell scripting in Apache JMeter

Bean Shell Sampler

• In this example, user name will be retrieved and manipulated using Bean Shell

sampler.

• If the username is not equal to NAVEENKUMAR NAMACHIVAYAM, then the thread

will stop.

• Use Regular Expression Extractor to extract the username as shown below.

Page 6: Beanshell scripting in Apache JMeter

Bean Shell Sampler

• Once the username is extracted, add Bean Shell Sampler to manipulate.

• Here the username will be stored in username variable.

• To get the extracted value in Bean Shell sampler, use below syntax.

• String userName = vars.get("username");

• Next step is to check the condition using if statement.

• if (userName != "NAVEENKUMAR NAMACHIVAYAM")

• You can use Data set config file variable too, if you do not want to hard code the

user name.

• Now, if the condition is true, following statement will be used to terminate the

execution.

• SampleResult.setStopThread(true);

• Below statement will be used to return the value.

• return userName

Page 7: Beanshell scripting in Apache JMeter

Bean Shell Sampler

Page 8: Beanshell scripting in Apache JMeter

Bean Shell Sampler Output

Page 9: Beanshell scripting in Apache JMeter

Bean Shell Sampler – Some more enhancement

• To print the output to command prompt, you can use

• System.out.println statement

• System.out.println("Response Code is " + ResponseCode);

• System.out.println("Response Message is " + ResponseMessage);

• if (SampleResult.isSuccessful())

• {

• System.out.println("Success");

• SampleResult.setStopTest(true);

• SampleResult.setStopThread(true);

• }

Page 10: Beanshell scripting in Apache JMeter

Bean Shell Sampler – Some more enhancement

Page 11: Beanshell scripting in Apache JMeter

Thank you

Please visit http://qainsights.com/services for more training programs.