JMeter XPath Extractor SAXException - xpath

I am using xpath extractor to retrieve a form attribute value from the response data. However, this response data contains, among other data the String "C&I", and this is causing the following SAXException
jmeter.extractor.XPathExtractor: SAXException while processing
(substring-after(//form[#id='headerForm']/#action,'/dashboard.xhtml?'))
The reference to entity "I" must end with the ';' delimiter.
I do not have any control over this data since it is being obtained from the database. I tried checking the "Use Tidy(tolerant parser)" option. That results in the following warning/error
ERROR - jmeter.util.XPathUtil: TidyException: line 35 column 31 -
Warning: trimming empty <div>
Line 35 of the response is as follows:
`<div style="clear: both;"></div>`
Extracting that attribute value is essential for further processing for me.

As for everything there is solution, it might be quick and dirty but there is always more solutions to a single problem.
I recommend using jsoup to do the parsing of HTML for you instead of xpath extractor. I'm assuming you're trying to extract the particular forms' action attribute.
Step 1 -> Add jsoup-1.6.3.jar or any other version to your JMETER_HOME\lib
Step 2 -> Add a BeanShell PostProcessor to your Sampler HTTP or any other
Step 3 -> In a Script big box paste this code :
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
String html = prev.getResponseDataAsString(); // get response from your sampler
Document doc = Jsoup.parse(html);
String formAction = doc.select("#headerForm").attr("action");
vars.put("action", formAction);
HTML selectors are jquery based. So it can do pretty nice and neat things. Anyway you should have ${action} variable to use further in your tests.
Update
So you don't get tangled with the code I've created jMeter post processor called Html Extractor here is the github url :
https://github.com/c0mrade/Html-Extractor

The stray & in your source document is not legal in XML. From the XML spec (emphasis mine)
The ampersand character (&) and the left angle bracket (<) may appear
in their literal form only when used as markup delimiters, or within a
comment, a processing instruction, or a CDATA section. They are also
legal within the literal entity value of an internal entity
declaration; see "4.3.2 Well-Formed Parsed Entities". If they are
needed elsewhere, they must be escaped using either numeric character
references or the strings "&" and "<" respectively.
The parser is interpreting the & as the start of an entity reference, which it expects to end with a semicolon.
Source:
http://www.w3.org/TR/1998/REC-xml-19980210/#syntax
Note that the error you've included is (almost certainly) unrelated. Tidy is merely pointing out that the div contains no content (i.e. it's empty).

Related

Response assertion failing even though the document does contain the expected text

I have a response assertion validating the 'Document (text)' of a response.
This is the assertion
This is the failure
This is the response that the assertion is checking. As you can see I have directly copied 'Statutory Currency USD Total Tax (SC) 58.80' from the failing assertion and it is found in the response?!
Other assertions are passing for this request.
Notice you can (maybe should) switch to using Substring instead of Contains which will fix your issue, because it doesn't use regex which have reserved characters as (
Contains - true if the text contains the regular expression pattern
Substring - true if the text contains the pattern string (case-sensitive)
Turns out the '(' and ')' needed to be escaped using a '\'.
You’re using document instead of Text response in Fields to test.
Document is usually reserved to asserting on pdf, excel, word document. it has an important cost in terms of performance and is not needed when asserting on text (html, json...)
Also you should favor substring instead of contains.

How to extract the response using a regular expression for Jmeter?

How can I extract the response using a regular expression for Jmeter.
Jmeter is not extracting the response item that I want to extract. I've tried several times, but I failed. How can I extract the response item?
Response data is as shown in the screenshot:
The value which I need to extract has been highlighted.
I tried extracting using the following expression, but it failed:
<h2>.+?<a hfref="http://(.+?)">.+?</a>.</h2>
Please follow the below steps to extract the Id value.
In your Regular Expression Extractor, configure below details. Use the regular expression as Public/FormsPreview.aspx\?Id\=(.+?)& to retrieve the Id.
You can use other unique left/right boundaries as well, as your regular expression.
How to validate your regular expression extractor?
To validate your regular expression extractor, add a Debug Sampler (Right Click on your thread group > Add > Sampler > Debug Sampler)
Execute the test plan
In View Results Tree you can see the c_ID value as shown below.
Depending on how vary can result be (I mean, how the form is changing? is that only querystring Id parameter that is different? could there be another response (than Object Moved) with Id that you don't want to parse? etc.), the regular expression in the Regular Expession Extractor Post-Processor (which fits best here) would be different.
From simplest context-agnostic, Id=(\d+) (that is considering you have numeric Id), to making it case-insensitive (?i)Id=(\d+) and/or alphanumeric Id=([0-9a-zA-Z]+), or even whatever symbols are in (but you'd be forced to start to include context in this case ) Id=(.+?)& - and up to one that uses broader context evaluating the whole multiline stuff, like (?im)<title>Object\smoved</title>.+<a\shref=.+Id=(\d+)
And don't forget to use $1$ as your Template and take the Match 1.
I suggest you to stick to the most narrow and most context-agnostic one you can afford (the very first example here).
Add some assertions to make sure you're on the right page - despite the fact they executed after post-processors, you'd see something is wrong, at least.
You can add regular expression extractor like following

How to pass jmeter response data (ex: getting response 295 without any lable) to next http request url path/body

How to pass jmeter response data (ex: getting response 295 without any lable) to next http request url path/body. This 3 digits/4 digits number is dynamically generated for every run and this value i have to use it for next API calls. Since this value is not having any lable/attribute name not sure how to extract this value. Please suggest.
Regular Expression Configuration:
Reference Name: anything
Regular Expression: (.+)
Template: $1$
Match No.(O for Random): 1
The Reference name should be passed as the variable in the next HTTP request URL path/body.
Screenshot from Regex Test in View Results Tree.
If you need to extract a single numeric value, the relevant regular expression will be as simple as (\d+). See Perl 5 Regex Cheat sheet for quick reference.
If in future you will need a regular expression which return the whole response (including line breaks, special characters, whatever), as per How to Extract Data From Files With JMeter article it will be something like (?s)(^.*)

Jmeter - Regular Expression Extractor

I'm new to jmeter and I'm facing an issue with Regular Expression Extractor.
Details:
Http request: /apps/Account/LogOn/TestFirm
This is the response I've received (I've edited for security purpose).
The response from the result tree appears to be from redirected message, probably its hidden response.
<html><head><title>Object moved</title></head><body>
<h2>Object moved to here.</h2>
</body></html>
!DOCTYPE html>
Now I need to capture SAMLRequest and I've used Regular Expression Extractor with the below information:
Reference Name: SAMLRequest
Regular Expression: SAMLRequest=(.+?)">here
Template: $1$
Match no (0 for random): 1
Default value:
And in the following HTTP Request I've used ${SAMLRequest} and in the request message it goes as ${SAMLRequest} instead of the value. I think nothing is being captured.
Can anyone please help me on how to capture the hidden response as shown above?
Also, what is RelayState? Is it applicable here?
Edit:
191 - is where the SAMLRequest is received from the response.
I have added Regular Expression Extractor, I guess here the value is hidden or something?
193 - I'm using ${SAMLRequest} and in the request body I can see
&SAMLRequest=${SAMLRequest}
instead of the actual value.
You may want to check the "Apply to" checkbox in the Regular expression extractor. This parameter defines which regular expression will be applied to either only main sample results or to the embedded resources too.
The Reg Ex which you have written is correct, Check where exactly this SAMLRequest is generating and use the Reg Ex there.
Try to use the match no as 0 and also make sure that the filed to check is Response Headers(since i could see that the value is generating in the header tag) and the regular expression
You may want to consider doing this with a BSF preprocessor, This will give you the full range of java string parsing operations.
Of course, there will be a way to make this work with a regular expression extractor, but BSF preprocessor, should work equally well, and may allow you to get around a roadblock by using a syntax you are more used to (if you are more used to java)

Invalid Token when using XPath

I am making a modification to a web application using XPath, and when executed I get an error message - Invalid token!
This is basic what I am doing
public xmlNode GetSelection (SelectParams params, xmldocument docment)
{
xpathstring = string.format("Name =\'{0}' Displaytag = \'{1}' Manadatory=\'{2}', params.Name, params.Displaytag, params.Manadatory);
return document.selectsinglenode(xpathstring);
}
As you can see, I am making a string and setting values on the nodes I am trying to find against my xml document, and thus returning xml data that matches my parameters.
What is happening is that I am getting an xpathexeception error in Visual Studio and it says invalid token.
I do know that in the xml document that the parameters I am looking in the tags have double quotes, for example, Name="ABC". So, I thought the problem could be solved using an "\".
Can anyone help?
Update from comments
In the Xml Document, the tag has
attributes where they are set as
Name="ABC" Displaytag="ATag"
Manadatory="true".
I guess you need:
//*[#Name="ABC"][#Displaytag="ATag"][#Manadatory="true"]
Or
//*[#Name="ABC" and #Displaytag="ATag" and #Manadatory="true"]
Meaning: any element in the whole document having a Name attribute with "ABC" value, a Displaytag attribute with "ATag" value and a Manadatory attribute with "true" value.
The string passed as argument to SelectSingleNode() (BTW, the exact capitalization is important) is something like:
Name ='someName' Displaytag = 'someString' Manadatory='true'
This is extremely different than a syntactically legal XPath expression.
And the error message just reflects the fact that toxic food has been given to the XPath engine.
Solution: Do read at least a light XPath tutorial and then specify a correct XPath expression.
The string you are constructing is not a valid XPath expression. In fact, it is nothing like XPath at all.
Indeed, even if it were a valid XPath expression, constructing it this way by string concatenation is a very dangerous practice, because of the possibility of injection attacks. But I suspect that advice will fall on stony ground.

Resources