Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating script library in SoapUI free version

Tags:

groovy

soapui

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

like image 829
NewBee Avatar asked Feb 11 '16 05:02

NewBee


People also ask

How do I add a script to SoapUI?

Create and Test a Groovy Script in a SoapUI Tool Step 1: Select the CalculatorSoap TestSuite and then select the Test Case in which we are going to create the Groovy script. Right-click on the Test Steps, and then go to the Add Step to select the Groovy Script from the available service, as shown below.

How do I enable JavaScript in SoapUI?

To switch a project to JavaScript, click on the project, travel to the window in the bottom left hand corner. Select the script language field and update it to JavaScript.

Which scripting tool supports SoapUI?

SoapUI provides extensive options for scripting, using either Groovy or Javascript (since SoapUI 3.0) as its scripting language, which to use is set a the project level in the project details tab at the bottom left.

What is setup script in SoapUI?

Initial Setup Script : soapUI initial setup is just a place holder/ an editor for writing your initial script before start your testing. This setup is available in two levels, TestSuite level and testCase level.


1 Answers

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

like image 64
Samarth Avatar answered Oct 10 '22 14:10

Samarth