WebTau can run commands in foreground and in background . When command is ran in background WebTau will continue execution, leaving command to run its course.To run a command in a background use cli.runInBackground with a single string parameter that includes a command and all its arguments: cli.runInBackground('scripts/sleeps') println "process above may still be running"
Use .stop to explicitly terminate the running background command def command = cli.runInBackground('scripts/sleeps') println "some commands that assume running process" command.stop()
We are going to test a "hello world" script that asks you for a name and greet you: #!/bin/bash echo "enter your name" read name echo "hello $name" Use .send to send input to a running command. Use .output.waitTo to wait for command to rich a certain state: def helloWorld = cli.runInBackground("scripts/hello-world") helloWorld.output.waitTo contain("enter your name") helloWorld.send("webtau\n") helloWorld.output.waitTo contain("hello webtau") helloWorld.stop() You can use << instead of .send for extra syntax sugar: def helloWorld = cli.runInBackground("scripts/hello-world") helloWorld.output.waitTo contain("enter your name") helloWorld << "webtau\n" helloWorld.output.waitTo contain("hello webtau") helloWorld.stop()
By default, WebTau waitTo waits 5 seconds for a condition to become true. This value can be changed either globally by using config value groovy waitTimeout = 20000 To override wait timeout locally, use command.output.waitTo(contain("line two"), 20_000)
Use cli.workingDir as a second parameter to cli.runInBackground to set a working dir: def command = cli.runInBackground('./listing', cli.workingDir('scripts')) command.output.waitTo contain('listing files') command.output.waitTo contain('sleeps') command.stop()
Use cli.env as a second parameter to cli.runInBackground to set the environment variables: #!/bin/bash echo "hello $MY_VAR" def command = cli.runInBackground('scripts/hello-env-var', cli.env([MY_VAR: 'webtau'])) command.output.waitTo contain('hello webtau') command.stop()
Common Environment Variables
Set cliEnv config value with environment values that needs to be passed to each cli.runInBackground : cliEnv { MY_VAR = "webtau" PREFIX_VAR = "__" } def command = cli.runInBackground('scripts/hello-env-var') command.output.waitTo contain('hello webtau') command.stop()
Combine configs by using cli.env(...).workingDir(...) in any order to set both: def command = cli.runInBackground('./hello-env-var', cli.workingDir('scripts').env([MY_VAR: 'webtau'])) command.output.waitTo contain('hello webtau') command.stop()
To specify PATH to use for CLI commands lookup use cliPath = ['scripts/more']