Forum Discussion

MikeDally's avatar
MikeDally
Contributor
10 years ago

How do I clone an existing Groovy test step and insert it into another test case?

Hi there,

 

I want to dynamically build a SoapUI test structure so that my team can significantly reduce their prep time and get straight into testing. In one of my previous posts I was able to generate test cases and insert rest requests.

 

I want to also add in a groovy step for each of my test cases, which will handle the generation of test data.

 

Can anyone please advise on how I copy existing Groovy steps and put them into a target test case?

  • Hi Mike,

     

    Where you say clone a Groovy TestStep, following on from your excellent work before, would it be acceptable to your needs to rather than clone, create new Groovy TestSteps for each of your scripted TestCases?

     

    What I mean is say you had a TestCase object testCase, then what do you think about doing something like:

     

    testCase.addTestStep("groovy", "HelloGroovy").setScript("log.info 'Hello!'")

     

    ?

     

    That way you could effectively script in all the Groovy steps you need and supply custom scripts if necessary.

     

    Cheers,

    Rup

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi Mike,

     

    Where you say clone a Groovy TestStep, following on from your excellent work before, would it be acceptable to your needs to rather than clone, create new Groovy TestSteps for each of your scripted TestCases?

     

    What I mean is say you had a TestCase object testCase, then what do you think about doing something like:

     

    testCase.addTestStep("groovy", "HelloGroovy").setScript("log.info 'Hello!'")

     

    ?

     

    That way you could effectively script in all the Groovy steps you need and supply custom scripts if necessary.

     

    Cheers,

    Rup

    • MikeDally's avatar
      MikeDally
      Contributor

      Rupert, you are the MAN! This does exactly what I need it to :D

       

      Many thanks,

       

      Mike

      • MikeDally's avatar
        MikeDally
        Contributor

        This all feels very progressive! It's the first time I've tried something like this, but I'm glad I did!

         

        Anyway, as per your suggestion, I now generate a new Groovy script step. I'm using a String method to return the specific script I want, and then calling that as a parameter in my script creation step, a la:

         

        import com.eviware.soapui.impl.wsdl.teststeps.registry.GroovyScriptStepFactory
        import com.eviware.soapui.impl.rest.RestResource;
        import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory
        import com.eviware.soapui.config.TestStepConfig
        import com.eviware.soapui.impl.rest.RestRequest;
        import com.eviware.soapui.impl.rest.RestService;
        listOfTestCases = [
        'Address Lookup with valid PostCode',
        'Address Lookup with Valid CompanyName',
        'Address Lookup with Valid Street',
        'Address Lookup with Valid town',
        'Address Lookup with Valid CompanyName & Postcode',
        'Address Lookup with Valid Street & Postcode',
        'Address Lookup with Valid Town & PostCode',
        'Address Lookup with Valid CompanyName & Street',
        'Address Lookup with Valid CompanyName & Town',	
        'Address Lookup with Valid Street & Town',	
        'Address Lookup with Valid Postcode, CompanyName, Street & Town']
        for (String testCase : listOfTestCases) {
           tc = context.testCase.testSuite.addNewTestCase(testCase)
           tc.addProperty("testcycl-id") 
           tc.addProperty("cycle-id") 
           tc.addProperty("test-id") 
           tc.addProperty("run-id") 
           tc.addProperty("test-config-id")
           tc.addProperty("test-step-id")
           tc.addTestStep("groovy", "Set request headers").setScript(requestHeaderMethodText())
        
        // ... other code here
         }
        }
        
        // Store my groovy script as a String so it can be called in the test //generation script.
        def String requestHeaderMethodText(){
        	return "import java.net.InetAddress;" + " \n" +
            "def currentTimeInMillis = System.currentTimeMillis().toString()" + " \n" +
            "ipAddress = InetAddress.getLocalHost();" + " \n" +
            "testRunner.testCase.setPropertyValue( 'cleId', currentTimeInMillis)" + " \n" +
            "testRunner.testCase.setPropertyValue( 'userIp', ipAddress.getHostAddress().toString())" + " \n"
        	}