Great timing. We very recently did some work for CLI testing which I am sure you can use effectively. Here is a thread on Twitter: https://twitter.com/maxandersen/status/1276431309276151814
And we have just released version 0.9.6.RC4 and new we have a new karate.fork()
option that returns an instance of Command
on which you can call exitCode
Here's an example:
* def proc = karate.fork('script.sh arg1')
* proc.waitSync()
* match proc.exitCode == 0
You can get more ideas here: https://github.com/intuit/karate/issues/1191#issuecomment-650087023
Note that the argument to karate.fork()
can take multiple forms. If you are using karate.exec()
(which will block until the process completes) the same arguments work.
- string - full command line as seen above
- string array - e.g.
['script.sh', 'arg1']
- json where the keys can be
line
- string (OR)
args
- string array
env
- optional environment properties (as JSON)
redirectErrorStream
- boolean, true by default which means Sys.err appears in Sys.out
workingDir
- working directory
useShell
- default false, auto-prepend cmd /c
or sh -c
depending on OS
And since karate.fork()
is async, you need to call waitSync()
if needed as in the example above.
Do provide feedback and we can tweak further if needed.
EDIT: here's a very advanced example that shows how to listen to the process output / log, collect the log, and conditionally exit: fork-listener.feature
Another answer which can be a useful reference: Conditional match based on OS
And here's how to use cURL for advanced HTTP tests ! https://stackoverflow.com/a/73230200/143475
In case you need to do a lot of local file manipulation, you can use the karate.toJavaFile()
utility so you can convert a relative path or a "prefixed" path to an absolute path.
* def file = karate.toJavaFile('classpath:some/file.txt')
* def path = file.getPath()