1

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.

1 Answers1

1

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.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks a lot! This works brilliantly, with the temporary file. – Cosmin Cîrstea Jan 12 '23 at 15:50
  • it seems to somehow transform my xml into a json. Am I doing something wrong? ` * def data = read('xmlFile.xml') * set data //fieldName = 'stuff to modify' * def file = karate.write(data, 'temp.xml')` – Cosmin Cîrstea Jan 12 '23 at 16:11
  • 1
    @CosminCîrstea read this: https://github.com/karatelabs/karate#type-conversion - so this should work: `* xmlstring dataString = data` and then save that – Peter Thomas Jan 13 '23 at 02:13
  • 1
    Thanks, that is what I needed. You are right, I did not understand type conversion too well initially, but I took a more in depth look into it now. Everything works as expected. Thank you very much for the support and the great stuff in Karate! – Cosmin Cîrstea Jan 13 '23 at 07:28