Forum Discussion

roryLIT's avatar
roryLIT
Contributor
11 years ago

Creating a Groovy step using GroovyScript

I'm creating a groovy step in my testcase using the below groovyscript. However when I try to run the below script it brings up an error: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 5: unexpected char: '#' @ line 5, column 259. uniqueId = context.expand( '${#TestCase# ^ org.codehaus.groovy.syntax.SyntaxException: unexpected char: '#' @ line 5, column

def tc1 = testRunner.testCase.testSuite.project.testSuites["TestCaseScenarios"].testCases[tc];
gs = tc1.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "rerun/cleanup" )
gs.properties["script"].value = 'def response = testRunner.testCase.testSteps["createClaim"].testRequest.response.contentAsString \nif (response.contains("Error processing query") == true) \n{ \ntestRunner.gotoStep(0) \n} \ndef uniqueId = context.expand( '${#TestCase#testCase}' )


Is there a way for it to recognise the # as I need to use it

Thanks!

4 Replies

  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    Hi Rory, nice little script you have there. The issue with your script is that your quotes inside of the context.expand are not escaped and thus your current groovy script is trying to handle them. In order to fix this issue, either escape the quotes or switch them. I switched them.


    import com.eviware.soapui.impl.wsdl.teststeps.registry.GroovyScriptStepFactory
    def tc1 = testRunner.testCase;
    gs = tc1.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "rerun/cleanup" )
    gs.properties["script"].value = 'def response = testRunner.testCase.testSteps["createClaim"].testRequest.response.contentAsString \nif (response.contains("Error processing query") == true) \n{ \ntestRunner.gotoStep(0) \n} \ndef uniqueId = context.expand( "${#TestCase#testCase}" )';

    and that worked for me.


    I did have a few suggestions for the script though:

    1) Have it check if the TC exists, if this is being ran from command line than no big deal here.
    2) Possibly having a teardown script to handle a lot of this would be useful.
    3) Have you ever looked into having groovy control the flow of your test cases?

    If you would like I could show you an example script where the test case never actually leaves your groovy script
  • Hi Paul,

    Sorry for delayed response!

    Thanks for your help, that did the trick

    I'm interested in your 3rd point:

    3) Have you ever looked into having groovy control the flow of your test cases?

    If you would like I could show you an example script where the test case never actually leaves your groovy script


    Also, I was wondering if it's possible to programmatically add a groovy step before the last step in the test. So currently it is added to the end of the test so it is the last step but I would like it to be the second-last step! Any ideas?
  • vajdaz's avatar
    vajdaz
    Occasional Contributor
    Could you explain why are you creating that step dynamically and not having it statically?
    I'm just wondering
  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    To put a step in the second before last test request ....

    If the test is always being added as the last step this should work:

    import com.eviware.soapui.impl.wsdl.teststeps.registry.GroovyScriptStepFactory
    def tc1 = testRunner.testCase;
    gs = tc1.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "rerun/cleanup" )
    [b]testRunner.testCase.moveTestStep(testRunner.testCase.getTestStepCount()-1,-1);[/b]
    gs.properties["script"].value = 'def response = testRunner.testCase.testSteps["createClaim"].testRequest.response.contentAsString \nif (response.contains("Error processing query") == true) \n{ \ntestRunner.gotoStep(0) \n} \ndef uniqueId = context.expand( "${#TestCase#testCase}" )';



    You could also use getTestStepIndexByName(String stepName) if you are unsure of where exactly it will be but the script above will only move it up 1 step.

    For my 3rd point, I have a post for a UI within SoapUI, it runs my test cases the way I want them ran. What I personally do with a lot of my test cases that I want ran multiple times is just iterate through the list of testcases using :

     for ( tests in testRunner.testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.class) ) {


    That will loop through the test steps of the given type (This being standard Soap requests) and assign each test to the variable "tests". As it loops through I grab the contents of the test case using:


    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context); //declared earlier in my script
    def holder = groovyUtils.getXmlHolder(testName+"#Request");


    holder then becomes the XML from the request that I was processing. I parse through the XML using regex looking for tags but if you wanted you could just use properties.

    After that my next step is transferring the assertions:


    /*
    * Stealing the assertions from the respective test steps and putting it into the Runner
    * test step
    */
    def testRan = testRunner.testCase.getTestStepByName(testName);
    def Assertions = testRan.getAssertions();
    def removeAssertions = testRunner.testCase.getTestStepByName("Runner").getAssertions();
    for (key in removeAssertions.keySet()) {
    testRunner.testCase.getTestStepByName("Runner").removeAssertion(removeAssertions.get(key));
    }
    for (assertion in Assertions.keySet()) {
    testRunner.testCase.getTestStepByName("Runner").cloneAssertion(Assertions.get(assertion),assertion);
    }


    And run the test step:


    testRunResultsXML = testRunner.runTestStepByName("Runner").getResponse();
    testRunResults = groovyUtils.getXmlHolder("Runner#Response");
    testPrint = testRunResults.getPrettyXml();


    It's really simple once you get used to it essentially just 1 Soap Request is ever ran. The main problem is editing the XML when ran.