简体   繁体   English

在将文件传递给 karate.toJavaFile 之前如何动态修改文件?

[英]How can I dynamically modify a file before passing it to karate.toJavaFile?

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?看到 karate.toJavaFile 需要一个路径,有没有什么办法可以在将它作为参数提供给方法之前修改一个基本文件?

For example, I have this xml in a file:例如,我在一个文件中有这个 xml:

<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.问题是,在将此文件提供给 karate.toJavaFile 之前,我需要将 id 字段中的 123 替换为我从另一个 GET 请求中获得的真实 id。 I'm unfortunately bounded by a Java method.不幸的是,我受限于 Java 方法。

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.我尝试将 xml 读入变量,然后使用 * set 修改其内容,但这无济于事,因为我需要提供路径而不是 karate.toJavaFile 的变量。

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如此处所述: 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. karate.write()返回一个java.io.File实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用 1 个参数在空手道功能文件中调用 js function - How can i call the js function in karate feature file with 1 argument Rails-在发出请求之前,如何修改http参数? - Rails - How can I modify the http params before the request is made? 如何动态创建和修改多个 SVG - How can I create and modify multiple SVGs dynamically 如何在返回之前修改此qr函数输出 - How can I modify this qr function output before returning 如何在作为道具传入之前从getter修改变量? - How to modify a variable from a getter before passing in as a prop? 我可以使用 webpack 挂钩在保存文件 output 之前对其进行修改吗? - Can I use a webpack hook to modify file output just before it gets saved? 发布文档之前,如何在find()中的对象级别上修改文档? - How can I modify the documents on an object-level in find() before I publish them? 如何编写JavaScript脚本来修改此CSS文件? - How can I write the javascript script to modify this css file? 如何从外部 Javascript 文件修改 HTML 元素? - How can I modify an HTML element from an external Javascript File? 如何以编程方式修改 PDF 文件中的 javascript 事件? - How can I modify a javascript event in a PDF file programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM