How to copy a request in soapui to a folder using groovy?

家住魔仙堡 提交于 2019-12-12 05:49:41

问题


I am new in groovy and I want to have soap request which contains file which I want to be copy to a folder with a specific name when I am running my TestCase. My testcase will be created; first soap request disable and the groovy that will run and copy the soap. Can you please advise?


回答1:


Yes, it can be done. It's really straight forward. Took me two minutes to create an example of copying the request and response to files....

In your test, add a groovy step after the request of interest. In the groovy, you need to reference the request and throw that at a file...

def rawRequest = context.expand( '${YOUR REQUEST NAME HERE#RawRequest}' )

def file1 = new File('c:/temp/groovy1.txt');
file1.write (rawRequest);

return rawRequest;

That will dump out your 'raw request' to a file.

Similarly, here's an example of saving the response of the request as XML. You'll need a further groovy script step. Again, really simple...

def responseAsXml = context.expand( '${YOUR REQUEST NAME#ResponseAsXml}' )


def file1 = new File('c:/temp/groovy2.xml');
file1.write (responseAsXml);
return responseAsXml;

You don't event need the return statements. I just threw them in so I could check I am pulling data from the request step.

Obviously, you'll need to expand this to correctly name your files. You should also wrap in in some execption checking to be sure there are no issues writign to file.

If you have the Pro version, you can right-click in the Groovy script and follow the 'Get Data' context menu to find the thing you want to dump into a file.

If you don't have the Pro version, then you'll need to look handcrafting the xpath to the node of interest. Use log.info(someVar) to help verify you're getting down to the node of interest.




回答2:


OK, I have a solution for this, but before I do, I ought to mention SoapUI's data driven test capabilities. Have you looked at data sinks and data stores?

Data Sinks allow you to pull specific values out of a particular test step and save them to a file. You can save values from a previous request or its response.

Further down in your tests, you create a data store, which links to the file you saved. The data store object allows you to map columns in your saved file. Once that is set up, you create another request with a standard request, but you can then substitute hard coded values with a value from the data store.

This allows you to create one single test, that can then be called with each row of data in your data store.

For example, you are testing a 'create user' web service. The Web Service has lots of business rules that need to be applied. E.g. Surname not be blank. You could write a separate SoapUI test to verify that each business rule works, but what would be better would be to create a data store that contains the parameters for each test including the expected result.

Using the data store and data loop, you can call you result test step for each row in your data store.

Example test data

Example Flow

All steps between the data source and data loop are called for every row in the data source.

IMHO, these are brilliant as it reduces the number of tests to maintain within SoapUI and if you need to added further scenarios, e.g. no email address, then you just update the spreadsheet your data source is linked to.

Back to pulling the contents of a file into a SoapUi request....

Example flow....

The first step is the original request to some web service.

The second step writes the request to a text file...

def rawRequest = context.expand( '${REST Test Request#RawRequest}' )

def file1 = new File('c:/temp/groovy1.txt');
file1.write (rawRequest);
return rawRequest;

The third step use Groovy to read a file...

def file1 = new File('c:/temp/groovy1.txt');

return file1.text;

Finally, the last step is the request with the file contents. To do this, create the request as normal. In the request tab, delete the body of the request and the use ${} to pull string that the Groovy Script read from file.

${Load Saved Request from File - Groovy Script#result#}

When the test runs, the Grrovy in Step 3, reads the file, but doesn't do anything with it. When SoapUI attempts to run Step 4, it sees the ${} instruction and gets the string returned by the Step 3 groovy script.

So now, you have examples of how to write a request to file, read a file and squirt the contents of a file into a request.




回答3:


I have seen in the past other solution which i still want use it instaed of using rest methods. I think i can have an option so have first THE SOAP request as diasable step and step 2 the groovy as in the screenshot: enter image description here




回答4:


def cur_Time = Calendar.getInstance(TimeZone.getTimeZone('IST')).format("EEE, dd MMM yyyy HH-mm-ss z")

new File("C:\\Users\\SHILPA\\Desktop\\soapUIxls\\RequestPayload"+cur_Time+".doc").write(context.rawRequest)  

It will help to capture request payload with dynamic values passed.

So you can see request and response payloads in soapUIlog

log.info(context.response)
log.info(context.rawRequest)


来源:https://stackoverflow.com/questions/46513082/how-to-copy-a-request-in-soapui-to-a-folder-using-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!