Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

groovy

soapui

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?

like image 651
Gab Avatar asked Jul 31 '12 14:07

Gab


2 Answers

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:
Project tree for declaring a global class in SoapUI

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

like image 147
Stephan Avatar answered Oct 13 '22 21:10

Stephan


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

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

like image 30
Abhey Rathore Avatar answered Oct 13 '22 20:10

Abhey Rathore