How to get certain expected values using SOAP UI Xquery Matches - xquery

I have a webService that returns certain values. I know what those values will be. I want to pick them out of the XML and if those values are true I want the assertion to pass. Imagine that my test passes if I get this result... How can I assert that that is the case?
<BasicPersons>
<id>4</id>
<firstName>Patricia</firstName>
<middleName>A</middleName>
<lastName>Cluss</lastName>
</BasicPersons>
<BasicPersons>
<id>5</id>
<firstName>Benjamin</firstName>
<middleName>L</middleName>
<lastName>Handen</lastName>
</BasicPersons>
<BasicPersons>
<id>6</id>
<firstName>Ellen</firstName>
<lastName>Frank</lastName>
</BasicPersons>
<BasicPersons>

SoapUI provides XPath assertion for test steps that extracts XML element from the response and compare it with expected data.
Let's take you XML fragment as an example (I added root element to make it well-formed). First create in SoapUI new Test Request step and fill it with request XML. Let's then check whether response contains BasicPersons element with id=4 and all other specified fields. Add new assertion XPath Match from Property Content group. Then type in the expression for the check. Here is what I get:
boolean(/root/BasicPersons[id=4 and firstName="Patricia" and middleName="A" and lastName="Cluss"])
Expected Result shall be true if XPath matches XML response. The assertion fails otherwise.
You may create several assertion for testing several persons from your response.

A xpath expression like...
((//*:BasicPersons[1]/*:middleName)='A' and (//*:BasicPersons[1]/*:firstName)='Patricia') and so on for other fields)
This will return TRUE when all condition will match the response.

Related

How to Write Response Assertion Using Groovy in JMeter

I want to write a code for Response assertion using groovy for one of the Request Giving Response data like this
{
"value":"200"
"value_description":"pass"
"value_code":"pass"
"data_encode":"uyt-09-0nbv"
}
after google Search i am getting only with Response Assertion SOAP-UI tools and i also checked with Blaze meter blog i am not understating about what they are saying. simple way i want demonstrate that.write code for Response Data Assert value for 200 is this possible. please help me to this stuff
The relevant Groovy code to check whether value attribute in the response equals 200 would be something like:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def value = json.value
if (value != '200') {
AssertionResult.setFailure(true)
AssertionResult.setFauilreMessage('Expected 200, but got ' + value)
}
Add JSR223 Assertion as a child of the request which returns the aforementioned JSON (it is not valid JSON by the way)
Put the above code into "Script" area (make sure you tick Cache compiled script if available box and choose groovy from the "Language" dropdown)
More information:
Groovy: Parsing and producing JSON
Scripting JMeter Assertions in Groovy - A Tutorial
You can add 4 JSON Extractors, each with different Path Expressions:
$.value, $.value_description, $.value_code, $.data_encode
It will assert that JSON parameter returned.
You can add Regular Expression Extractors to check each variable you got using Apply to JMeter Variable.

Using a substring of a return value in a subsequent request

I'm attempting to construct a series of Paw calls using the variables feature. I have one situation I'm unable to solve.
At authentication into the server I'm using, I get a JSON response, with one value that looks like this:
endpoint = "https://sub.something.com/thingone/thingtwo.php?token=sometoken&id=blahblah"
The endpoint portion "https://sub.something.com/" is then used as the base for subsequent calls, where a call might be "GET https://sub.something.com/data?id=123".
I don't want to hardcode the endpoint in Paw, as the endpoint will vary based on factors I can't predict at my end.
Is there a way to do basic string processing like this either in Paw, or by calling out to a shell script and using the return value of said script as a Paw variable?
That's doable using that RegExp Match dynamic value extension. Click on that previous link and hit Install Extension.
Type "Regexp" in the field you expect this value to be used. Pick Regexp Match from the completion results:
Then enter a regexp that matches your need, https?://[^/]+/? should be good:
I've put your example string in the screenshot above to show that it works, but you can instead put a "pointer" (Response Dynamic Value) to the response you want:
In the choices, pick Response Parsed Body if you want to parse a JSON or XML from the reponse. If the string is simply in plain text in the response body, pick Response Raw Body.
Once these steps are completed, you've got a working "Pointer" + "Parser" to the response that extract the part of the string you need. You can do the same operation with another regex for the token…
Tip: these dynamic value tokens can be selected like text and copy/pasted (Cmd+C/Cmd+V) :-)

Jmeter how to get the dynamic parameter value in the path

JMeter path contents some dynamically generated value.
Eg
[HTTP Request]
[path-home/user?p=er3562]
This "p" value is dynamically generated.
I want to get this parameter value in the first HTTP request path.
This parameter value should pass through each HTTP request path.
I am new to JMeter. Please help me to solve this?
You need to extract it somehow and convert into a JMeter Variable for later reuse. JMeter provides several PostProcessors for extracting data from different responses types, in your case the most suitable one will be Regular Expression Extractor.
Add Regular Expression Extractor as a child of the request which returns that path-home/user?p=er3562 value
Configure it as follows:
Apply to: depending on where the "interesting" value lives, the most "safe" setting is Main sample and sub-samples
Field to check: depending on where the "interesting" value comes from, in the majority of cases it's Body but in your case it may be i.e. URL
Reference Name: anything meaningful, it is JMeter Variable name, if JMeter finds anything it will store the result in a variable named accordingly to this field. I.e. path-home
Regular Expression: Perl5-style regular expression, in your case it would be something like: path-home/user\?p=er(\d+)
Template: if you're looking to extract a single value it will be $1$
Refer extracted value as ${path-home} where required.
References and tips:
You can use Debug Sampler and View Results Tree listener combination to view JMeter Variable names and test regular expressions against actual response
Using RegEx (Regular Expression Extractor) With JMeter
Perl 5 Regex Cheat sheet
JMeter Regular Expressions
By using correlation concept ,
regular expression extractor concept & try this key
p=er(.*?)

Compare responses in SoapUI in order to assert case insensitivity

we have a web service that is supposed to be case insenisitive. So, we want the same result on a search for 'foo' and 'FOO'. What is the best way to create an assertion that compares the response for the two different string parameters?
Thank you!
I would not compare the two results at all. Your tests could just test the response you receive. So if you are testing each response the same you would be verifying the responses match.
If using soapUI open source, I would create two tests that are exactly the same except for the data, probably use a test case property. My requests and response assertions would be the same, but the data is extracted.
If I'm using soapUI Pro, I could have one test case that uses a data sheet, which is alot easier to maintain going forward, but that costs money.
If you want to know what exactly to assert in your test, you will need to look at your requirements and decide which pieces are important to assert. If the answer is the whole request a simple xpath match against the entire soap envelope would work.
If you must, groovy is the answer, I think something like this should work:
def rawRequest = context.expand( '${Teststepname#RawRequest#declare namespace soapenv=\'http://schemas.xmlsoap.org/soap/envelope/\'; //soapenv:Body[1]}' )
def rawRequest2 = context.expand( '${Teststepname2#RawRequest#declare namespace soapenv=\'http://schemas.xmlsoap.org/soap/envelope/\'; //soapenv:Body[1]}' )
if (rawRequest == rawRequest2) {
assert true
}
else {
assert false
}

Access the HTTP Response from xdmp:http-get()

Using MarkLogic to pull in data from a web service with xdmp:http-get() or xdmp:http-post(), I'd like to be able to check the headers that come back before I attempt to process the data. In DQ I can do this:
let $result := xdmp:http-get($query,$options) (: $query and $options are fine, I promise. :)
return $result
And the result I get back looks like this:
<v:results v:warning="more than one node">
<response>
<code>200</code>
<message>OK</message>
<headers>
<server>(actual server data was here)</server>
<date>Thu, 07 Jun 2012 16:53:24 GMT</date>
<content-type>application/xml;charset=UTF-8</content-type>
<content-length>2296</content-length>
<connection>close</connection>
</headers>
</response>
followed by the actual response. the problem is that I can't seem to XPath into this response node. If I change my return statement to return $result/response/code I get the empty sequence. If I could check that code to make sure I got a 200 back before attempting to process the actual data that came back it would be much better than using try-catch blocks to see if the data exists and is sane.
So, if anyone knows how to access those response codes I would love to see your solution.
For the record, I have tried xdmp:get-response-code(), but it doesn't take any parameters, so I don't don't know what response code it's looking at.
You're getting burned by two gotchas at once:
awareness of namespaces
awareness of document nodes
First, the namespace. The XML output of the http-get function is in a namespace as seen by the top-level element:
<response xmlns="xdmp:http-get">
To successfully access elements in that namespace, you need to declare a prefix in your query bound to the correct namespace, and then use that prefix in your XPath expressions. For example:
declare namespace h="xdmp:http-get";
//h:code
Now lets talk about document nodes. :-)
You're trying to access $result as if it is a document node containing an element, but in actuality, it is a sequence of two root nodes (so they're not siblings either). The first one (the one you're interested in here) is a parentless <response> element—not a document containing a <response> element.
This is a common gotcha: knowing when a document node is present or not. Document nodes are always invisible when serialized (hence the gotcha), and they're always present on documents stored in the database. However, when you just use a bare element constructor in XQuery (as the http-get implementation does), you construct not a document node but an element node without a document node parent.
For example, the following query will return the empty sequence, because it's trying to get the <foo> child of <foo>:
declare variable $foo := <foo>bar</foo>;
$foo/foo
On the other hand, the following does return <foo>, because it's getting the <foo> child of the document node (which has to be explicitly constructed, in XQuery):
$declare variable $doc := document{ <foo>bar</foo> };
$doc/foo
So you have to know how a given function's API is designed (whether it returns a document containing an element or just an element).
To solve your problem, don't try to access $result/h:response/h:code (which is trying to get the <response> child of <response>). Instead, access $result/h:code (or more precisely $result[1]/h:code, since <response> is the first of a sequence of two nodes returned by the http-get function).
For more information on document nodes, check out this blog article series: http://community.marklogic.com/blog/document-formats-part1

Resources