cancel
Showing results for 
Search instead for 
Did you mean: 

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

Occasional Contributor

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?

6 REPLIES 6
Highlighted
Community Hero

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

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.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Community Hero

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

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.


Regards,
Rao.
Occasional Contributor

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

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.

Occasional Contributor

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

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.

Occasional Contributor

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

Can anyone please help?

Community Hero

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

Hi,

I might be misunderstanding the problem here, but is it not what either @msiadak or @nmrao said in a previous post? I.e. add the groovy as a script assertion on the relevant test step?

So if you sku datasource is pointing to a spreadsheet and you want the groovy to execute for each row, essentially each row of the datasheet equates to the rest or soap step that sources the data from the spreadsheet. Just add the groovy and tailor it if necessary so it works as a script assertion on that particular rest or soap step?

Or have i misunderstood?

Cheers,

Richie
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
New Here?
Join us and watch the welcome video:
SeptemberNews
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win