Forum Discussion

US_20's avatar
US_20
Occasional Contributor
6 years ago

How to make sure a Groovy script runs for every test step

Hi, 

 

How do I make sure that a particular Groovy script runs for every test step? Say I have a Tescase A with n number of steps and Groovy script is a test step part of Test case. And where should I add it inside the groovy script?

  • groovyguy's avatar
    groovyguy
    Community Hero

    The easiest place to put it might be as a groovy script assertion on whichever test step comes last to check. Are you using a test case like this?

     

    1. REST Request 1
    2. JDBC Request 1
    3. Check results of request-1 here.
    4. REST Request 2
    5. JDBC Request 2
    6. Check results of request-2 here.
    7. etc.

     

    If that's the case, if you can put a groovy script assertion on whichever step comes last, that would be ideal. Or, if you can provide a screenshot of your test case/steps as they exist now, that might give us a better idea of what you want. Mock it up with sample steps if needed, but how do you envision it working?

     

    As far as I know, you'll have to re-use this groovy script every time you want to check a set of two test steps with each other. Either as a script assertion or as a groovy script test step.

    • US_20's avatar
      US_20
      Occasional Contributor

      Hi, 

      Thanks again. Below is a sample of my test case.

       

      Test case name
      Sku
      soapreq1
      soapreq2
      Restreq1--returns the morphonly amt ( can be a +ve value or -ve value)
      soapre3--- where the Order total value in the groovy script is asserted
      RestReq2
      JDBCreq1
      JDBCreq2
      Groovy script
      Datasink
      Datasourceloop


      Groovy script:

      Get the morphonly amount from restreq1
      def morphOnlyrestResponse = getdata

      //Get the total amount from soapre1
      def searchOrdersoapresponse = getdata


      def ordertotal = new JsonSlurper().parseText(searchOrdersoapresponse);
      log.info("ordertotal = "+ordertotal);
      def morphonly = new JsonSlurper().parseText(morphOnlyrestResponse);
      log.info("morphonly = "+morphonly);

      if (ordertotal > 0)
      {

      assert (ordertotal == morphonly)
      log.info("Assertion positive Pass");
      }

      Questions:
      So I want to make sure the above groovy script runs for every row in the Sku(data sheet). And my assumtion is if the order total is 0, then the groovy script will still execute as a test step?

      I tried including def tc = testRunner.testCase.testSteps in my groovy script in the beginning. But when the order total was 0. i checkeed in the trasaction log, TestStepResult: Step[Groovy script] ran with assertion status [PASS]. Not sure if this is a correct way of doing it? Please advice.

      • US_20's avatar
        US_20
        Occasional Contributor

        Can anyone please help?

    • US_20's avatar
      US_20
      Occasional Contributor

      Hi, Thanks again. Below is a sample of my test case. Test case name Sku soapreq1 soapreq2 Restreq1--returns the morphonly amt ( can be a +ve value or -ve value) soapre3--- where the Order total value in the groovy script is asserted RestReq2 JDBCreq1 JDBCreq2 Groovy script Datasink Datasourceloop Groovy script: Get the morphonly amount from restreq1 def morphOnlyrestResponse = getdata //Get the total amount from soapre1 def searchOrdersoapresponse = getdata def ordertotal = new JsonSlurper().parseText(searchOrdersoapresponse); log.info("ordertotal = "+ordertotal); def morphonly = new JsonSlurper().parseText(morphOnlyrestResponse); log.info("morphonly = "+morphonly); if (ordertotal > 0) { assert (ordertotal == morphonly) log.info("Assertion positive Pass"); } Questions: So I want to make sure the above groovy script runs for every row in the Sku(data sheet). And my assumtion is if the order total is 0, then the groovy script will still execute as a test step? I tried including def tc = testRunner.testCase.testSteps in my groovy script in the beginning. But when the order total was 0. i checkeed in the trasaction log, TestStepResult: Step[Groovy script] ran with assertion status [PASS]. Not sure if this is a correct way of doing it? Please advice.

  • nmrao's avatar
    nmrao
    Champion Level 3
    Use case lacks of clarity.

    "A groovy script runs for every test step" - What does this script contains? Please add more context or code snippet. It is difficult to image and answer.