如何在空手道中使用 FileUtils?

how to use FileUtils in Karate?

需要将编码后的字符串转换生成PDF并保存为pdf进行比较。

特征文件示例:

Feature: Compute All  

Scenario: trial and error
* call read('common.feature')
* def xyz = getMDash( 'my password')
* print xyz
* call read('classpath:/test/java/com/intuit/karate/FileUtils.java')



* def doWork =
"""
function('temp.txt','hello world') {
  var JavaDemo = Java.type('com.intuit.karate.FileUtils');
  var jd = new JavaDemo();
  jd.writeToFile('temp.txt','hello world');  
}
"""

* def result = call writeToFile 'temp.txt','hello world'


* def FileUtils = Java.type('com.intuit.karate.FileUtils')
* def result = FileUtils.writeToFile('temp.txt','hello world')

使用给定的示例。

日志中的错误:

Tests run: 9, Failures: 0, Errors: 2, Skipped: 3, Time elapsed: 1.314 sec <<< FAILURE!
* def doWork =(Scenario: trial and error)  Time elapsed: 0 sec  <<< ERROR!
java.lang.RuntimeException: javascript evaluation failed: function('temp.txt','hello world') {
  var JavaDemo = Java.type('com.intuit.karate.FileUtils');
  var jd = new JavaDemo();
  jd.writeToFile('temp.txt','hello world');
}
at com.intuit.karate.ScriptBindings.eval(ScriptBindings.java:115)
at com.intuit.karate.ScriptBindings.updateBindingsAndEval(ScriptBindings.java:103)
at com.intuit.karate.ScriptBindings.evalInNashorn(ScriptBindings.java:88)
at com.intuit.karate.Script.evalJsExpression(Script.java:362)
at com.intuit.karate.Script.evalKarateExpression(Script.java:284)
at com.intuit.karate.Script.evalKarateExpression(Script.java:170)
at com.intuit.karate.Script.assign(Script.java:598)
at com.intuit.karate.Script.assign(Script.java:524)
at com.intuit.karate.StepDefs.def(StepDefs.java:305)
at com.intuit.karate.StepDefs.defDocString(StepDefs.java:300)
at ?.* def doWork =(testSuite/users/dummy.feature:11)
**Caused by: javax.script.ScriptException**: <eval>:1:10 Expected ident but found temp.txt
function('temp.txt','hello world')

需要有关在空手道中使用 fileUtils 的帮助。

空手道不支持写入文件,因为这是不好的做法。我不知道你在上面想做什么。通常您通过将响应保存在内存中来进行比较,请参考文件上传示例:https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/upload/upload.feature

同样的例子还有一个使用自定义的例子Javaclass。如果您确实需要向文件写入内容,请使用上述代码编写您自己的实用程序并使用 com.intuit.karate.demo.util.FileChecker 作为参考。