Reading json from file in Karate feature fails with js evaluation failed error - karate

I'm using Karate Netty, version 0.9.6 on Windows 10 with openJDK 14.0.2.
I'm trying to read data from a json file in a feature file.
The following code fails:
Scenario: Get the credit balance
* def data = read('classpath:examples1/user_credit_balance_get.json')
My console output looks as follows:
Karate version: 0.9.6
======================================================
elapsed: 2.31 | threads: 1 | thread time: 0.02
features: 1 | ignored: 0 | efficiency: 0.01
scenarios: 1 | passed: 0 | failed: 1
======================================================
failed features:
features.protect_a_prospect: protect_a_prospect.feature:4 - evaluation (js) failed: read('classpath:examples1/user_credit_balance_get.json'), java.lang.RuntimeException: evaluation (js) failed: ?{
"session_data": {
"user_id": "101",
"session_id": "dslkdaskljd",
"token": "02389poasklj"
},
"call_data": {
"user_id": "101"
}
}, javax.script.ScriptException: <eval>:2:18 Expected ; but found :
"session_data": {
^ in <eval> at line number 2 at column number 18
stack trace: jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:477)
stack trace: com.intuit.karate.ScriptBindings.eval(ScriptBindings.java:155)
com.intuit.karate.exception.KarateException: there are test failures !
at ...(.)
This leads me to believe that Karate is trying to read my json file as if it were JavaScript.
What could be the reason for this behaviour?
-- Edit --
Using karate.readAsString instead of read works as a workaround for me:
Scenario: Get the credit balance
* def data = karate.readAsString('classpath:examples1/user_credit_balance_get.json')

This is mighty confusing, you say Karate mocks but then you show the log for a Karate test. Karate should never try to evaluate a *.json file.
I think the best thing to do is to follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Related

I am struggling to get Karate to read my data from my csv file before I call my function [duplicate]

This question already has an answer here:
How do I invoke my JS Function for a particular column in a csv file in the Karate Framework?
(1 answer)
Closed 1 year ago.
#noinspection CucumberTableInspection
Feature: Serverless
Background:
* def data = read('input.csv')
* csv data = data
* def isValid = function(x){ return (x.name && x.unit) ? x.cost.length != 0 : true }
Scenario Outline:
* assert isValid(__row)
Examples:
| data |
The test runs but doesn't check anything.
Karate version: 1.1.0.RC4
elapsed: 1,77 | threads: 1 | thread time: 0,00
features: 0 | skipped: 1 | efficiency: 0,00
scenarios: 0 | passed: 0 | failed: 0
HTML report: (paste into browser to view) | Karate version: 1.1.0.RC4
===================================================================
Process finished with exit code 0
The * csv data = data is not needed and was used to explain something totally different earlier. When "read" a CSV becomes a JSON array automatically: https://github.com/intuit/karate#csv-files
If you are new to Karate I recommend you don't use CSV files and the Scenario Outline. Please just get used to Examples first and then move on from there. You are combining 3 advanced topics - CSV, dynamic outlines and conditional assertions. Do your tests really need to be so clever and complex ? Read this please: https://stackoverflow.com/a/54126724/143475
And please please read the docs and the examples: https://github.com/intuit/karate#dynamic-scenario-outline

Module highlight.js has no exported member HLJSStatic & IModeBase

my package.json looks like below,
dependencies:
"highlight.js": "^10.4.0",
"vue-highlight.js": "^3.1.0",
devDependencies:
"#types/highlight.js": "^10.1.0",
FYI, Its already working fine but i have tried upgrade(Bump updates) im getting this issue.
error in D:/paypayWorkspace/bump-update/developer-panel-web/client/node_modules/vue-highlight.js/dist/types/HLJSLang.d.ts
ERROR in D:/paypayWorkspace/bump-update/developer-panel-web/client/node_modules/vue-highlight.js/dist/types/HLJSLang.d.ts
1:10 Module '"highlight.js"' has no exported member 'HLJSStatic'.
> 1 | import { HLJSStatic, IModeBase } from 'highlight.js';
| ^
2 | /**
3 | * Highlight.js language
4 | */
error in D:/paypayWorkspace/bump-update/developer-panel-web/client/node_modules/vue-highlight.js/dist/types/HLJSLang.d.ts
ERROR in D:/paypayWorkspace/bump-update/developer-panel-web/client/node_modules/vue-highlight.js/dist/types/HLJSLang.d.ts
1:22 Module '"highlight.js"' has no exported member 'IModeBase'.
> 1 | import { HLJSStatic, IModeBase } from 'highlight.js';
| ^
2 | /**
3 | * Highlight.js language
4 | */
ERROR Build failed with errors.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
make: *** [dev-env] Error 1
I have not enough reputation to comment, but I think the answer is that highlight.js updated to version 10.XX.XX and there are some breaking changes so vue-highlight.js is not working anymore.
https://github.com/gluons/vue-highlight.js/issues/66
I have just now realized that you opened the issue there... but I think my answer will help those who land here.

Karate: test count using failsafe + parallel runner

I'm writing some integration tests using failsafe.
There are two features like this:
Feature: example feature 1
Scenario:
Given url 'http://httpbin.org/'
When method get
Then status 200
My "suite" is:
public class ApiIT {
#Test
public void testParallel(){
Results results = Runner.path("classpath:.").tags("~#ignore").parallel(5);
assertEquals(results.getErrorMessages(), 0, results.getFailCount());
}
}
When I run integration tests using mvn (mvn clean install) I get:
Karate version: 0.9.6.RC4
======================================================
elapsed: 1.41 | threads: 5 | thread time: 1.39
features: 2 | ignored: 0 | efficiency: 0.20
scenarios: 2 | passed: 2 | failed: 0
======================================================
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.558 sec - in ApiIT
Is there any way to count the real tests so I can get this in the logs:
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.558 sec - in ApiIT
I uploaded an example project here: https://github.com/italktothewind/test-count
Nope. Ignore the last line, that is JUnit because you have 1 #Test annotation. What matters here is the Karate output. JUnit makes it simpler to call Karate. But if it bothers you so much, call the Runner using a Java main method.

How to Validate a Non-JSON response body using Karate (2)

This is in continuation to How to validate Non-JSON response body using Karate.
Details: When the API post call is made, if the employee is already available in the DB, then an error response is thrown as follows in the response body, which is not of Json/String format:
{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'NewEmp' for key 'employee_name_unique'}}
My aim is to validate the above if the error response is thrown as expected.
I tried the solution provided in How to validate Non-JSON response body using Karate, but it did not work as expected. Below are the details:
I do not understand how to use the * provided in the solution of my previous question. Could you please explain how to use the *
Karate Feature:
Scenario: Testing non-string response
Given url 'dummy.restapiexample.com/api/v1/create'
And request {"name":"PutTest8","salary":"123","age":"23"}
When method POST
Then status 200
* string temp = response
And match temp contains 'error'
The above is throwing an error as follows --
line 20:4 mismatched input '*' expecting <EOF>
17:43:46.230 [main] ERROR com.intuit.karate.core.FeatureParser - syntax error: mismatched input '*' expecting <EOF>
17:43:46.235 [main] ERROR com.intuit.karate.core.FeatureParser - not a valid feature file: src/test/java/learnKarate/postcall.feature - mismatched input '*' expecting <EOF>
NOTE: I also tried to 'assert' the response - which failed, too, with the below error.
Then assert $ contains 'error'
Error:
com.intuit.karate.exception.KarateException: postcall.feature:29 - javascript evaluation failed: $ contains 'error', <eval>:1:2 Expected ; but found contains
$ contains 'error'
^ in <eval> at line number 1 at column number 2
at ✽.Then assert $ contains 'error' (postcall.feature:29)
There is something seriously wrong in your example or your environment. The * is just a replacement for Given When etc. For example paste this into a new Scenario, and this works for me:
* def response = 'error'
* string temp = response
And match temp contains 'error'
Since you seem to be stuck, it is time for you to follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
All the best !

Avro : java.lang.RuntimeException: Unsupported type in record

Input: test.csv
100
101
102
Pig Script :
REGISTER required jars are registered;
A = LOAD 'test.csv' USING org.apache.pig.piggybank.storage.CSVExcelStorage() AS (code:chararray);
STORE A INTO 'test' USING org.apache.pig.piggybank.storage.avro.AvroStorage
('schema',
'{"namespace":"com.pig.test.avro","type":"record","name":"Avro_Test","doc":"Avro Test Schema",
"fields":[
{"name":"code","type":["string","null"],"default":null}
]}'
);
Getting a runtime error while STORE. Any inputs on resolving the same.
Error Log :
ERROR org.apache.pig.tools.pigstats.SimplePigStats - ERROR 2997: Unable to recreate exception from backed error: org.apache.avro.file.DataFileWriter$AppendWriteException: java.lang.RuntimeException: Unsupported type in record:class java.lang.String
at org.apache.avro.file.DataFileWriter.append(DataFileWriter.java:263)
at org.apache.pig.piggybank.storage.avro.PigAvroRecordWriter.write(PigAvroRecordWriter.java:49)
at org.apache.pig.piggybank.storage.avro.AvroStorage.putNext(AvroStorage.java:722)
at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigOutputFormat$PigRecordWriter.write(PigOutputFormat.java:139)
at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigOutputFormat$PigRecordWriter.write(PigOutputFormat.java:98)
at org.apache.hadoop.mapred.MapTask$NewDirectOutputCollector.write(MapTask.java:558)
at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:85)
at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:106)
at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigMapOnly$Map.collect(PigMap
2015-06-02 23:06:03,934 [main] ERROR org.apache.pig.tools.pigstats.PigStatsUtil - 1 map reduce job(s) failed!
2015-06-02 23:06:03,934 [main] INFO org.apache.pig.tools.pigstats.SimplePigStats - Script Statistics:
Looks like this is a bug: https://issues.apache.org/jira/browse/PIG-3358
If you can, try to update to pig 0.14, according to the comments this has been fixed.