Ich möchte meinen Test automatisieren. Dafür habe ich in Soupui ein grooviges Skript geschrieben. Jetzt rufe ich dieses Skript mit Java-Code auf. aber ich möchte die vom Benutzer angegebenen Parameter übergeben. Ich sah eine Schnittstelle wie Soapui-Skript oder Skript-Engine. Würde es helfen? Ich weiß, ich sollte nicht fragen, ohne etwas zu versuchen, aber ich weiß nicht, wie ich anfangen soll. Bitte helfen Sie mirwie Parameter in groovy Skript übergeben mit einigen Java-Code
0
A
Antwort
0
Lassen Sie uns annehmen, dass es ein Skript in src/test/groovy/com/ka/script.groovy und ein Test in src/test/java/com/ka/MyScriptTest.java
Skript .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);
} finally {
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);
}
}
}
Hier ist der Test bestanden wird der Wert des Namensvariable an das Skript. Es ist das, was du brauchst, wenn ich es verstehe.
Übrigens ist [dies] (https://community.smartbear.com/t5/SoapUI-Open-Source/how-to-pass-parameters-in-groovy-script- using-some-java- code/mp/119986 # M20704) frage deine? – Rao
Ja, Sir, ist das falsch? –