Getting error in Karate V1.1.0 org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (add) on [] failed due to: Message not supported - testing

I was using Karate v0.9.6 all this while. Recently I have upgraded to the version 1.1.0
One thing is troubling a lot is as bellows,
def responsevalue = response.body
def length = responsevalue.length
def number = []
eval for(var i = 0;i < length; i++) if(response.body[i].availablenumber > 10000) number.add(response.body[i].Id)
Error:- org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (add) on [] failed due to: Message not supported.
I'm sure I'm missing important part from the release notes. I would really appreciate any solution to this problem.
Many Thanks!

First I'd like to say that loops like this are not recommended. Use filter() operations instead:
* def number = response.body.filter(x => x.availablenumber > 10000)
Refer: https://github.com/karatelabs/karate#json-transforms
Anyway, the change you need to make is : number.push() instead of number.add(): https://github.com/karatelabs/karate/wiki/1.0-upgrade-guide#java-api-s-for-maps-and-lists-are-no-longer-visible-within-js-blocks

Related

How to use mapFieldType with gdal.VectorTranslate

I'm trying to export a postgresql database into a .gpkg file, but some of my fields are lists, and ogr2ogr send me the message :
Warning 1: The output driver does not natively support StringList type for field my_field_name. Misconversion can happen. -mapFieldType can be used to control field type conversion.
But, as in the documentation, -mapFieldType is not a -lco, i don't find how to use it with the python version of gdal.VectorTranslate
here ma config :
gdal_conn = gdal.OpenEx(f"PG:service={my_pgsql_service}", gdal.OF_VECTOR)
gdal.VectorTranslate("path_to_my_file.gpkg"), gdal_conn,
SQLStatement=my_sql_query,
layerName=my_mayer_name,
format="GPKG",
accessMode='append',
)
so i've tried to add it in the -lco :
layerCreationOptions=["-mapFieldType StringList=String"]
but it didn't work
so i diged into the code of gdal, added a field mapFieldType=None into the VectorTranslateOptions function, and added into its code the following lines :
if mapFieldType is not None:
mapField_str = ''
i = 0
for k, v in mapFieldType.items():
i += 1
mapField_str += f"{k}={v}" if i == len(mapFieldType) else f"{k}={v},"
new_options += ['-mapFieldType', mapField_str]
And it worked, but is there an other way ?
And if not, where can i propose this feature ?
Thank you for your help

How to debug groovy script using in live template?

i am new to groovy,i want to generate parameters comments like "#param [paramemter name] [parameter type] " for a method by live template.The predefined function "methodParameters()" can not do this,so i want run the custom grooy script  by predefined function "groovyScript".
The custom script named "test.groovy"  as follow:
def methodParameters=_1
def methodParameterTypes=_2
def result='';
def params=methodParameters.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();
def type=methodParameterTypes.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();
for(i = 0; i < params.size(); i++) {
result+='* #param '+ params[i] + ' ' + type[i] + ((i < params.size() - 1) ? '\\n ' : '')
};
return result
and I call this this script by inline function "groovyScript" like this:
groovyScript("D:\project\groovyDemo\src\test.groovy",methodParameters(),methodParameterTypes())
but I got the error message as follow:
No signature of method: java.util.ArrayList.replaceAll() is applicable for argument types: (java.lang.String, java.lang.String) values: [[\\[|\\]|\\s], ]Possible solutions: replaceAll(java.util.function.UnaryOperator)
i can not find any ideas to debug this groovy script using in live template of Idea.Can anyone give me some advice?
update:
Question definition:
how to fix this error
the steps about how to debug the groovy script using the live template of IntelliJ IDEA if it produces other errors
For the second question,I asked the IntelliJ IDEA support for help.He said that I can fix errors by simulating scripts instead of debugging directly,but I do not know how to simulate it.

How to call a feature file using a for loop. The feature file being called will also accept parameters from the feature file calling it

example :
Contents of FooGetRequest.feature file
* eval for (var i=0; i<foobarInDB.length; i++) call read('../features/BarGetRequest.feature') { foo_code:'#(foo_code)' , bar_code:'#(foobarInDB[i])'}
This is how BarGetRequest.feature file looks like :
Background:
* url baseUrl
Given path
"/v1/foo/"+foo_code+"/skus/"+bar_code+"/bar"
When method get
Then status 200
When I execute FooGetRequest.feature file i get the following error
[java.lang.RuntimeException: javascript evaluation failed: Expected ; but found read
you can use driven data driven feature in karate for loop over feature multiple times
Assuming foobarInDB is an array of bar_code and foo_code will always be same
* set foobarInDB[*].foo_code = foo_code
* call read('../features/BarGetRequest.feature') foobarInDB
refer Data driven feature
I wrote a small java script to return me a map like { foo : bar}
* def fun = function(x){return {foo :x }}
* def fooBarMap = karate.map(fooBarMap,fun)
* def validateResponse = call read('../features/BarGetRequest.feature) propertySkuMap
and in the BarGetRequest.feature I read the values accordingly.

Calling karate feature with variable

In karate you can call feature by sending json/list & map. There are multiple ways I have tried to send the value and use it in the called feature but it throws error
For example:
Error thrown : path: $, actual: '', expected:
'30e093da-c4e3-4ee0-b180-e5d0b4302d9f', reason: not a sub-string
Step:* call read('Logcheck.feature') {requestId : "#(responseHeaders['request-id'][0])"}
In the logCheck feature I am trying to get requestId by using #(requestId)
Logcheck feature Step
* def resultFromServiceLog = utils.getResultFromLog(baseUrl,'#(requestId)','service')
I have tried another approach where I have assigned it to
* def x = {requestId : responseHeaders['request-id'][0]}
* call ('logcheck.feature;) x
Another approach where I will send the json
* json req = {requestId : "#(responseHeaders['request-id'][0])"}
* call read('logcheck.feature') req
Step for logcheck.feature
* print req
* def resultFromServiceLog = utils.getResultFromLog(baseUrl,'#(requestId)','service')
For example:
Error thrown : com.intuit.karate.exception.KarateException:
javascript evaluation failed: utils.getResultFromLog(baseUrl,'#>
(requestId)','service'), null
print req output is
[print] {"requestId": "c996f752-c288-40c7-9398-c913254971e6"}
You need to be clear about using embedded expressions, please read the docs again: https://github.com/intuit/karate#rules-for-embedded-expressions
For example this is wrong:
* def resultFromServiceLog = utils.getResultFromLog(baseUrl,'#(requestId)','service')
Instead:
* def resultFromServiceLog = utils.getResultFromLog(baseUrl, requestId, 'service')
And this is wrong:
* call ('logcheck.feature') x
You probably meant:
* call read('logcheck.feature') x

Getting an invalid token on an interpolated string sent from python/jinga2 backend

I'm sending a variable called apiID from a tornado/jinja2 python file to my vuejs template like this:
class SmartAPIUIHandler(BaseHandler):
def get(self, yourApiID):
doc_file = "smartapi-ui.html"
dashboard_template = templateEnv.get_template(doc_file)
dashboard_output = dashboard_template.render(apiID = yourApiID )
self.write(dashboard_output)
then in vuejs I'm interpolating the variable with no problem except it gives me an error
it says: Uncaught SyntaxError: Invalid or unexpected token
I checked on the python handler file and apipID is a string, so I don't see the problem. I'm quite new to python so maybe the answer is more obvious to one of you. I appreciate the help!!
Because of dashboard_output = dashboard_template.render(apiID = yourApiID ), you must have, in your template, something around the code:
this.apiID = {{ apiID }};
Due to the value being not a number but a string, add the 's:
this.apiID = '{{ apiID }}';