cancel
Showing results for 
Search instead for 
Did you mean: 

passing values from multiple scripts

SOLVED
Highlighted
Contributor

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

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"]

}
}

 

 

View solution in original post

8 REPLIES 8
Highlighted
Community Hero

If all three are scripts, then why not use together in one?


Regards,
Rao.
Highlighted
Contributor

Well I was trying to make my code modular.

Highlighted
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.

 

 

/T3D
Highlighted
Contributor

thanks  for your reply. I will go try it out

Highlighted
Contributor

So not exactly what I was looking for

lets say I have the following structure see below.

 

TestSuite1

 - testCase1

 - Script reads in data from a file and saves it to a MAP. I then want to pass this MAP to another TestCase2-1 and manipulate the MAP data.

 

TestSuite2

 -testCase2-1

   - testStep Groovy script. What I need to do here using the values from "TestCase1" MAP is do some test parsing and replace the MAP values.

 

Any help would be appreciated.

 

 

 

Highlighted
Contributor

I figured it out

 

TestSuite1

 - testCase1

    - Groovy Script   

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

      class MyClass extends GroovyTestCase{
        def V3

         def myMethod(x){
           x[1] = "NEW VALUE"
          V3 = x
       return V3
      }
    }

 

TestSuite2

 -testCase2-1

   - Groovy Script
      def scripts
      def sumNum1

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

 
     sumNum1 = context.myClass1.myMethod(context.testCase.getPropertyNames())
     log.info sumNum1.toString()

Highlighted
Contributor

Oops my last post only passes property files, I still need a way to pass a MAP

Highlighted
Contributor

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"]

}
}

 

 

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get a full Raw JMS request

Participate!

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors