How to set request body params in karate for soap request [duplicate] - karate

I have the following xml:
<?xml version="1.0" encoding="utf-8"?>
<request>
<head>
<session-id>none</session-id>
</head>
<service name="test">
<function name="testFunc">
<guids>
<guid>#guid#</guid>
<guid>#guid#</guid>
</guids>
</function>
</service>
</request>
I have 2 test cases:
i want to test with single guid.
I want to test with 2 guids.
I created 2 separate xml for both the test cases and it worked.
The question here is how can i use a common xml?
Also i tried using remove but how do i remove single guid from the xml?

There is something called "embedded expressions" in Karate. Read the docs: https://github.com/intuit/karate#embedded-expressions
Example:
* def guids = <guids><guid>one</guid><guid>two</guid></guids>
* def body = <root>#(guids)</root>
Also refer this file, it has a lot of other examples and ideas for XML data-driven tests: xml.feature.

Related

How to create a message containing xml prolog declaration

I am trying to create some dynamic xml messages which will be sent to a service which expects xml. Said service also needs the xml prolog to be specified.
As I want to do a lot of manipulation on the xml I am reading it in a native xml type, however it seems that karate loses the xml prolog information during the conversion (on 0.9.5 and 0.9.6.RC3)
Feature: Example
# https://www.w3.org/TR/xml/#sec-prolog-dtd
Scenario: Example
Given def nativeXML =
"""
<?xml version="1.0" encoding="UTF-8" ?>
<greeting>Hello world</greeting>
"""
When string nativeToString = nativeXML
* print nativeToString
Then assert nativeToString.contains("encoding")
Is there a way to preserve the xml prolog?
Code to reproduce this can be found here: https://github.com/KostasKgr/karate-issues/blob/xml_prolog_to_string/src/test/java/examples/example.feature
Kind regards
Right now there is no other way but to append it manually, which IMO is simple enough. Karate removes it by default during a string conversion, because it gets in the way during Node operations.
Given def temp =
"""
<?xml version="1.0" encoding="UTF-8" ?>
<greeting>Hello world</greeting>
"""
* string body = temp
* def body = '<?xml version="1.0" encoding="UTF-8" ?>' + body
* print body
Then assert body.contains("encoding")

Karate data driven with xml and son [duplicate]

This question already has an answer here:
karate does not match xml with namespaces
(1 answer)
Closed 1 year ago.
I'm new to automation and started with Karate Framework.
I want to parametrize random and countrycode for different test data. How to do that with xml and json?
My json requests are bulky and need multiple parameterizations
Scenario: create User
def createuserrequest = read('classpath:com/imscreateuser/createuser.xml')
Given url imscreateuserurl
And header Content-Type = 'application/json; charset=utf-8'
And request createuserrequest
When method post
Then status 200
<?xml version="1.0" encoding="UTF-8"?>
<ReqBody version="1.9.11" options="prettyOutput">
<req dest="UserManagement" api="createPerson">
<Person>
<countryCode>'#(countrycode)'</countryCode>
<credentials class="Credentials">
<password>P#ssword1</password>
<username>new+'#(random)'#testing.com</username>
</credentials>
</Person>
<AuthRequest />
</req>
</ReqBody>
<countryCode>'#(countrycode)'</countryCode> is wrong. It should be <countryCode>#(countrycode)</countryCode>.
And <username>new+'#(random)'#testing.com</username> is wrong. It should be <username>#('new' + random + '#testing.com')</username>.
Look at this example for XML ideas: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml.feature
For parameterization, you would not be asking this question if you have read the docs. Refer this section: https://github.com/intuit/karate#data-driven-tests
And look at this example: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/outline/examples.feature
Find the Karate demo examples and look at the dynamic-params.feature.

What is the proper way to export data from an ArrayList in moqui

I am trying to export data in XML file. I have several entity from where I retrieve all the necessary data in an ArrayList. I can utilize this ArrayList and export data by embedding Java / groovy code in the script tag. But I like to know is there any other way to export data in Moqui?
I am only aware of the writeXmlText method which can write entity data in XML.
EDIT: This is my sample service
<service verb="get" noun="ExportData" type="script" location="component://PracticeTutorial/service/ExportTutorialServices/getExportData.groovy">
<out-parameters>
<parameter name="employees" type="List">
<parameter name="employee" type="Map">
<parameter name="empId"/>
<parameter name="firstName"/>
<parameter name="lastName"/>
<parameter name="designation"/>
</parameter>
</parameter>
</out-parameters>
</service>
The sample Groovy script:
import org.moqui.entity.EntityList
import org.moqui.entity.EntityValue
employees = []
EntityList employeeList = ec.entity.makeFind("tutorial.Employee").list()
for(EntityValue ev : employeeList) {
employees.add([empId:ev.empId, firstName:ev.firstName,
lastName:ev.lastName, designation:ev.designation])
}
I called the service like this:
<transition name="export">
<actions>
<service-call name="ExportTutorialServices.get#ExportData" in-map="context" out-map="context"/>
</actions>
<default-response url="."/>
</transition>
Question 1: How should I access the employees ArrayList from the screen after calling the service?
Question 2: Is there any simpler way to export data from ArrayList employees? I want to produce output like this.
Expected output:
<employees>
<employee>
<id>001</id>
<firstName>John</firstName>
<lastName>Doe</lastName>
<designation>Developer</designation>
</employee>
</employees>
Thanks
In short it sounds like you want to create an XML document with a specific structure, ultimately based on data from a database.
There are various tools for doing this in Java, the Groovy Node and XML API being a pretty good one. Appending strings to a Writer or StringBuilder is sometimes a good way to go, because the output format is so simple (for smaller documents, or those that follow particular patterns anyway).
For more complex documents, such as if the list you mentioned is one of dozens or the elements are nested a few levels deep, then I'd recommend using an FTL template to generate the XML and a Moqui Screen with actions to call your data prep service, and a screen.widgets.render-mode element to include the FTL template.
With the FTL wrapped in a screen approach you can use the ScreenFacade.makeRender() method to create a ScreenRender object. On that object set the root screen location, a render mode of "xml", other options as desired, and then call the render(Writer) method or the render() method that returns a String. With these methods you can stream the text to a file, send it over a web request, or whatever.

Tsung Issue with Dyn_Variable

I am very new to ERLANG and TSung, I never worked in this areas, but I am very much keen to know the fundamentals and do distributed load test for my web application. I am in half the way to complete, but I have a big hurdle and not able to moving forward , please read below tsung.xml file and advise me where & what I am missing?
**===> tsung.xml (this file perfectly working without any errors)**
*<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd">
<tsung loglevel = "debug" dumptraffic="true" version="1.0">
<clients>
<client host="localhost" weight ="1" maxusers="40000" cpu = "1" >
<ip value = '127.000.000.111'/>
</client>
</clients>
<servers>
<server host="127.000.000.112" port="80" type="tcp"></server>
</servers>
<load duration="1" unit="minute">
<arrivalphase phase="1" duration="1" unit="minute">
<users arrivalrate="10" unit="second"></users>
</arrivalphase>
</load>
<sessions>
<session name="mySession" probability="100" type="ts_http">
<transaction name="trx">
<request>
<dyn_variable name="myId" re="<myId>(\.*)\</myId>"/> <-- Trying with RegExp option, not getting the value myId
<!--dyn_variable name="myId" xpath="//response/myId" /--> <-- Trying with xpath option, not getting the value myId
<!--dyn_variable name="myId" jsonpath="response.myId" /--> <-- Trying with jsonpath option, not getting the value myId
<http url='http://127.000.000.112/Create_Rec' method='POST' version='1.1' content_type='text/xml'/>
</request>
<request subst="true">
<http url='http://999.000.000.999/Get_Rec/myId=%%_myId%%' method='GET' version='1.1' content_type='application/xml'/>
</request>
</transaction>
</session>
</sessions>
</tsung>*
When I run this url (it is web service call) "http://_127.000.000.112/Create_Rec" in the web browser, I get the following similar response from Server (in the back ground it creates the record in database and generates new id i.e. myId). When I run above tsung.xml, the first request working perfectly fine as I expected.
===> response (browser response)
<response id="SomeWebService">
<status>
<statusCode>1</statusCode>
<statusMsg>SomeMessage</statusMsg>
<statusTime>2013-06-20 02:52:25</statusTime>
</status>
<myId>298346728934734987</myId>
</response>
What I am looking here, I need to grab the myId from first request and pass into second request myId=%%_myId%%, but it is never working and myId always empty string. I am beyond of dyn_variable since two days, no clue and proper examples/documentation on it. Please suggest me, what I am missing.
You will have to set up the subst="true" in your request for substitution to work. So, your request should change to..
<request subst="true">
If still it doesn't work then I would suggest you to see the tsung.dump file and check the response which you are getting from server

MSBuild XMLUpdate Question

I am using the XMLUpdate to update an xml formatted file in MSBuild. It updates fine but adds <?xml version="1.0" encoding="utf-8"?> at the top after update. Here is my statement that updates
<Import Project="C:\Program Files\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<XmlUpdate XmlFileName="$(AppName).alx" Xpath="/loader/application/version" Value="$(AppVersion)" />
Is it possible to update without the xml element at the top?
Thanks
Ponnu
The <?xml ...> is more of a descriptor than a real XML element. It describes your document and, for example defines the encoding. It won't interfere with your existing elements. I think it is even a standard feature of a XML document (but I don't have the specs handy)