How to define a global class in SoapUI as a groovy

2019-01-15 16:02发布

问题:

I want to define a class in a groovy script that i could reuse trough other groovy script inside SoapUI.

I alredy tried to define my class inside a TestSuite property but it doesn't work. I would like to avoid defining the class in a JAR because we work in team and every one would have to import the JAR in their SoapUI in order to run my tests. I use SoapUI 3.6.1

Here's how my TestSuite is made :

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

To simplify me tests, i defined a class in 'Test1' and i would like to reuse this class in 'Test2'. So ideally i would define that class in 'Init' and it would be accessible to any other groovy script.

How can i achieve that?

回答1:

I am pretty sure that you will have to create a JAR file and put it in \bin\ext.

SoapUI will automatically pick it up on restart (you should see it mentioned in the startup stuff).

You basically just create a Java or Groovy project, Export it (with Eclipse) and it will work. SoapUI will probably have your dependencies covered, but if not you can add those JARs too (safer than creating a runnable JAR since SoapUI might use different versions of what you use).

If you need help, post related questions.



回答2:

try this, I think will help you in reusable code.

http://forum.soapui.org/viewtopic.php?t=15744



回答3:

Based on @Abhey Rathore's link, here is how to define a global class in SoapUI's groovy script:

Step 1: Declare the global class

  • Create a project called MyRepository
  • Under this project create a disabled TestSuite called lib
  • Under this test suite create a TestCase called MyClass
  • Under this test case create a groovy script called class

Your project tree should look like the image below:

Step 2: Write the global class code

Inside the groovy script called class, copy and and paste the code below:

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) )

Step 3: Import the global class

From any project, you can import the global class by using the following snippet:

// 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:

In SoapUI 5.3.0 you can use properties that can be set and get on the context variable. That variable is available in script assertion snippet.

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

And from different test script you can get the value by: context.getProperty("prop");

You can use the property from when you define property as a step of test suite or as a value for field in header. You can do it by ${=context.getProperty("prop");}



回答5:

Write this into your Project's load script, then reload your project or just run it.

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

After it has run, you can now put .groovy files into the same folder as your soapui project xml. Plus, there is no need to compile anything.

e.g. in MyClass.groovy:

class MyClass {String name}

You can import those classes as normal in all your other scripts:

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

The only caveat is that your load script (the one setting the classpath), cannot have those imports, because the script will fail to compile. If you want to import something during your project load script, just have your load script 'evaluate' another groovy script (in other words, run it), in which you can use the imports:

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

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

This seems the easiest way to me, so I'm not sure why I couldn't find this answer elsewhere online.



标签: groovy soapui