Forum Discussion

enliven's avatar
enliven
Contributor
15 years ago

How to access log, context, etc.. from a Class in groovy?

Hi,

Im trying to create some scripts to generate my test-suites and have some custom classes which I need to be able to access log from (to help with debugging). Could someone tell me how pls?

Also,
Do the soapui plugings for eclipse or netbeans  provide better groovy support such as code hints, intellisense, etc?

Sincerly,
a semi-frustrated .net developer

3 Replies

  • btw, Im just looking for a 'leg up' to code more efficiently. Im not going to be bothering you with how to write java code... here is what Ive accomplished so far in the soapui editor:


    def static groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def static baseSuiteName = "zTestSuite-"
    testRunner.testCase.testSuite.project.testSuites.each{
    if(it.value.name.contains(baseSuiteName)){
    testRunner.testCase.testSuite.project.removeTestSuite(it.value)
    }
    }
    BuildSuites(baseSuiteName, log, context, testRunner)
    def BuildSuites(baseSuiteName, log, context, testRunner){
    def testSuiteMap = [:]
    for(iface in testRunner.testCase.testSuite.project.getInterfaceList())
    {
    log.info(iface.getName())
    def DTTestSuite testSuite
    for(op in iface.getOperationList())
    {
    def opName = op.getName()
    def names =  opName.split('_')
    def suiteName = baseSuiteName + names[0]
    def caseName = names[0] + "CRUD"
    def stepName = names[1]
    if(testSuiteMap[suiteName] == null){
    testSuite = new DTTestSuite(log, suiteName)
    testSuiteMap.put(suiteName, (testSuite))
    //log.info("TestSuite: " + testSuiteMap.toMapString())
    }
    if(testSuiteMap[suiteName].TestCaseMap[caseName] == null){
    testCase = new TestCase(log, caseName)
    testSuiteMap[suiteName].AddCase(caseName, testCase)
    }
    def step = new TestStep(log, stepName, "r", op)
    testSuiteMap[suiteName].TestCaseMap[caseName].AddStep(stepName, step)
    }
    log.info("TestSuites: " + testSuiteMap.toMapString())
    testSuiteMap.each{
    log.info("Creating: " + it.value.name)
    it.value.Create(testRunner)
    log.info("Created: " + it.value.name)
    }
    }
    }

    class DTTestSuite
    {
    def name, log
    def HashMap Specifics
    def HashMap TestCaseMap
    public DTTestSuite(log, name){
    this.log = log
    this.name = name
    this.TestCaseMap = [:]
    this.Specifics = [:]
    }
    def AddCase(caseName, theCase){
    this.TestCaseMap.put(caseName, (theCase))
    }
    def Create(testRunner){
    log.info("Adding SUITE: " + name)
    def wsdlTestSuite = testRunner.testCase.testSuite.project.addNewTestSuite( this.name )
    assert wsdlTestSuite != null
    TestCaseMap.each{it.value.Create(wsdlTestSuite)}
    }
    String toString(){
    return "SUITE: " + name + ", " + TestCaseMap.toMapString()  + Specifics.toMapString()
    }
    }

    class TestCase
    {
    def log, name
    def HashMap Specifics
    def HashMap TestStepMap
    public TestCase(log, name){
    this.log = log
    this.name = name
    this.Specifics = [:]
    }
    def AddStep(stepName, theStep){
    if(TestStepMap == null)
    TestStepMap = [:]
    this.TestStepMap.put(stepName, (theStep))
    }
    def Create(wsdlTestSuite){
    log.info("Adding CASE: " + name)
    assert wsdlTestSuite != null
    def wsdlTestCase = wsdlTestSuite.addNewTestCase( this.name )
    assert wsdlTestCase != null
    TestStepMap.each{it.value.Create(wsdlTestCase)}
    }
    String toString(){
    return "CASE: " + name + ": " + TestStepMap.toMapString() + Specifics.toMapString()
    }
    }

    class TestStep
    {
    def name, log
    def type
    def HashMap Specifics
    public TestStep(log, name, type, op){
    this.log = log
    assert name != null
    this.name = name
    assert type != null
    this.type = type
    assert op != null
    //log.info(op.getRequestCount() + ", " + op.getReqeustList)
    this.Specifics = ["operation":op]
    }
    static def ASSERT_SCRIPT_RequiestId = 'def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) \n
    def req = groovyUtils.getXmlHolder( messageExchange.requestContent  ) \n
    def res = groovyUtils.getXmlHolder( messageExchange.responseContent  ) \n
    req.namespaces["ns"] = "http://fwapi.DriverTech.com/CommonTypes/2009/04" \n
    res.namespaces["ns"] = "http://fwapi.DriverTech.com/CommonTypes/2009/04" \n
    def reqid = req["//ns:RequestHeader/ns:RequestId"] \n
    def resid = res["//ns:ResponseHeader/ns:RequestId"] \n
    assert reqid == resid, "RequestID differs"'
    def Create(wsdlTestCase){
    log.info("Adding STEP: " + name)
    //Create TestStep
    assert wsdlTestCase != null
    def wsdlTestStep = com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory.createConfig(Specifics["operation"], name);
    assert wsdlTestStep != null
    //Perform custom mods to the request
    //def wsdlRequest = wsdlTestStep.getRequestByName(Specifics["operation"])

    wsdlTestStep = wsdlTestCase.addTestStep(wsdlTestStep)
    assert wsdlTestStep != null
    //Create Assertions
    wsdlTestStep.addAssertion( "Schema Compliance" )
    wsdlTestStep.addAssertion( "SOAP Response" )
    wsdlTestStep.addAssertion( "Not SOAP Fault" )
    def ass = wsdlTestStep.addAssertion( "Script Assertion" )
    ass.setScriptText(ASSERT_SCRIPT_RequiestId)
    ass.name = "RequestId are Equal"
    }
    String toString(){
    return "STEP: " + name + ": " + Specifics.toMapString()
    }
    }
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    The soapUI plugins for the IDEs currently use the same groovy editor as the standalone version, but if you put your scripts in the soapUI Pro script library you can edit them in eclipse/netbeans groovy editors.

    Regarding access to log, context, etc.. you will have to pass these as arguments to the classes/methods you are calling. Also, you can access the global soapUI logs through com.eviware.soapui.SoapUI.getLogMonitor() and associated methods on the returned object.

    Hope this helps!

    regards,

    /Ole
    eviware.com
  • what IDE and plugin would you recommend for editing Groovy on the basis of:
    1) Auto completion
    2) Syntax Highlighting
    3) Refactoring

    Also, could you pls provide me a list of imports to start with?