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.