Forum Discussion

pankajmalinda's avatar
pankajmalinda
Occasional Contributor
6 years ago

Groovy Script to run SOAP test step 2 times

Hi HimanshuTayal 

 

I am trying to execute one SOAP Test step 2 times, I have below script but it seems that it's not working fine.

 

if( context.loopIndex == null )
context.loopIndex = 0
if( ++context.loopIndex < 3 )
testRunner.gotoStepByName( "AuthenticateAndGetUserDetails" )

 

  • Hi pankajmalinda ,

     

    If the only motive of 1st groovy is to execute SOAP Step 2times then you can use below code:

     

    def SOAPStep = testRunner.testCase.getTestStepByName("Test Step Name");
    
    for(i=0;i<2;i++){
    	SOAPStep.run(testRunner,context);
    }

    If yoy want to execute all below 3 steps then use below code:

     

    def SOAPStep = testRunner.testCase.getTestStepByName("Your Test Step Name");
    def groovyStep1 = testRunner.testCase.getTestStepByName("Your Test Step Name");
    def groovyStep2 = testRunner.testCase.getTestStepByName("Your Test Step Name");
    
    disableTestStep(SOAPStep, "false")
    disableTestStep(groovyStep1, "false")
    disableTestStep(groovyStep2, "false")
    
    for(i=0;i<2;i++){
    	SOAPStep.run(testRunner,context);
    	groovyStep1.run(testRunner,context);
    	groovyStep2.run(testRunner,context);
    }
    
    disableTestStep(SOAPStep, "true")
    disableTestStep(groovyStep1, "true")
    disableTestStep(groovyStep2, "true")
    
    
    def disableTestStep(testStep, disableFlag){
    	if(disableFlag.toLowerCase() == "true"){
    		if( testStep.disabled.toString().trim() == "false" ){
    			testStep.disabled = true
    		}
    	}
    	else{
    		if( testStep.disabled.toString().trim() == "true" ){
    			testStep.disabled = false
    		}
    	}	
    }
  • Radford's avatar
    Radford
    Super Contributor

    What do you mean when you say "but it seems that it's not working fine."? Are there any error messages?

     

    Have you tried adding some log file messages to help trace the flow of the code or tried debugging the Groovy script?

  • Hi pankajmalinda ,

     

    If the only motive of 1st groovy is to execute SOAP Step 2times then you can use below code:

     

    def SOAPStep = testRunner.testCase.getTestStepByName("Test Step Name");
    
    for(i=0;i<2;i++){
    	SOAPStep.run(testRunner,context);
    }

    If yoy want to execute all below 3 steps then use below code:

     

    def SOAPStep = testRunner.testCase.getTestStepByName("Your Test Step Name");
    def groovyStep1 = testRunner.testCase.getTestStepByName("Your Test Step Name");
    def groovyStep2 = testRunner.testCase.getTestStepByName("Your Test Step Name");
    
    disableTestStep(SOAPStep, "false")
    disableTestStep(groovyStep1, "false")
    disableTestStep(groovyStep2, "false")
    
    for(i=0;i<2;i++){
    	SOAPStep.run(testRunner,context);
    	groovyStep1.run(testRunner,context);
    	groovyStep2.run(testRunner,context);
    }
    
    disableTestStep(SOAPStep, "true")
    disableTestStep(groovyStep1, "true")
    disableTestStep(groovyStep2, "true")
    
    
    def disableTestStep(testStep, disableFlag){
    	if(disableFlag.toLowerCase() == "true"){
    		if( testStep.disabled.toString().trim() == "false" ){
    			testStep.disabled = true
    		}
    	}
    	else{
    		if( testStep.disabled.toString().trim() == "true" ){
    			testStep.disabled = false
    		}
    	}	
    }
    • TanyaYatskovska's avatar
      TanyaYatskovska
      SmartBear Alumni (Retired)

      Thanks for your help, Community.

      pankajmalinda, do the suggestions help you resolve the issue? Please accept the best answer as a solution

  • gotoStepByName does not actually run the test step. It tells testRunner what step to run next. AuthenticateAndGetUserDetails is already after the groovy script so that is why it does not run twice. If all you need that groovy script to do is run the test step twice you can remove the if statements and do it with a single line. Just use runTestStepByName(String name) and that will execute the step one time. After the groovy script is finished the test will continue onto the next step and your request will be sent a second time.