karate.eval fails if there is nothing for it to do
See original GitHub issueRan into this trying to use def
variables in Examples:
tables for scenario outlines.
It seems that if karate.eval()
has nothing to actually do in the passed value, it will throw an exception:
org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (eval) on com.intuit.karate.core.ScenarioBridge@4f4c789f failed due to: Cannot convert ‘…’(language: Java, type: com.intuit.karate.graal.JsMap) to Java type ‘java.lang.String’: Invalid or lossy primitive coercion.
The issue seems to arise when passing a JSON object that does not contains variables to expand (at least).
In my case I wanted to put variables (defined, say, in Background) in a JSON object in the examples table, for use in populating scenario values in the feature (in practice this would not be a fixed value, but, say, a timestamp):
Background: test
* def value = 123
Scenario Outline: scenario
* karate.set(karate.eval(example))
* print foo
* print bar
Examples:
| example! |
| {foo: value, bar: 0} |
| {foo: 0, bar: value} |
| {foo: 0, bar: 0} |
The ! in the examples column header makes sure Karate sees the value as a JSON object, but it seems that examples tables simply do not expand variables within them (I also tried “#(…)” notation). So, the karate.eval()
within the scenario re-parses the object to expand the variables (here ‘value
’) in the object.
The first two examples work fine. In both cases, ‘value
’ is replaced with its assigned number, resulting in, respectively:
- {foo: 123, bar: 0}
- {foo: 0, bar: 123}
However, in the last example, it blows up with the exception mentioned above. The same values used in the other examples work, but it seems when there is nothing to expand, the eval fails.
The workaround I ended up doing is, amusingly, creating a def
called “zero” in my Background, assigned with 0, and using the var name “zero” instead of 0 in at least one of the places in the third example. Which works.
This is using karate-core 1.1.0 under Java 8 on Intel MacOS Monterey.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
@keithdtyler the design is such that no variable resolution is allowed / supported at all in an
Examples
table. a possible way to work around this is as follows:there is a proposal to make dynamic scenarios play well with
Background
here: https://github.com/karatelabs/karate/issues/1905@keithdtyler I have nothing to add to my previous comment. all the code is open-source, you are welcome to attempt a PR / fix against this ticket