Search code examples
jmeterbeanshelltest-plan

Beanshell script launched once (start and end of test plan) in JMeter


Good afternoon !

I will try to explain you clearly my problem.

The context

I have a JMeter TestPlan which send HTTP requests to a server. I have a Beanshell script to assert each different case of error returned.

  • 302 response code -> OK
  • 200 response code -> ?

In each error 200, I check the response data string to see if it is an error or a correct case. (User error like User don't have correct rights is OK, but Server is unavailable is ERROR and both have 200 as response code.)

Here is my test plan :

TestPlan JMeter

The goal

As I have several errors returned by only one assertion script, I am not able to differenciate each error, except by uncollaspe the assertion in a ViewResultTree. But I disable it when launching my test, and I will launch my TestPlan remotely.

I had the idea to manually count each error. All my samples goes in my Assertion script, and goes to the correct if block according to their content. I increment some variables (JMeter.properties in fact) in each block.

int test = Integer.parseInt(props.getProperty("302"));
test++;
props.setProperty("302", ""+test);

I want to display all those variables in a JFrame at the end of my testplan like this :

ScriptJFrame

The problem

My problem is that I don't know how to launch a Beanshell script before and after the TestPlan.

I want a first script to be started before any sample is send, just to initialize all my properties variables to 0 (else, they keep the value of the last TestPlan).

And, I want a second one to display my Frame with all the variables after the test plan is finished. (Currently it is a JFrame but it will not stay like this.)

Tested solutions

1) For my first script, I set a Counter (JMeter > Config Element > Counter) in the beginning of my test plan to 0. I use it to check if my test already started of not with an If Controller :

If Controller (counter==0)

I have a Pre-Processor Beanshell with props.set("302","0"); where "302" is my property to count all 302 response code.

It correctly works but I want to know if there is a proper way to do this.

2) Then, for my second script, I tried to use ${JMeterThread.last_sample_ok} in an If Controller aswell but it doesn't work like I expected. If I put it after my sample, it start after all OK assertion, and if I put it at the end of the test plan, it is never called.

How can I run my beanshell script once, after all my threads are stopped (i.e. all sample finished) ?

Thank you in advance, I hope you understood everything !


Solution

  • JMeter SetUp thread group and TearDown thread group are meant for exactly this.

    Add your beanshell component to the setUp thread group to do some setup activities before your actual test starts. Similarly the tearDown thread group runs after your test execution is complete.