I have used regular expression to remove few unwanted items in my XML. Now, I need to print all includedService nodes which contain multiple child elements inside.
Here's code i used;
* string test = read('classpath:PP1/data/Test.xml')
* string test= test.replaceAll("xsi:nil[^/]*", "")
* def Complete_XML = test
* def included = $Complete_XML/Envelope/Body/getPricePlanResponse/pricePlanSummary/includedService
* print included
If i run this, I get below response.
20:19:54.169 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] [
[#document: null],
[#document: null]
]
However, I can print selected elements outside of includedService node. Please help!
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/>
<S:Body>
<ns10:getPricePlanResponse>
<ns10:pricePlanSummary>
<ns5:descriptionFrench>Forfait Montre Affaires</ns5:descriptionFrench>
<ns4:category/>
<ns4:effectiveDate>2009-11-05</ns4:effectiveDate>
<ns4:serviceDataSharingGroupList>
<ns4:dataSharingGroupCode>CAD_DATA</ns4:dataSharingGroupCode>
<ns4:contributingInd>true</ns4:contributingInd>
</ns4:serviceDataSharingGroupList>
<ns4:feature>
<ns5:descriptionFrench>Service</ns5:descriptionFrench>
<ns4:poolGroupId/>
</ns4:feature>
<ns4:recurringCharge>10.0</ns4:recurringCharge>
<ns4:ppsStorageSize>0</ns4:ppsStorageSize>
<ns4:includedService>
<ns4:term>0</ns4:term>
<ns4:brandId>1</ns4:brandId>
<ns4:feature>
<ns5:code>MBAPN</ns5:code>
<ns4:type/>
<ns4:additionalNumberRequiredInd>false</ns4:additionalNumberRequiredInd>
</ns4:feature>
<ns4:recurringCharge>0.0</ns4:recurringCharge>
<ns4:callingCircleFeaturesInd>false</ns4:callingCircleFeaturesInd>
</ns4:includedService>
<ns4:includedService>
<ns4:term>0</ns4:term>
<ns4:brandId>1</ns4:brandId>
<ns4:feature>
<ns5:code>MBAPN</ns5:code>
<ns4:type/>
<ns4:additionalNumberRequiredInd>false</ns4:additionalNumberRequiredInd>
</ns4:feature>
<ns4:recurringCharge>0.0</ns4:recurringCharge>
<ns4:callingCircleFeaturesInd>false</ns4:callingCircleFeaturesInd>
</ns4:includedService>
<ns4:availableTermInMonths>0</ns4:availableTermInMonths>
</ns10:pricePlanSummary>
</ns10:getPricePlanResponse>
</S:Body>
</S:Envelope>
Keep in mind XML always has to be wrapped inside some element or the other. Try this:
* def included = $xml/Envelope/Body/getPricePlanResponse/pricePlanSummary//includedService
* def root = <root></root>
* def fun = function(x, i){ karate.set('root', '/includedService[' + (i + 1) + ']', x) }
* eval karate.forEach(included, fun)
* print root
You should get:
<root>
<ns4:includedService>
<ns4:term>0</ns4:term>
<ns4:brandId>1</ns4:brandId>
<ns4:feature>
<ns5:code>MBAPN</ns5:code>
<ns4:type/>
<ns4:additionalNumberRequiredInd>false</ns4:additionalNumberRequiredInd>
</ns4:feature>
<ns4:recurringCharge>0.0</ns4:recurringCharge>
<ns4:callingCircleFeaturesInd>false</ns4:callingCircleFeaturesInd>
</ns4:includedService>
<ns4:includedService>
<ns4:term>0</ns4:term>
<ns4:brandId>1</ns4:brandId>
<ns4:feature>
<ns5:code>MBAPN</ns5:code>
<ns4:type/>
<ns4:additionalNumberRequiredInd>false</ns4:additionalNumberRequiredInd>
</ns4:feature>
<ns4:recurringCharge>0.0</ns4:recurringCharge>
<ns4:callingCircleFeaturesInd>false</ns4:callingCircleFeaturesInd>
</ns4:includedService>
</root>
For other ideas, refer this answer: https://stackoverflow.com/a/53553979/143475
Related
For some reason a variable with a / character get converted to a \/, how do I prevent this?
I start a echo server that listens on localhost:3000 by running npx http-echo-server
I execute the following:
code:
* def CHALLENGE_USER = '/abc/user'
* def loginJson = { user: '#(CHALLENGE_USER)' , name: 'Some Name'}
* print loginJson
* def TEST_URL = 'http://localhost:3000'
Given url TEST_URL+'/session/loginresponse'
And header Content-Type = 'application/json'
And request loginResponseJson
And method put
Then status 200
It prints { "user": "/abc/user", "name": "Some Name" } like I expect.
The http server logs show "--> {"user":"/schemes/ATT_5_55/CH_1","name":"Some Name"}"
Karate shows the result of the echo {"user":"\/abc\/user","name":"Some Name"}
I have tried:
def CHALLENGE_USER = '/abc/user'
def CHALLENGE_USER = "/abc/user"
def CHALLENGE_USER = '/abc/user'
def CHALLENGE_USER = '//abc//user'
also setting the variable after the fact does not work:
* def loginJson = { name: 'Some Name'}
* loginJson.user = CHALLENGE_USER
Yes this is legal as per the JSON spec: JSON: why are forward slashes escaped?
And the Java libraries we use does that.
Does your server have a problem ? If so - then you have a bug that Karate surfaced.
And if you really want to have full control over the request, please use text but IMO it may be a waste of time: https://stackoverflow.com/a/68344856/143475
A nasty workaround, please forgive me Peter Thomas.
You can convert the json to a string and then remove the \ characters.
I only have one use case for this thank goodness.
* def CHALLENGE_USER = '/abc/user'
* def loginJson = { user: '#(CHALLENGE_USER)' , name: 'Some Name'}
* string json = loginJson
* def loginJsonText = json.replaceAll("\\", "")
* print loginJson
* def TEST_URL = 'http://localhost:3000'
Given url TEST_URL+'/session/loginresponse'
And header Content-Type = 'application/json'
And request loginJsonText
And method put
Then status 200
so basically I am just starting with karate testing framework and and probably missing something really simple, but I cant seem to get an embedded expression to be resolved properly. If I have a feature file like so that does the same thing a couple ways:
Feature: Test Service
Background:
* url 'http://testurl:8080'
* def localDateTime = Java.type('java.time.LocalDateTime')
Scenario: Successful request
* def createDateTime = LocalDateTime.now()
* def testRequest =
"""
{
createDateTime: "#(createDateTime)",
expiryDateTime:"#(localDateTime.now().plusMinutes(5))"
}
"""
* print testRequest
* set testRequest.createDateTime = createdTime
* print testRequest
Then when it gets to the print lines, I get output like this where the values are empty js objects {}
16:43:28.580 [main] INFO com.intuit.karate - [print] {"createDateTime":{},"expiryDateTime":{}}16:43:28.586 [main] DEBUG com.jayway.jsonpath.internal.PathCompiler - Using cached path: $true[]
Also, I can see where it appears to be setting the path for the first print statement like so:
16:32:30.612 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['createDateTime']
16:32:30.613 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['createDateTime'] new value 2017-09-14T16:32:30.566
16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['expiryDateTime']
16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['expiryDateTime'] new value 2017-09-14T16:37:30.621
Can anybody please explain to me why I am unable to get the actual dates inserted into the testRequest?
Thank you.
I think your problem will be solved if you convert those date objects to strings.
* def LocalDateTime = Java.type('java.time.LocalDateTime')
* def createDate = LocalDateTime.now() + ''
* def expiryDate = LocalDateTime.now().plusMinutes(5) + ''
* def testRequest = { createDateTime: '#(createDate)', expiryDateTime: '#(expiryDate)' }
* print karate.pretty(testRequest)
The above is working for me and this is the output:
06:11:47.010 [main] INFO com.intuit.karate - [print] {
"createDateTime": "2017-09-15T06:11:46.983",
"expiryDateTime": "2017-09-15T06:16:46.990"
}
This question already has an answer here:
Karate assert - I'm trying to extract a value from HTML
(1 answer)
Closed 1 year ago.
I am getting response as below:
How to extract value of attribute xmlns as "https://www.w3schools.com/xml/"?
def response =
"""
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
soap:Body
37.7777777777778
</soap:Body>
</soap:Envelope>
"""
Sometimes it is better to convert to JSON. Try this example:
* def response =
"""
<soap:Envelope xmlns:soap="schemas.xmlsoap.org/soap/envelope" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema"> <soap:Body> <FahrenheitToCelsiusResponse xmlns="w3schools.com/xml"> <FahrenheitToCelsiusResult>37.7777777777778</FahrenheitToCelsiusResult> </FahrenheitToCelsiusResponse> </soap:Body> </soap:Envelope>
"""
* json response = response
* def ns = response['soap:Envelope']['_']['soap:Body'].FahrenheitToCelsiusResponse['#'].xmlns
* match ns == 'w3schools.com/xml'
Now you will be able to get any attribute you want.
Also read this: https://github.com/intuit/karate#print - especially the part about internally XML being JSON so if you don't print correctly, you won't see what you want.
I have this query which fails on this extract value
EXTRACTVALUE(xmltype(REQUEST), '/soapenv:Envelope/soapenv:Body/ns2:getCustomerRequestRetrieve/ns2:requestHeader/ns5:referenceID', 'xmlns:soapenv:http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns8="ca/bell/oms/autotype/customerrequestretrieve" xmlns:ns2="ca/bell/oms/autotype/customerrequestretrieve"') RequestReferenceID
Somewhere i am setting the wrong path. can you let me know where i am making a error
The actual XML is
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns2:getCustomerRequestRetrieve xmlns = "ca/bell/oms/autotype/omscommonresponse"
xmlns:ns5 = "sa/cell/oms/autotype/omscommonrequest"
xmlns:ns2 = "sa/cell/oms/autotype/customerrequestretrieve"
xmlns:ns3 = "sa/cell/oms/autotype/omscommon"
xmlns:ns4 = "sa/cell/oms/customerprofile">
<ns2:requestHeader>
<ns5:customerInteractionType>CustomerNotification</ns5:customerInteractionType>
<ns5:serviceRequestUserId>null</ns5:serviceRequestUserId>
<ns5:serviceConsumer>khg</ns5:serviceConsumer>
<ns5:serviceRequestTimestamp>2019-02-05T03:50:12.000-05:00</ns5:serviceRequestTimestamp>
<ns5:language>English</ns5:language>
<ns5:referenceID>Tjutrf7T78H4</ns5:referenceID>
<ns5:transactionIdentifier>eed7ffe0-da22-498f-9913-c2279d1549356612606</ns5:transactionIdentifier>
</ns2:requestHeader>
<ns2:searchCriteria>
<ns2:requestIdentifier>
<ns2:orderNumber>TTjutrf7T8H4</ns2:orderNumber>
</ns2:requestIdentifier>
</ns2:searchCriteria>
<ns2:filterCriteria>
<ns2:fullOrderDetail>ContactOnly</ns2:fullOrderDetail>
</ns2:filterCriteria>
</ns2:getCustomerRequestRetrieve>
</soapenv:Body>
</soapenv:Envelope>
enter code here
You have mistakes in namespaces declaration.
EXTRACTVALUE(REQUEST, '/soapenv:Envelope/soapenv:Body/ns2:getCustomerRequestRetrieve/ns2:requestHeader/ns5:referenceID'
,'xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns2="sa/cell/oms/autotype/customerrequestretrieve"
xmlns:ns5="sa/cell/oms/autotype/omscommonrequest"
') RequestReferenceID
Moreover EXTRACTVALUE function is deprecated. Replacemnet for it is xmlquery,xmltable.
select xmlcast(xmlquery('declare namespace soapenv="http://schemas.xmlsoap.org/soap/envelope/";
declare namespace ns2="sa/cell/oms/autotype/customerrequestretrieve";
declare namespace ns5="sa/cell/oms/autotype/omscommonrequest";
/soapenv:Envelope/soapenv:Body/ns2:getCustomerRequestRetrieve/ns2:requestHeader/ns5:referenceID/text()' passing xmltype(request) returning content) as varchar2(100)) from ....;
I am trying the Force.com Streaming API demo and I get the following error:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body><soapenv:Fault>
<faultcode>UNKNOWN_EXCEPTION</faultcode>
<faultstring>UNKNOWN_EXCEPTION: Element type "soapenv:Envelope" must be followed by either attribute specifications, ">" or "/>". </faultstring>
<detail>
<sf:UnexpectedErrorFault xsi:type="sf:UnexpectedErrorFault">
<sf:exceptionCode>UNKNOWN_EXCEPTION</sf:exceptionCode>
<sf:exceptionMessage>Element type "soapenv:Envelope" must be followed by either attribute specifications, ">" or "/>".</sf:exceptionMessage>
</sf:UnexpectedErrorFault>
</detail>
</soapenv:Fault></soapenv:Body>
</soapenv:Envelope>
Line 22 has a misplaced double quote that is not caught by your compiler.
Replace this code:
private static final String ENV_START =
"
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xmlns:urn='urn:partner.soap.sforce.com'><soapenv:Body>";
With this code:
private static final String ENV_START =
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'"
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xmlns:urn='urn:partner.soap.sforce.com'><soapenv:Body>";