Forum Discussion

davebeattie's avatar
davebeattie
New Contributor
5 years ago
Solved

How to use external groovy script to save code repetition in setup/teardown scrips

Hi all,

 

Am new to ReadyAPI and just trying to get head around groovy scripting. 

 

I have a lot of duplication in my test suites - mostly on setup/tear down methods and would like to be able to refactor into a reusable external script, which I can call from each test case.

 

Currently script is being used to pull in an external xml data file as a request for each test case (some xml are too large to save with project)

:

def suiteName = testRunner.testCase.testSuite.name
def testcaseName = testRunner.testCase.name

//use suite name to get path to xml files
log.info("suite name: " + suiteName)
def splitName = suiteName.split("_");
def declarationOrInvoice = splitName[0]
def type = splitName[1]

//URL of data files on server(Utilities is an external file - just holding constant values)
def baseURL = new Utilities().getBaseUrl()


def fileName = baseURL + declarationOrInvoice + "\\" + type + "\\" + testcaseName + ".xml"
log.info(fileName)

//get file and add to project property
def myRequest = new File(fileName).getText()
testRunner.testCase.testSuite.project.setPropertyValue("request", myRequest)

 

This is copied/pasted across multiple test cases. Is there a way I could move this to an external script, so if I need to make a change, am only changing it in 1 place?

 

I am aware that you can initialise scripts with a testrunner/log/context etc, just not really sure how! 

If I have an external script, how would it know what test case is calling it?

 

Many thanks in advance for any advice

 

Dave

  • Hi davebeattie ,

    I think it's worth doing a script, your script is too simple for that. It consists mainly of variable declarations. Have you considered the possibility to reuse a test case in another test case? In ReadyAPI exist a test step that is called run test case. I have already used it, but not to read out a file.

     

    What I can also recommend to you is to deal with the events and event handlers. With events it is possible to manage code centrally and use it in many or all test cases.

4 Replies

  • Hi davebeattie ,

    I think it's worth doing a script, your script is too simple for that. It consists mainly of variable declarations. Have you considered the possibility to reuse a test case in another test case? In ReadyAPI exist a test step that is called run test case. I have already used it, but not to read out a file.

     

    What I can also recommend to you is to deal with the events and event handlers. With events it is possible to manage code centrally and use it in many or all test cases.

    • HimanshuTayal's avatar
      HimanshuTayal
      Community Hero

      giovanni_favara : Yes you can use the Setup script to do this task but also what you can do is :

       

      1. create one Test Suite with the name of Test Configuration and under that

      2. Create a Test Case named Configuration script and uder that

      3. Create one Groovy Test Step which will contain your groovy code to setup these configuration file.

       

      Refer my attached sample project for the same.

       

  • There are a few options depending on your needs:

     

    1. Move your script to the Setup/Tear down tab of your TestSuite. This option would make the script run before/after any test within a specific suite

     

    2. Move your script to an Event Handler. It will work for multiple suites in a project. If your code truely needs to be run before each testcase/execution an event like TestRunListener.beforeRun or TestSuiteRunListener.beforeTestCase might work for you. 

     

    3. Call your code using the Run TestCase Step. In the past I've made an 'Action Library' Test Suite that contains common actions I need and call them using this step.

     

    4. Move your code to a Script Library. Then you could call it from the desired Set up tab(s). This option would work across multiple projects.