Forum Discussion

captainfranz's avatar
captainfranz
Occasional Contributor
12 years ago

Visibility of variables in load test setup script

I currently have a simple load test which consists of two steps: a Groovy script and an HTTP request.
I need some setup code that generates some variables and then I need to read those variables (lists if that matters) in the Groovy test step.
I feel that the right place to put this initialization code is in the setup script of the load test. However, it seems that I can't directly access those variables from the Groovy script test step. Is there a special syntax that I need to use in order to make them visible there or is there a better way of doing this?
This page I found in the documentation might be relevant:
http://www.soapui.org/Load-Testing/load ... pting.html
However, even after reading that, it is still not quite clear how to do exactly what I'm asking, maybe an example in that page would be helpful.

5 Replies

  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    Sounds like you need to either store the variable to a property or use a context variable

    I personally like properties but context variables are easier on the processor. It's really up to you.
  • captainfranz's avatar
    captainfranz
    Occasional Contributor
    Apologies for the delay in my reply, I didn't have time to come back to this problem until today.
    I am aware of what you proposed, however an example would be extremely helpful. The documentation is very lacking on examples.

    1. If I use the context, I am doing the following.
    In the load test setup script:
    context.myList = [1, 2, 3]

    In the test step script:
    listSize = context.LoadTestContext.myList.size()

    This fails with "java.lang.NullPointerException: Cannot invoke method size() on null object". So it appears this is not the right syntax, but it is what the documentation says.

    2. If I use a property, I have used the following syntax to get a property value from a test step (and similarly to set a value):
    testRunner.getTestCase().getTestSuite().getPropertyValue("myList")

    However I am not able to set a property from the load test and successfully read it from the test step. Also, since property values can only be strings, I would have to convert from list to string and back. How do I convert back without writing my own parser? What if the object I need to pass is a complex one and not just a simple list?

    3. Is the execution of the load test setup script triggered by a different "play" button (just above the script) than the entire load test (at the top of the screen). It appears so. This is weird. I would expect that when I trigger the load test, the setup script executes first, then the steps of the load test are executed.
  • GiscardN's avatar
    GiscardN
    Frequent Contributor
    Hi,

    This is an example where you define an array in your LoadTest Setup Script and put it in the context:
    def array1 = ["a", "b", "c"]
    context["myarray"] = array1

    Then in a script in your TestCase you can retrieve it as follows:
    def arr = context.LoadTestContext["myarray"]

    But another way to go about this is to use a DataSource in your TestCase: http://www.soapui.org/Data-Driven-Testing/datasources.html
    Notice the "Shared" configuration option to share the DataSource between running threads during your LoadTest. That's another option to explore depending on your testing scenario.

    Regards,

    Giscard
    SmartBear Support
  • captainfranz's avatar
    captainfranz
    Occasional Contributor
    Thank you, this worked. So the problem was that you have to use the array/map notation instead of the dot notation. It would be nice to have this in the documentation, as the only example I can find here http://www.soapui.org/Load-Testing/load ... pting.html uses the dot notation and therefore won't work (unless it's somewhat different than the example I had).
  • naz4viper's avatar
    naz4viper
    New Contributor
    Hi Giscard/SmartBear Support -

    Thank you for your help as your answer here helped me with my scripting issue also. Question - what context should be used if I want the TestSuite-level Setup Script to be used when I import my project into LoadUI (using SoapUI Runner) ?

    Using the SoapUI built-in LoadTest feature I used the LoadTestContext in my TestStep because the LoadTest couldn't execute the TestSuite-level Setup Script. I just want to know how I need to set my TestStep context when importing project using the standalone LoadUI tool to ensure the Setup Script is executed? Should I use Test Suite setup script? or LoadTest Setup Script?

    Please let me know if my question doesn't make sense I can try to explain further.