callExternalScript() function

I have published a Katalon Studio project on GitHub as a proof of concept.


One day I was inspired by a Katalon Forum discussion and got a question to myself:

When I have a set of Groovy Scripts located outside a Katalon Studio project, is it possible for a TestCase script in the Katalon Studio project to load and execute these external scripts?

I wrote Test Cases/reinvent/main . It worked for me. This test case opens/closes the web sites of GAFA. Guys, please try running it.

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

Path findExternalScript(String scriptName) {
	Path project = Paths.get(RunConfiguration.getProjectDir())
	return findExternalScript(project, scriptName)
}
Path findExternalScript(Path basedir, String scriptName) {
	Path scripts = basedir.resolve('Include/externalScripts')
	Path script = scripts.resolve(scriptName)
	if (!Files.exists(script))
		throw new FileNotFoundException(script.toString())
	return script
}
List<Path> findExternalScripts() {
	Path project = Paths.get(RunConfiguration.getProjectDir())
	return findExternalScripts(project)
}
List<Path> findExternalScripts(Path basedir) {
	Path scripts = basedir.resolve('Include/externalScripts')
	return Files.walk(scripts)
		.filter { item -> Files.isRegularFile(item) && item.toString().endsWith('.groovy') }
		.collect();
}
//println findExternalScripts()

void callExternalScript(Path file) {
	WebUI.comment(">>> callin up ${file}")
	def shell = new GroovyShell()
	shell.evaluate(file.toFile())
}

WebUI.comment(">>> activating a single script")
callExternalScript(findExternalScript('FB.groovy'))

//
WebUI.comment(">>> activating all scrits")
for (Path f in findExternalScripts()) {
	callExternalScript(f)
}

Test Cases/reinvent/main implements a function callExternalScript(Path file) which can load and execute Groovy script located anywhere, even outside the Katalon Studio project. An instance of groovy.lang.GroovyShell does this magic.

The function callExternalScript(Path) looks similar to the KS-builtin WebUI.callTestCase() keyword. This is a re-invention. But it is interesting for me.

7 Likes

Me too. I like this a lot! :heart: :heart: :heart:

Very nice work! Works like a charm, but one thing which probably would be nice too is to run the scripts with a given Profile in the Project! If i got time I’ll look up if i could maybe integrate this for my tests!

1 Like

I believe you can make it.

@kazurayam I’ll be making good use of this. Thank you!

Minor change you might want to consider:

Passing args might be considered too, but I can live without them.

Thank you, Russ. I changed my code.

By the way, I added one more example: a testcase in a Katalon Studio project downloads a Groovy script from GitHub and immediately run it; the test case opens a browser window to navigate to https://www.amazon.com/ and close it.

It worked :blush:

// Execute scripts outside the project
WebUI.comment("download a script from GitHub and immediately run it")
String url = "https://raw.githubusercontent.com/kazurayam/ReinventTestCaseInvocation/master/Include/externalScripts/AMZN.groovy"
callExternalScript(new URL(url).getText())
2 Likes

image