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.