Evaluating Kotlin Scripts from Kotlin

Hi there,

We are building a Kotlin-based DSL for writing automated, WebDriver based tests for our web application. The goal is to allow smart non-developers to easily write lots of tests.

Using Kotlin script files (.kts), we’ve managed to arrive at a DSL that is simple enough to gain a bit of traction with our QA team, with a nice development workflow using IntelliJ. Our analysts can write .kts files that look like this:

import com.acme.test

test {
    setup {
        database("Case Registration") {
            form("Case") {
                field("Name")
            }
        }
    }
    login()
    clickListItem("Case Registration")
    clickButton("Available offline")
    waitForToastContaining("downloaded and ready to use offline")
}

They just right click in IntelliJ and run, and the test function takes care of starting ChromeDriver, running the block, and reporting errors in a readable fashion.

What I’m trying to accomplish now is to combine all these tests scripts into a suite that can run on our CI servers, etc. For this, I need to at a minimum enumerate all the .kts files and execute them. Ideally, execute them multiple times against different browsers, etc.

I have tried to create an instance of KotlinJsr223JvmLocalScriptEngineFactory and evaluate the scripts, but the evaluator does not seem to inherit the current classpath and cannot resolve any of the functions from the rest of the project: if I debug I see that the templateClasspath contains only kotlin-stdlib.jar and kotlin-script-runtime.jar.

Ideally I would like to compile the scripts to a class, load that class, and then execute as needed.

Can someone point me to an example of this? I’ve looked at GenericReplCompilingEvaluator but cannot figure out how to configure it. What about using KotlinToJVMBytecodeCompiler.compileScript() ?

Any pointers would be appreciated.