Forum Discussion

zsousa's avatar
zsousa
Contributor
9 years ago

passing values from multiple scripts

I have three scripts each script calls the other script and needs to pass back a value to the original calling script.

 

Script 1

loops all my properties until a condition is met. Then it passes that property value to Script 2

so, script 1 passes value X to script 2

 

Script 2

calls Script 3, which passes back a value to script 2 and combines that value to the value form Script 1.

so script 2 calls Script 3. Script 3 passes back a value Y to script 2.

then it combines X + Y and returns that value to Script 1

 

hope that makes sense, the issue I am facing is how to call each script and pass the values back in the order the scripts where called.

  • ok I figured it out, how to pass a MAP to another groovy script and modify it.

    This is how I did it.

    1. create your map

    2. serialize your map

    3. save you map as a test case property

    4. in the script your calling, all you have to do is de-serialize the property and use it like a map.

     

    See code below:

     

    import groovy.json.JsonBuilder

    def scripts
    def sumNum1
    testRunner.testCase.removeProperty("map")
    def map2 = [:]
    def n


    for(i=0;i<testRunner.testCase.getPropertyNames().size();i++){
    n = testRunner.testCase.getPropertyAt(i).getValue()
    def y = testRunner.testCase.getPropertyAt(i).getName()
    map2.put(y,n)

    }


    scripts = testRunner.testCase.testSuite.project.testSuites["sand_box"];
    scripts.testCases["TestCase 1"].testSteps["Groovy1"].run(testRunner, context);

    builder = new JsonBuilder()
    builder(map2)

    testRunner.testCase.setPropertyValue("map",builder.toString())

    sumNum1 = context.myClass1.myMethod(context.testCase)

     

     

    class script:

    import groovy.json.JsonSlurper


    context.setProperty("myClass1", new MyClass());

    class MyClass extends GroovyTestCase{

     

     

    def slurper
    def V3
    def m

    def myMethod(x){
    slurper = new JsonSlurper()


    V3 = x.getPropertyValue("map")

    m = slurper.parseText(V3)

    return m["C1"]

    }
    }

     

     

  • nmrao's avatar
    nmrao
    Champion Level 3
    If all three are scripts, then why not use together in one?
    • zsousa's avatar
      zsousa
      Contributor

      Well I was trying to make my code modular.

      • prashobkt's avatar
        prashobkt
        Contributor

        1. Create a groovy class in a separate file with (.groovy extension),

        2. create methods corresponding to your scripts

        3. Add a groovy script test step, 

        4. In the groovy test step create the object of the file 

        5. Call all the functions as required

         

         

        Attached is screenshot to add the class file to your project.