Home > Software engineering >  How can I dynamically modify a file before passing it to karate.toJavaFile?
How can I dynamically modify a file before passing it to karate.toJavaFile?

Time:01-13

Seeing that karate.toJavaFile expects a path, is there any way I can modify a base file before providing it as parameter to the method?

For example, I have this xml in a file:

<object> <id>123</id> <name>Bob</name> </object>

Thing is, I need to replace the 123 in the id field with a real id which I get from another GET request before providing this file to the karate.toJavaFile. I'm unfortunately bounded by a Java method.

I tried reading the xml into a variable then modifying its content with * set, but that doesn't help since I need to provide a path and not a variable to karate.toJavaFile.

CodePudding user response:

First I would try to achieve what you eventually want to do without writing any file. As explained here: https://stackoverflow.com/a/54593057/143475

But you should be able to do this by saving a temp file:

* def data = <foo>bar</foo>
* set data/bar = 'baz'
* def file = karate.write(data, 'temp.xml')

Won't that work ? karate.write() returns a java.io.File instance.

  • Related