how to pass parameters in groovy script using some java code -
i want automate test. have written script in groovy in soapui. calling script using java code. want pass parameters given user. saw interface soapui script or script engine. help? know should not ask without trying don't know how begin. please me out
let's assume there script in src/test/groovy/com/ka/script.groovy , test in src/test/java/com/ka/myscripttest.java
script.groovy
println "hello ${name}" name
myscripttest.java
package com.ka; import groovy.lang.binding; import groovy.lang.groovyshell; import groovy.lang.script; import org.junit.test; import java.io.*; public class myscripttest { @test public void executescript() { binding binding = new binding(); binding.setvariable("name", "anonymous"); groovyshell shell = new groovyshell(binding); script script = shell.parse(getscript("./src/test/groovy/com/ka/script.groovy")); bytearrayoutputstream scriptoutput = new bytearrayoutputstream(); printstream newout = new printstream(scriptoutput); printstream oldout = system.out; try { system.setout(newout); object result = script.run(); system.out.println("result = " + result); } { system.setout(oldout); } system.out.println("script output = " + scriptoutput.tostring()); } private inputstreamreader getscript(string scriptpath) { try { return new inputstreamreader(new fileinputstream(scriptpath)); } catch (filenotfoundexception e) { throw new runtimeexception(e); } } }
here test passes name variable's value script. it's need undestand.
Comments
Post a Comment