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.

JS API to type-cast and handle all the match types supported in the DSL

See original GitHub issue

from discussion here: https://stackoverflow.com/a/62638847/143475

currently type conversion is possible only via the companions to def e.g. json xml etc: https://github.com/intuit/karate#type-conversion

sometimes there is a need to type-cast when within a JS block. also - a feature to auto-detect a string and type-cast it would be nice. proposals below (JS block, not Gherkin)

// not 100% sure that data1 can "remain" as XML after making it back to a feature
var data1 = karate.fromString(someString);
// this will follow the same rules as type-conversion keywords (see link above)
var data2 = karate.toType('json', anyObject);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Sdaascommented, Jul 22, 2020

Hi Kostas

I had put together something simple a while ago to test Kafka from Karate. Pls see if https://github.com/Sdaas/karate-kafka helps

Daas

p.s. I am working on adding something close to a kafka dsl to karate, so that people that are not very technical could write end to end tests that involve both kafka and api calls, not sure if that will end up working, it has become more complicated than i hoped for 😄

1reaction
KostasKgrcommented, Jul 13, 2020

Managed to get things working! Thank you for your explanations! Seems that if I use match then no asMap or asList is needed, but they are needed if I want to invoke a custom javascript filter.

All of the below pass with latest develop, many thanks, I’ll have to think how to incorporate it with the rest of our code once it is in a release candidate

Feature: Matching and filtering in javascript

  Scenario Outline: Match native types in js: <type>
    * eval
    """
    var temp = karate.fromString(<data>);
    karate.log(temp);
    karate.log(temp.value);
    var val = temp.value;
    // Match handles type conversions, so no asMap or asList needed
    var res = karate.match(<match>);
    if (!res.pass) karate.fail(res.message);
    """

    Examples:
      | type      | data                                                        | match                                             |
      | json map  | '{ "foo": "bar", "nested": { "deeply": "here", "id": 5 } }' | "val contains deep { nested: { deeply: 'here' }}" |
      | json list | '[{"type": "car"}, {"type": "bicycle"}]'                    | "val contains [{type: 'car'}]"                    |
      | string    | 'hello world'                                               | "val contains 'hello'"                            |
      | xml       | '<greeting language="en">Hello World</greeting>'            | "val /greeting/@language == 'en'"                 |


  Scenario Outline: Filter native types in js: <type>
    * eval
    """
    var temp = karate.fromString(<data>);
    karate.log(temp);
    karate.log(temp.value);
    var val = temp.value;
    if(temp.json) {
      val = temp.mapLike ? temp.asMap : temp.asList;
    }
    var filter = function(data) { <filter> }
    var res = filter(val)
    if (!res) karate.fail("Filter did not match");
    """

    Examples:
      | type      | data                                                        | filter                                                    |
      | json map  | '{ "foo": "bar", "nested": { "deeply": "here", "id": 5 } }' | return data.nested.id == 5                                |
      | json list | '[{"type": "car"}, {"type": "bicycle"}]'                    | return data[0].type == "car"                              |
      | string    | 'hello world'                                               | return data.contains("hello")                             |
      | xml       | '<greeting language="en">Hello World</greeting>'            | return karate.xmlPath(data, '/greeting/@language') == 'en' |
Read more comments on GitHub >

github_iconTop Results From Across the Web

JS API to type-cast and handle all the match types supported ...
p.s. I am working on adding something close to a kafka dsl to karate, so that people that are not very technical could...
Read more >
Variables and their type conversion using KarateDSL
They exist just to categorize and store data in memory. Type conversions is the process of converting a variable from one data type...
Read more >
Karate DSL - $.embedded_json_message | data types don't ...
I have a scenario where I'm retrieving rows of data from a Postgres DB table and matching the corresponding column output against a...
Read more >
Multi-match query | Elasticsearch Guide [8.5] | Elastic
Types of multi_match query:edit ; best_fields. (default) Finds documents which match any field, but uses the _score from the best field. See best_fields...
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 >

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