0

I am trying to create a script library and compile a groovy class as described in https://stackoverflow.com/a/35498212/9997207

Groovy Script

import groovy.json.*
import com.eviware.soapui.*

class ScriptLibrary  {

    def context
    def testRunner
    def log

def boolean setHeaderValues(userId, password){
try{
loginTestStep = testRunner.testCase.testSuite.testCases["SetHeaderParameters"].testSteps["SetHeaderParametersJsonRequest"]
def request = loginTestStep.getPropertyValue("Request").toString()
def jsonReq = new JsonSlurper().parseText(request);
def builder = new JsonBuilder(jsonReq)
builder.content.userId=userId
builder.content.password=password
def jsonReqAsString = JsonOutput.toJson(jsonReq)
loginTestStep.setPropertyValue("Request",jsonReqAsString)
def contextJsonRequest = new WsdlTestRunContext(loginTestStep);
loginTestStep.run(testRunner,contextJsonRequest)

def response = loginTestStep.getPropertyValue("Response").toString()
def jsonResponse = new JsonSlurper().parseText(response);
def accessTokenFromResponse = jsonResponse.accessToken.toString()
def userPermissionFromResponse = jsonResponse.userPermissionIds.toString()
def userIdFromResponse = jsonResponse.userId.toString()

testRunner.testCase.testSuite.project.setPropertyValue("HEADER_USER_ID", userIdFromResponse)
testRunner.testCase.testSuite.project.setPropertyValue("HEADER_USER_PERMISSION", userPermissionFromResponse)
testRunner.testCase.testSuite.project.setPropertyValue("HEADER_ACCESS_TOKEN", accessTokenFromResponse)

log.info "Header set with values "+userIdFromResponse+":::"+userPermissionFromResponse+":::"+accessTokenFromResponse
setHeader = true
}
return setHeader
}
catch (Exception ex) {
    log.info "Header Not Set  " +ex
    setHeader = false
    testRunner.testCase.testSuite.project.setPropertyValue("HEADER_USER_ID", "")
    testRunner.testCase.testSuite.project.setPropertyValue("HEADER_USER_PERMISSION", "")
    testRunner.testCase.testSuite.project.setPropertyValue("HEADER_ACCESS_TOKEN", "")
     return setHeader
    }
}
}

Getting following compilation error while trying to compile the groovy script from the command prompt

C:\Path\apache-groovy-binary-2.5.1\groovy-2.5.1\bin\GroovyScripts>groovy ScriptLibrary.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\Path\apache-groovy-binary-2.5.1\groovy-2.5.1\bin\GroovyScripts\ScriptLibrary.groovy: 20: unable to resolve class WsdlTestRunContext
 @ line 20, column 26.
   def contextJsonRequest = new WsdlTestRunContext(loginTestStep);
                            ^

1 error
Novice Coder
  • 105
  • 1
  • 6

1 Answers1

0

You have to include soupui-{version}.jar file in the classpath to resolve this dependency issue. You can find it in SoapUI-{version}/bin folder. Let's say that SoapUI 5.4.0 is located in /tmp/SoapUI-5.4.0/. In this case I can compile a script with following command:

groovyc -d classes -cp ".:/tmp/SoapUI-5.4.0/bin/soapui-5.4.0.jar" script.groovy

Keep in mind that this command is run from the folder where script.groovy is located and compiled class can be found in ./classes/script.class

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131