Karate data driven with xml and son [duplicate] - automation

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.

Related

Karate "match contains" for xml does not work like JSON [duplicate]

This question already has an answer here:
How to compare XML response with Json in Karate
(1 answer)
Closed 1 year ago.
I've noticed that "match contains" for checking if xml structure contains in another xml structure works differently than json counterpart. To give you an example :
* def expectedResult =
"""
<?xml version="1.0" encoding="UTF-8"?>
<response>
<id>1</id>
<id>2</id>
<id>3</id>
</response>
"""
* header Accept = xml
Given path 'some\path'
When method GET
Then status 200
## If response contains -
## <response><id>1</id></response>
## The following match will fail with -
## "actual and expected arrays are not the same size - 1:3"
And match response contains expectedResult
If it's a JSON structure, it will pass because 1 contains in the list of 1,2,3. But in XML, it's still checking array size. Is that how it suppose to work?
Update : This is not the same question as the other question. I'm trying to compare reference XML file that I have with XML structure being returned from the API. The question you linked to is comparing JSON reference file to XML response. So, that begs the question, in karate, do I have to convert my reference XML and response XML to json just to perform "contains"?
XML has some fundamental "shape" differences from JSON. I leave it to you to figure out, one of the challenges is the concept of a "root" tag, which JSON does not have. So they will not "match up" the way you may expect.
One tip - convert XML to JSON if in doubt. Here is an example:
* def expected =
"""
<?xml version="1.0" encoding="UTF-8"?>
<response>
<id>1</id>
<id>2</id>
<id>3</id>
</response>
"""
* json temp = $expected/response
* match temp.response.id == ['1', '2', '3']

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

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.

Karate: XML element changed to have empty namespace

when a xml message define like below, karate adds empty namespace to child element.
Karate feature
Feature: test xml
Scenario: test xml empty namespace
* def x =
"""
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<validateEmailAddressRequest xmlns="anotherUri">
<request>
<email>some#domain.com</email>
</request>
</validateEmailAddressRequest>
</soapenv:Body>
</soapenv:Envelope>
"""
* print x
Output
16:39:17.009 [main] INFO com.intuit.karate - [print] <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<validateEmailAddressRequest xmlns="anotherUri">
<request xmlns="">
<email>some#domain.com</email>
</request>
</validateEmailAddressRequest>
</soapenv:Body>
</soapenv:Envelope>HTML report: (paste into browser to view) | Karate version: 0.9.4
if you look at the <request> element in the output it's now has the empty namespace where in feature does not.
That does create a problem when SOAP request hitting the server.
Do you know why this behaviour and how can I override it?
This is very interesting and I just tried this and don't see this problem at all. My guess is that the version of the Java runtime you are using may have some bug in the XML libraries. Can you do us a favor and replicate this problem using this process ? https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue - and make sure you specify the exact OS + Java version.
Meanwhile as a workaround, you can do this - convert the XML to a string, do a Java String.replace() and then re-convert:
* xmlstring x = x
* xml x = x.replace(' xmlns=""', '')
* print x
EDIT - here is the issue that provides a solution (available in 1.2.0 onwards) https://github.com/karatelabs/karate/issues/1587
* configure xmlNamespaceAware = true

How to get ID's of the records in Cross-reference field using Web API in RSA Archer?

I need to get the ID's of the records in a cross-reference field using Web API. Is there an API method to solve this type of task? Or at least I want to know, how to get the value of a specific field?
There are many different methods described in the documentation for operating with list fields (such as GetValuesListValue) and I wonder if there a same way to solve my task.
I can use ExecuteSeach method, but it isn't very convenient.
Alexander, you can either use the REST or Webservices APIs.
REST APIs
Using the Get content by id, /api/core/content/*contentid*
Then you can pass OData to just get the field (id) contents by passing the following in the body
{"Value":"?$filter=FieldId eq '*field id of cross-reference field*'"}
Webservices APIs
You can call the /ws/record.asmx GetRecordById passing the following
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetRecordById xmlns="http://archer-tech.com/webservices/">
<sessionToken>session token</sessionToken>
<moduleId>int</moduleId>
<contentId>int</contentId>
</GetRecordById>
</soap:Body>
</soap:Envelope>
Then you'd have to iterate through the returned XML to get the field contents.

Inuit TimeActivity Create: The request sent by the client was syntactically incorrect

I am trying to test my ability to import TimeActivity records and even when using the sample Create Request XML for TimeActivity I get an error:
Apache Tomcat/7.0.23 - Error report HTTP Status 400 - type Status reportmessage description The request sent by the client was syntactically incorrect ().Apache Tomcat/7.0.23
below is my sample xml create request (pulled from https://developer.intuit.com/docs/0025_quickbooksapi/0050_data_services/v2/0400_quickbooks_online/timeactivity)
<?xml version="1.0" encoding="utf-8"?>
<TimeActivity xmlns:ns2="http://www.intuit.com/sb/cdm/qbo" xmlns="http://www.intuit.com/sb/cdm/v2">
<TxnDate>2011-08-03-07:00</TxnDate>
<NameOf>Vendor</NameOf>
<Vendor>
<VendorIdidDomain="QBO">3793</VendorId>
</Vendor>
<CustomerId>3794</CustomerId>
<ItemId>3</ItemId>
<ClassId>1</ClassId>
<StartTime>2011-08-30T11:45:00-07:00</StartTime>
<EndTime>2011-08-30T13:15:00-07:00</EndTime>
<Taxable>true</Taxable>
<HourlyRate>10.5</HourlyRate>
<BillableStatus>Billable</BillableStatus>
<Hours>10</Hours>
<Minutes>5</Minutes>
<Description>This is a Description</Description>
</TimeActivity>
What am I doing wrong here?
Sharing a sample TimeActivity create request. Please check other reference attributes like Vendor, Class etc. It should work.
<?xml version="1.0" encoding="utf-8"?>
<TimeActivity xmlns='http://www.intuit.com/sb/cdm/v2'>
<TxnDate>2012-08-30-07:00</TxnDate>
<NameOf>Employee</NameOf>
<Employee>
<EmployeeId>11</EmployeeId>
</Employee>
<CustomerId>2</CustomerId>
<JobId>4</JobId>
<BillableStatus>Billable</BillableStatus>
<HourlyRate>50.0</HourlyRate>
<StartTime>2012-08-30T11:45:00-07:00</StartTime>
<EndTime>2012-08-30T13:15:00-07:00</EndTime>
<Description/>
</TimeActivity>
There should be couple of small changes in the sample request ( We will update the doc )
<?xml version="1.0" encoding="utf-8"?>
<TimeActivity xmlns:ns2="http://www.intuit.com/sb/cdm/qbo" xmlns="http://www.intuit.com/sb/cdm/v2">
<NameOf>Vendor</NameOf>
<Vendor>
<VendorId idDomain="QBO">5</VendorId>
</Vendor>
<CustomerId>2</CustomerId>
<ItemId>3</ItemId>
<Taxable>true</Taxable>
<HourlyRate>10.5</HourlyRate>
<BillableStatus>Billable</BillableStatus>
<Hours>10</Hours>
<Minutes>5</Minutes>
<Description>This is a Description</Description>
</TimeActivity>
First replace all the reference values with the same of your test data.
For ex - VendorId, Class, Item and Customer
There are two bugs in the sample request.
1. There should be a space between 'VendorId' and 'idDomain' ( Ref -5th line of the req )
2. According to the doc, " Either method of indicating duration can be entered, but not both. Hours/Minutes OR StartTime/EndTime is required."
So plz remove any one of these. ( I've removed StartTime/EndTime )
Otherwise you'll get the error which you have mentioned above.
That is - Response code 400, Error msg - TIME_ACTIVITY_MULTIPLE_DURATION_TYPES_PRESENT
Please let us know if the above changes work for you.
I tried the same XML in Api Explorer. It worked fine for me. PFB snapshot. Please check if you are facing any encoding issue.
Please let me know if it works for you.