How to define a global class in SoapUI as a groovy script?

I want to define a class in a groovy script so that I can reuse another groovy script inside the SoapUI.

I alredy tried to define my class inside the TestSuite property, but it does not work. I would like to avoid class definition in the JAR , because we work in a team, and each of them will have to import the JAR into its SoapUI to run my tests. I am using SoapUI 3.6.1

This is how my TestSuite works:

TestSuite
  TestCase
    TestSteps
       Init         (Groovy Script)
       GetResponse1 (Test Request)
       Test1        (Groovy Script)
       GetResponse2 (Test Request)
       Test2        (Groovy Script)

To simplify the tests, I defined the class in 'Test1', and I would like to reuse this class in 'Test2'. Therefore, ideally, I would define this class in "Init", and it would be available for any other groovy script.

How can i achieve this?

+5
5

, JAR \bin\ext.

SoapUI ( ).

Java Groovy, ( Eclipse), . SoapUI, , , JAR ( runnable JAR, SoapUI , ).

, .

+1

@Abhey Rathore, SoapUI groovy script:

1:

  • MyRepository
  • TestSuite lib
  • TestCase MyClass
  • groovy script, class

:
Project tree for declaring a global class in SoapUI

2:

groovy script, class, :

class MyClass {
     // The three following fields are MANDATORY
     def log
     def context
     def testRunner

     // The constructor below is MANDATORY
     def MyClass(logIn, contextIn, testRunnerIn) {
        this.log = logIn
        this.context = contextIn
        this.testRunner = testRunnerIn
     }

     def myMethod() {
        this.log.info 'Hello World!'
     }
}

// Other scripts elsewhere in SoapUI will access the reused class through their context
context.setProperty( "MyClass", new MyClass(log, context, testRunner) )

3:

, :

// Import the class
def currentProject = testRunner.testCase.testSuite.project
currentProject
    .testSuites["lib"]
       .testCases["MyClass"]
          .testSteps["class"]
             .run(testRunner, context)

// Now use it
context.MyClass.myMethod()

SoapUI 5.2.1

+4

SoapUI 5.3.0 , . script .

: context.setProperty("prop", "value");

script : context.getProperty("prop");

, . ${=context.getProperty("prop");}

0

script, .

com.eviware.soapui.support.ClasspathHacker.addFile( project.context.expand('${projectDir}') )

.groovy , xap soapui. , -.

. MyClass.groovy:

class MyClass {String name}

:

import MyClass
assert new MyClass(name:"me") instanceof MyClass

The only caveat is that your load script (the one that sets the classpath) cannot have these imports because the script will not compile. If you want to import something while loading your script project, just load the script 'grade' of another groovy script (in other words, run it) in which you can use the import:

evaluate (new File (project.context.expand('${projectDir}') + 'myProjectLoadScript.groovy'))

//in myProjectLoadScript.groovy:
import MyClass
/* Do things using MyClass... */

It seems to me that this is the easiest way, so I'm not sure why I could not find this answer elsewhere on the Internet.

0
source

All Articles