Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

javascript - karateframework:Pass values read from csv as arguments to javaprogram

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...