Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to pass values as parameters dynamically to java program using karate framework.

I am trying to read the data from csv file. which is converted to json format

Csv Data

FirstName,MiddleName,LastName
"John","K","Kennady"
"Mahesh","g",Readdy"
* def csvData = read('csv Data')
* read csvData

Output of read csvData

[{"FirstName" : "John","MiddleName" : "K", "LastName" : "Kennady"},{"FirstName" : "Mahesh","MiddleName" : "g", "LastName" : "Readdy"}]

I am able to extract the values from json and the result looks like this

values = ["John", "K", "Kennady"]

I should pass the values as paramters to below command JavaDemo.doWorkStatic

* def JavaDemo = Java.type('com.mycompany.JavaDemo')
* def result = JavaDemo.doWorkStatic('Firstname','MiddleName','lastname')

in javascript we can assign array to array which can assign to arguments for java . But the below step is not possible in karateframework

[a, b ,c ] = Values
cosnole.log(a)
cosnole.log(b)
cosnole.log(c)
def result = JavaDemo.doWorkStatic(a,b,c)

How can i acheive the final step passing the values as arguments

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here you go:

* def v = ['John', 'F', 'Kennedy']
* def result = JavaDemo.doWorkStatic(v[0], v[1], v[2])

Karate is not JavaScript. Please contribute code if you want to change anything.

EDIT: note that JSON arrays are auto-converted to Java List-s. Please read the docs: https://github.com/intuit/karate#calling-java

For example if you have this:

public static String concat(List<String> list) {
    StringBuilder sb = new StringBuilder();
    for (String s : list) {
        sb.append(s);
    }
    return sb.toString();
}

You can do this:

* def array = ['a', 'b', 'c']
* def res = Utils.concat(array)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...