question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

karate.eval fails if there is nothing for it to do

See original GitHub issue

Ran 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:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ptrthomascommented, Jul 1, 2022

@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:

Feature:

  Background:
    * def value = 123
    * def data = [{ foo: '#(value)', bar: 0 }]

  Scenario Outline: scenario
    * print foo
    * print bar

    Examples:
      | data |

there is a proposal to make dynamic scenarios play well with Background here: https://github.com/karatelabs/karate/issues/1905

0reactions
ptrthomascommented, Jul 5, 2022

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

karate: failed to use 'eval' in scenario step - Stack Overflow
Related. 1 · Failed tests when calling feature file from inside a scenario do not show which scenario (in the called feature) failed....
Read more >
Scenarios failing due to evaluation of 'karate-config.js ... - GitHub
When running a project with multiple (upwards of 1000) scenarios and a few functions set in karate-config.js , including a cached login call ......
Read more >
Karate | Test Automation Made Simple.
Karate is the only open-source tool to combine API test-automation, mocks, performance-testing and even UI automation into a single, unified framework.
Read more >
Karate API Testing Tutorial : How to use Eval in Karate?
In this video, we are going to learn JS Eval function API in Karate API Testing Framework.This is part of the Karate API...
Read more >
7 New Features in Karate Test Automation Version 1.0
Improved error details for match failures. Karates match command with its == , contains and contains any operators are very powerful. The JSON ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found