-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS API to type-cast and handle all the match types supported in the DSL #1202
Comments
while we're at it - I've felt the need for a |
and |
Thank you for raising this @ptrthomas ! I managed to get the minimum behavior I was looking for by parsing json in java, so that karate understood that to be a json. Not sure i can make any such workaround for xml based messages though. But if you see this coming to 0.9.6 it would be awesome. By the way I noticed that match contains, and match with jsonpath cannot be used in javascript, I have 3 features exploring that. I would love to contribute but I am still wrapping my head around how this all works :D 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 :D |
I looked at the
yes, this is somewhat deliberate, to prevent teams doing "too much" in javascript - but I'm, open to revising that as well. btw you can use the "short-cuts" so this will work * def valid = match(foo, '#(^bar)');
you may get some ideas from this thread: #1191 (comment) |
yes that means each, contains, anything supported in the dsl / gherkin side I resisted this for years because designing an api for all the types was so painful but then it struck me - the match statement is parsed as plain-text and we have a java class for that not the most conventional way - but hey you are not supposed to be doing too much in js which was the other reason why I avoided this until now - anyway
we now have a way to do all the DSL if a variable needs to be set from JS, that is easy using EDIT: in the rare case that you need to set a variable as XML into the context from JS you can use note that we can conditionally fail a test using |
…ional logic #1202 will return a ScriptValue instance - which has plenty of helper methods / functionality on it for introsprection, type conversion and more, intended for advanced users, so will keep lightly documented
and implemented @KostasKgr I think you should have all you need ! |
Thank you @ptrthomas ! Sorry for not being responsive, looks to be everything that was discussed! I will try it out! |
Hello @ptrthomas , I tried develop commit 2f8765b, I think i built it correctly (I have testBigDecimalsInJson test failing both there and on 0.9.5, so i am assuming its something weird with being on windows) I tried the karate.fromString() but I couldn't get it to work, also I am getting some very confusing behavior while trying to log things. You can hopefully reproduce this at the first scenario here (used version 2.0.0 which was in develop branch): https://github.com/KostasKgr/karate-issues/blob/java_json_interop_v3/src/test/java/examples/consumption/consumption.feature#L5 So we start with this in java, and return it with another one in a List
The iterate the list in consume.js and covert using karate.fromString().
Keep the value of this and print it, it still says jayway:
Pass the value to the filtering function, but now it appears like the json I was expecting to see if I karate.log it from within the filter:
However if I print the correlation id, it is undefined, so the test fails, as I cant actually filter on correlation id:
Am I using this wrong? Cheers p.s. I didnt get time to test the new karate.match features |
@KostasKgr I think you missed that |
Hello @ptrthomas , I am already using .value on the scriptvalue object, is that what you mean?
However this did not work in a javascript function. Could it be that match autoconverts left value to a native type on the fly in your examples in #1202 (comment) ? I can't seem to get that behaviour inside of a javascript function, |
@KostasKgr use Scenario: parsing json, xml or string within a js block
* eval
"""
var temp = karate.fromString('{ "foo": "bar" }');
if (!temp.json) karate.fail('expected json');
var val = temp.asMap;
var res = karate.match(val, { foo: 'bar' });
if (!res.pass) karate.fail(res.message);
""" |
Hello @ptrthomas , I will provide feedback as soon as I try it. Not sure I understood when to use asMap and asList, as the code will not have knowledge of if the json is a json whose root element is a list, or if it's root element is a map. |
I added both elements with a list root and a map root in my topic. asMap worked before I added list elements in the topic, afterwards I get an exception such as this: I have updated the branch mentioned above to have this code.
Is there no uniform api that determines the correct method to call? It seems to work in feature files from your examples :( |
@KostasKgr can you please paste a self-contained snippet here with the problem, my comment above would have given you an example |
Ofcourse! I will try to make one such as the above. My main question though was that I do not know what jsons will be arriving, so I cannot decide to use asMap or asList |
@KostasKgr the answer is you should use feel free to suggest any better approaches you see |
Oh, ok! I will try it, i only saw the fromString example and missed the mapLike and listLike mentioned in the fromObject example! Cheers! |
Managed to get things working! Thank you for your explanations! 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
|
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
|
0.9.6 released |
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-conversionsometimes 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)
The text was updated successfully, but these errors were encountered: