Forum Discussion
Actually I want to automate my test. for this I have written a script in groovy in soapui. Now I am calling that script using java code. but I want to pass parameters given by user. Previously I was reading from file
- nmrao9 years agoChampion Level 3
Still not sure what your use case is.
If you have soapui project, you can run it from soapui tool, or testrunner.bat/sh utility or from Java code using Junit etc. In any of these cases, it does not arise a case where you need to execute a groovy script. Looks you doing something different. If you tell exactly, that will help better.Can you clarify?
I would also like to give you another example where I use it exactly reverse way that you mentioned
I use IDE create a class / methods in Java, this is entirely a different java project, compile, create jar, place it SOAPUI_HOME/bin/ext directory and soapui's groovy script call the java api like create object and call method.
Now it comes to properties.
If you see a groovy script test step editor's top right, you would read as:
"script is invoked with context, log, testRunner variables"
So, now i would just refine java class's Constructor which can take these parameters and have these variables available in java.
Here is sample java code:
/** * this class is initialized and called from groovy script **/ import com.eviware.soapui.model.testsuite.TestCaseRunContext import com.eviware.soapui.model.testsuite.TestCaseRunner import org.apache.log4j.Logger public class ScriptHerlper { public TestCaseRunContext context; public Logger log; public TestCaseRunner testRunner; public ScriptHerlper(TestCaseRunContext context, Logger log) { this.context = context; this.log = log; this.testRunner = context.getTestRunner(); } public void execute() { log.info("This is in execute method of ScriptHerlper class"); String myPropertyValue = context.getTestCase().getPropertyValue("ACCESS_TOKEN"); log.info("TestCase property ACCESS_TOKEN value is :"+myPropertyValue); } }
As mentioned, compile above class, create jar, place it under SOAPUI_HOME/bin/ext directory and restart soapui.
A groovy script now will look:
//creating a test case level property say ACCESS_TOKEN context.testCase.setPropertyValue('ACCESS_TOKEN', '1234-a3h-45ab-12dh2g') def myScriptHelper = new ScriptHelper(context, log) //ACCESS_TOKEN will be accessed in execute method. myScriptHelper.execute()
Hope this helps at least understanding of how properties can be accessed between groovy and java.
Also note that, context is a variables which is something that varies at different levels of scripting such as Setup and Tear down scripts of test case, Test Suite, and Project.
- vihang9 years agoOccasional Contributor
I am paosting groovy script here. I am using properties file here instead of testCase level property. and when I am passing parameter user need not to go and invoke this groovy script. my java program will only invoke with passing this passing parameter given by user. Sir one last thing script I have written inside one testCase as one teststep.
import jxl.* import jxl.write.* def dataFileLocation="D:/SOAP/input.xls" //Datasheet read start def workbook = Workbook.getWorkbook(new File(dataFileLocation)) def sheet = workbook.getSheet(0) int count=workbook.getNumberOfSheets() def rowCount = sheet.getRows() def colCount = sheet.getColumns() def myTestCase = context.testCase propTestStep = myTestCase.getTestStepByName("Properties"); def arr=[] //Datasheet read end //Content Write start WritableWorkbook workbook1 = Workbook.createWorkbook(new File("D:/SOAP/output1.xls")) WritableSheet sheet1 = workbook1.createSheet("Worksheet 1", 0) //Content Write end for(int i = 0;i < rowCount; i++){ for(int j = 1;j < colCount+1; j++){ arr[i]= sheet.getCell(j-1,i).getContents() def val=arr[i] propTestStep.setPropertyValue("zip",val) def project = testRunner.testCase.testSuite.project def aa=testRunner.runTestStep( project.testSuites['USZipSoap TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByZIP'] ) def response = testRunner.testCase.testSteps["GetInfoByZIP"].testRequest.response.contentAsString Label label = new Label(j,i,response); log.info label sheet1.addCell(label); workbook1.write() log.info j+" " +i+" "+" "+response } } workbook1.close()
Here I am reading data from some excel file. But now I want to pass this data what user give in java program.The way you have shown its reverse I want to achieve.
- nmrao9 years agoChampion Level 3Well, you can load property file and you do the above code in java. Then why are you bringing groovy script in between?
Related Content
- 2 years ago
- 4 years ago
Recent Discussions
- 15 years ago