Forum Discussion

Kathryn_O_Malle's avatar
Kathryn_O_Malle
Contributor
14 years ago

Passing groovy 'lists' via testcase properties...

Hi,

I am trying to work out how I can create a groovy list and then pass it between testcases using a testcase property. I can create the list happily in a groovy script step but I hit a problem when:
a) I try and store the groovy list in a testcase property
b) I try and retrieve the stored groovy list from a testcase property

The groovy script snippet below helps illustrate it:
[NB: testcase containing this groovy script step has a property called 'idList']
def initIdList = [1,2,3]
log.info("...initIdList = "+initIdList)
testRunner.testCase.setPropertyValue("idList", (String)initIdList)

def postIdList = context.expand( '$(#TestCase#idList)' )
log.info("...postIdList = "+postIdList)
log.info(...size of postIdList = "+postIdList.size())

Current Issues:
(1) Having to use '(String)' when storing the list in a testcase property - script step won't currently run without it
(2) When I extract the list out from the testcase property I've lost the contents of the list and instead get a two character array of '[]'

Thanks in advance for any help to sort this one out...

29 Replies

  • By the way, below solution given by Smartbear staff also works.

    Although not as clean as the one I share in other comment but this also works. If I put all the pieces together what is shared in this post, this is what you have to do (Run tested OK - no changes required below. No addition of property at test case level).

     

    1. Create a test case (say with name TestCase1).
    2. Add a groovy script step (say with name Groovy Script1). Add below code here:

    import javax.naming.Context

    def list = [1,2,3,"adfadfas"]
    context.setProperty("list",list)
    log.info "from script 1 " + list

     

    3.Add a second groovy script step (say with name Groovy Script2). Add below code here:

     

    import com.eviware.soapui.support.types.StringToObjectMap
    def contextMap = new StringToObjectMap( context )

    def list1 = contextMap.list
    log.info "from script 2" + list1

     

    4. If you want to add another groovy script step (say with name Groovy Script3). Repeat above code:

     

    import com.eviware.soapui.support.types.StringToObjectMap
    def contextMap = new StringToObjectMap( context )

    def list1 = contextMap.list
    log.info "from script 3" + list1

     

    5. Now add a last groovy script to call the above 3 steps (by running the whole test case). Remember to make this step disabled otherwise while running the test case, this step will again call the test case and you will go in a recurring loop. (So Groovy Script4 (disabled)): add below code. 

     

    import com.eviware.soapui.support.types.StringToObjectMap
    def contextMap = new StringToObjectMap( context )

    def tCase = testRunner.testCase.testSuite.testCases["TestCase 1"]
    tCase.run(contextMap, false)

     

    6. Now if you run this disabled step, you will see that the array/list from groovy script1 was passed in all other steps.

    note: You can also do the same by adding this run groovy script in another test case and run from there. 

     

    Hope this helps!

    Cheers, Pramod 

    • nmrao's avatar
      nmrao
      Champion Level 3

      Thank you PramodYadav for your efforts and posting the solution.

       

      There is a suggestion:

      Change #1

      From

       

       

      import javax.naming.Context
      def list = [1,2,3,"adfadfas"]
      context.setProperty("list",list)
      log.info "from script 1 " + list

      To

       

      context.list = [1,2,3,"adfadfas"]
      log.info "from script 1 : ${context.list}"

       

      Change #2

      From

       

      import com.eviware.soapui.support.types.StringToObjectMap
      def contextMap = new StringToObjectMap( context )
      def list1 = contextMap.list
      log.info "from script 2" + list1

      To 

       

      log.info "From script 2 : ${context.list}"

      But there is a limitation with above approach i.e., that works only if test case is run and does not work if individual steps are run.

       

      Hope you aware of it.

    • groovyguy's avatar
      groovyguy
      Champion Level 1

      While posting solutions and helping is great, usually reviving a thread that's this old isn't a good idea.

      • PramodYadav's avatar
        PramodYadav
        Contributor

        Well this was a problem that was bothering us for a very long time and we needed a solution badly to make a good solution design. We could also see many other users struggling with the same problem statement without any concrete answers on internet.

         

        Nonetheless, I was not aware reviving a old thread could be a bad idea. Will explore more on this. Any inputs are also welcome and much appreciated on this matter. Thanks! 

  • RJanecek's avatar
    RJanecek
    Regular Contributor
    hi

    try this in setup script
    here you define list

    import javax.naming.Context

    def list = [1,2,3]
    context.setProperty("list",list)


    here you can get this list


    import javax.naming.Context

    def list1 = context.list
  • Hi,

    Thanks for that. In isolation I can get the code to work but when I insert it into my project I can write to the list variable but I can't read from it; instead I get "null". I think I'm hitting scope issues with my setup.

    I create and write to the list variable in testsuite1. Inside testsuite1, a script calls testsuite2. When I try to read the list variable in testsuite2 I get nothing. I suspect the list variable has gone out of scope at this point.

    As per http://www.soapui.org/Scripting-Properties/working-with-properties.html is there a way to modify the code so as to specfiy a #global# or #project# scope for this variable. Due to our current setup, defining the list variable in the project startup script would hinder our debugging process at times.

    Many thanks.
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello,

    The context object is scoped to the current execution. When you run the second TestSuite from Groovy you're probably creating a new TestRunner with a new context, so the variable is not passed into it. Try using the following Groovy code to run the second TestSuite (change 'TestSuite 2' to the name of your actual TestCase):


    import com.eviware.soapui.support.types.StringToObjectMap

    def contextMap = new StringToObjectMap( context )
    testRunner.testCase.testSuite.project.testSuites['TestSuite 2'].run( contextMap, false )


    Regards,
    Dain
    eviware.com
  • md_zoran's avatar
    md_zoran
    Occasional Contributor

    I think I am in the same situation as PramodYadav and the issue is still present. Should I post a new thread or can I continue on the same thread, especially since the last message is not over 1 year old (as mentioned before in this thread) ?

  • md_zoran's avatar
    md_zoran
    Occasional Contributor

    And also... I found a solution / a way in which it works.