The below function in feature files was working for version 0.9.2. Upgraded to 0.9.3 and this gives error : javascript function call failed: Index: 0.0, Size: 0. Code below:
var cnd = ['test1','test2'];
function set_filter(arg)
{
var i;
var filter = {filterValues:[]};
for(i=0;i<arg.length;i++)
{
filter.filterValues[i] = arg[i];
}
return filter;
}
set_filter(cnd)
Also i was earlier able to push values in a javascript array using below, but this has also stopped working in 0.9.3. Get error:javascript function call failed: TypeError: arr.push is not a function
var arr = [];
arr.push('test1','test2');
Try the scenario below that works in 0.9.2 but reports error (mentioned above) in 0.9.3
Scenario: JS test
* def filter_template =
"""
function() {
var filter_params = {
filterValues:[]
};
return filter_params;
}
"""
* def template = call filter_template
* def filter_condition = ['test1','test2']
* def setFilter =
"""
function(arg) {
var i;
var filter = arg.filter_template;
for(i=0;i<arg.condition.length;i++)
{
filter.filterValues[i] = arg.condition[i];
}
return filter;
}
"""
* def getFilter = call setFilter { filter_template: '#(template)', condition: '#(filter_condition)' }
* print getFilter
Help is much appreciated.
We've made the JS conversions stricter, everything will be a Java collection behind the scenes. If you make this change, things will start working:
filter.filterValues.set(i, arg.condition.get(i));
The same goes for push() - use add() or karate.appendTo(varname, value) instead.
My strong recommendation is don't use too much of JS logic especially loops. Karate has functions such as map(), forEach() and repeat() to solve for these needs. Please refer the docs here: https://github.com/intuit/karate#loops
You will thank me later because it will make your scripts easier to understand and maintain. One reason why this is needed is to pave the way for us to change the JS engine in the future.
Related
I have the following javascript file MyService.js:
function(config) {
config.MyService = function(request) {
return call('classpath:path/to/my.feature#tag', request);
};
return config;
}
I load this js from my karate-config.js in order to reuse it from my feature files.
config = karate.callSingle('classpath:path/to/MyService.js', config);
It works as expected and I can call my.feature from any feature file. For example:
Given def res = call MyService myRequest
The problem appears when I try to add a new level to MyService.js function:
function(config) {
config.ApiOauthService = {
myCall : function(request) {
return call('classpath:path/to/my.feature#tag', request);
}
};
return config;
}
When I add the following code to my feature file:
Given def myCall = call MyService.myCall myRequest
I get the following error: "not a js function or feature file"
Do anybody know where is the problem? Is it possible to do what I am trying to do?
Thanks
Yes in Karate 0.9.3 onwards we have limited JS function nesting to only the top-level. You can find a full explanation in this comment.
So if you need JS function "name spacing" you can easily achieve this as per the documentation: https://github.com/intuit/karate#multiple-functions-in-one-file
And if you need more complex nesting, switch to using Java code where you can nest functions at any level.
Ok, so I am going to explain what my scenario is and how I have implemented it using karate. But i am looking for a better way of doing things which make my tests more readable and also want to use karate's api rather than too much javascript.
Scenario: I have a filter parameter for the api endpoint that i am testing and this filter param takes a json object of key-value pairs. So i have done the following:
Have created a filter-template.js as below:
function() {
var filter_params = {
jobId:null,
customerIds:[],
filterValues1:[],
filterValues2:[],
};
return filter_params;
}
I read this template in my karate scenario and because this is a template, at runtime i set the values in this template and run the test. I will have different values for key-value pairs so each test will set its own filter condition.
This is done by writing custom js function that takes the template as argument and also the filter condition values (referring to arg function argument below), sets the passed conditions to specific key's and then returns json object. Code below:
Scenario: Set filter scenario
* def filter_template = call read('filter-template.js')
* def filter_vals_list = [1001,1002]
* def filter_condition = { cnd1: 'foo', cnd2: '#(filter_vals_list)' }
* def setFilter =
"""
function(arg) {
var i;
var filter = arg.template;
filter.jobId = arg.condition.cnd1;
for(i=0;i<arg.condition.cnd2.length;i++)
{
filter.filterValues1.add(arg.condition.cnd2.get(i));
}
return filter;
}
"""
* def getFilter = call setFilter { template: '#(filter_template)',
condition: '#(filter_condition)' }
I then pass the getFilter as a param to my api request.
What I am hoping to understand is:
How can i get away from using JS loops above when setting filter?
Use karate's in-built functions like karate.map(), karate.forEach() to simplify the tests.
Any better approach if possible on tackling this scenario.
Help and guidance much appreciated.
From what I understood, I have simplified your scenario below:
* def filter_vals_list = [ 1001, 1002 ]
* def job_id = 'foo'
* def filter_template =
"""
{
jobId: '#(job_id)',
customerIds: [],
filterValues1: '#(filter_vals_list)',
filterValues2: [],
}
"""
Let me know if I missed anything. Please refer embedded expressions: https://github.com/intuit/karate#embedded-expressions
Now, you can easily use a re-usable JSON for the template, by changing the last step to the below, and yes - embedded expressions work even in re-usable JSON files !
* def filter_template = read('filter-template.json')
You may get even better ideas once you try the data-driven Scenario Outline. So hope that makes it clear how you are un-necessarily complicating things with JS ! You don't even need karate.map() etc.
Suppose I have saved followings functions in a Utility js file.
function getCurrentDate(){
return 'date';
}
function getMonth(){
return 'Oct';
}
Please help me how any of these methods can be accessed in feature file.
I tried following code but it is not working.
* def fun = call read('Utility.js')
* def result = getData()
or
* def result = fun.getData()
In Karate, a JS file can contain only one function and it does not need a name, take a closer look at the examples.
I don't really recommend combining multiple functions into one file, it just makes things much harder to maintain. But if you really insist, here's how:
function() {
return {
getCurrentDate: function(){ return 'date' },
getMonth: function(){ return 'month' }
}
}
EDIT: a much better answer is here: https://stackoverflow.com/a/49384760/143475
* def data = karate.read('classpath:<path>/getRandomString.js')(<inputToTheFunctionIfAny>);
The above code works.
Request you to help me find the solution of below issue.
I have one function and corresponding Jasmine test case, like the one written below.
If I use angular.copy (and I have to use this only) in my function, Jasmine test case fails and error shown is expected 'originalValue' to equal 'newValue'.
If I use var obj = param1 (and not angular.copy) then Jasmine test case executes successfully.
I have to use angular.copy and at the same time, want jasmine test case to pass. Please help.
function
function func(param1, param2, condition)
{
var obj = angular.copy(param1);
if(condition){
obj.prop = param2;
}
}
jasmine test case
it('xxxx', function(){
var param = {'prop': ''};
var obj = {'prop': 'orignialValue'};
func(param, 'newValue', true);
expect(obj.prop).toEqual('newValue');
});
The example you provided is incorrect.
it('xxxx', function(){
var obj = {'prop': 'orignialValue'};
func(param, 'newValue', true);
expect(obj.prop).toEqual('newValue');
});
param is not defined over here, this should be obj I assume.
Since you use angular.copy, obj.prop is not added by ref to param1.
I found the solution. Instead of doing angular.copy inside the function func, I am first doing angular.copy and then passing this copied object to function func as its first parameter (param1).
I have a js interop function which is using the for in construct to iterate over the input elements, but it's throwing error at runtime.
native("document")
val ndoc: dynamic = noImpl
fun jsInterOp() {
js("console.log('JS inling from kotlin')")
val ies = ndoc.getElementsByTagName("input")
for (e in ies) {
console.log("Input element ID: ${e.id}")
}
}
Getting the following js error
Uncaught TypeError: r.iterator is not a functionKotlin.defineRootPackage.kotlin.Kotlin.definePackage.js.Kotlin.definePackage.iterator_s8jyvl$ # kotlin.js:2538
Any suggestions on how to fix this one?
Kotlin : M12
The generated js code for the function is,
jsInterOp: function () {
var tmp$0;
console.log('JS inling from kotlin');
var ies = document.getElementsByTagName('input');
tmp$0 = Kotlin.modules['stdlib'].kotlin.js.iterator_s8jyvl$(ies);
while (tmp$0.hasNext()) {
var e = tmp$0.next();
console.log('Input element ID: ' + e.id);
}
},
forEach didn't work because it's an Array function in JS, but getElementsByTagName returns HTMLCollection . So i changed the kotlin code to use the traditional for loop which iterate over this collection and is work as expected.
val ies = ndoc.getElementsByTagName("input")
for (i in 0..(ies.length as Int) - 1) {
console.log("InputElement-${i} : ${ies[i].id}")
}
Kotlin for-loop uses a lot of internal magic.forEach() is more straightforward on JS. Try this:
ies.iterator().forEach { ... }
It seems to be a bug in Kotlin M12, because I was unable to do a for-loop even on simple a list.
for(i in listOf(1, 2)); // TranslationInternalException
Also
I am not sure what is that document that you use here, but you may like the standard API:
import kotlin.browser.document
val ies = document.getElementsByTagName("input")