Creating script library in SoapUI free version

2019-03-19 05:54发布

I am new in SoapUI and groovy scripting

I would like to create a repository of groovy scripts that can be reused at various test steps

I am using SoapUI Free version and following is the structure of my SoapUI Project

Project
|-TestSuite
| |-TestCase
|   |-TestSteps
|     |-LocalScript (Groovy TestStep to reuse library scripts)
|     |-OtherTestStep (Run TestCase TestStep)
|-ScriptLibrary
  |-TestCase
    |-TestSteps
      |-GroovyScriptStep1 (Contain a class for commonly used functions)
      |-GroovyScriptStep2 (Contain another class for other functions)

Here is what I was able to do:

I was able to create a library using the sample mentioned in this post. Similar to example in the post, my code in test step (GroovyScriptStep1 as per above structure) of library was just reading some value from external file and is used in test step of other TestSuite (LocalScript step in above structure).

Here is the problem:

Now I want to create a new class and add a function to it which will need info from running class and simply print it. The difference here is that some values are generated in the test run and should be passed to library script inorder to process/print etc.

To make my question more clear following is the code snippet

I will be using a simple scenario here

Sample objective: Want to be able to print all the assertions and status (since this will be used in all the test cases I want to create a library)

Code for same when not using library will be as under(this can go as groovy script step)

def obj = context.testCase.getTestStepByName("Request 1");
def assertions = obj.getAssertionList()

//Loop on assertions
assertions.each{
    log.info(it.name +  ' --> ' + it.status)

Code something similar in Library TestSuite's Test case step

context.setProperty("Assertions", new Assertions());

class Assertions{

    def printAssertion(def someArgumentToGetAssertionlistforTestStepinAnotherTestSuite){


        def obj = ????

        def assertions = obj.getAssertionList()

        //Loop on assertions
        assertions.each{
            log.info(it.name +  ' --> ' + it.status)
        }
    }

}

Code from where I want to call this method (LocalScript as per above project structure)

scripts = testRunner.testCase.testSuite.project.testSuites["ScriptLibrary"]; 
scripts.testCases["Scripts"].testSteps["Assertions"].run(testRunner, context);

context.Assertions.printAssertion(ArgumentRequired);

This is just one example, I want to create libraries of some more common scripts that use context variable when used locally

Kindly help me with this and please let me know if some more information/clarification is required

标签: groovy soapui
3条回答
可以哭但决不认输i
2楼-- · 2019-03-19 06:43

What I get from your questions is that you want to create a code library in SoapUI that can be reused. I think the best way is by creating jar files and deploying it in ext folder of SoapUI

  1. Create a new groovy script file with a class (follows java standards in file naming i.e. class name and file name should be same)
  2. Compile the groovy code file
  3. Create the jar file
  4. Deploy the jar file at SoapUI_Home/bin/ext folder
  5. Restart the SoapUI if was already open
  6. Create the object of class and use the methods anywhere in the SoapUI projects

Note: If you are migrating your project to some other machine, make sure to migrate these libraries as well if you are using them in projects

Details with example:

Step 1: Create a new groovy script file with a class structure

i. Considering the class ScriptLibrary that contain a method named printTestDetails as in following code in it:

class ScriptLibrary  {

    def context
    def testRunner
    def log

    def printTestDetails(def PrintThisToo) {
        log.info 'Name of the test case is :'+testRunner.testCase.name
        log.info 'Name of the test suite is : '+testRunner.testCase.testSuite.name
        log.info PrintThisToo
    }
}

ii. Save the file with class name, ScriptLibrary.groovy in this case

Step 2: Compile the code

i. Open command prompt and fire following command (from the folder where where your .groovy file is kept)

Compile the code:

groovyc -d classes SimplePrint.groovy

Step 3: Create the jar file

i. After compiling the code we can create the jar Create jar file:

jar cvf SimplePrint.jar -C classes .

Step 4: Deploy the jar file at SoapUI_Home/bin/ext folder

Step 5: Restart the SoapUI if was already open

Step 6: Create the object of class and use the methods anywhere in the SoapUI projects

i. Creating object

def scripts = new ScriptLibrary(context:context, log:log, testRunner:testRunner)

ii. Calling methods

scripts.printTestDetails(“This is my argument”)

I hope this solves your problem over all, this approach will allow you to freely use the code any where in the SoapUI and most importantly will solve your problem for getting context, log and testrunner in external code

You can also use any IDE of your choice to create code library and work on same to compile and create jar as well.

Let me know if you have any doubts or need more clarification

查看更多
时光不老,我们不散
3楼-- · 2019-03-19 06:47

This should do it

 context.setProperty("Assertions", new Assertions());


class Assertions{

    def printAssertion( tStep){

        def assertions = tStep.getAssertionList()

        //Loop on assertions
        assertions.each{
            log.info(it.name +  ' --> ' + it.status)
        }
    }

}

and call it like this

    TestStep=testRunner.testCase.testSuite.getTestCaseByName("yourTestCase").getTestStepByName("stepName")

    context.Assertions.printAssertion(TestStep)
查看更多
Viruses.
4楼-- · 2019-03-19 06:58

For Assertion :

Put this script in Repository

context.setProperty("Assertions", new Assertions());   
    class Assertions{    
    def printAssertion(tStep){    
        def assertions = tStep.getAssertionList()    
        //Loop on assertions           
    }    
}

Use this Script in SoapUI:

TestStep=testRunner.testCase.testSuite.getTestCaseByName("addTestCase").getTestStepByName("add")

    //context.Assertions.printAssertion(TestStep)

scripts = testRunner.testCase.testSuite.project.testSuites["ScriptLibrary"];
scripts.testCases["Demo"].testSteps["TestAssertion"].run(testRunner, context);

context.Assertions.printAssertion(TestStep).each{
     log.info(it.name +  ' --> ' + it.status)
    }

return null
查看更多
登录 后发表回答