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.
Related
I have an API that contains a customer ID value, that is generated randomly and contains special character.
I need to assert this customer ID using Response Assertion, But due to the special characters , the comparison shows fail .
Example:
API body:
{Customer_ID : "rzrzlk#kad9$l11zr#zz9dr1"
}
My response assertion:
"data":[{"customer_id":"rzrzlk#kad9$l11zr#zz9dr1",
Result:
Assertion failed
I know I should user backslash , but when you dont know where to user it inside the values, it will be useless.
You should use a backslash only if you're escaping regular expressions meta characters
If you just need to ensure that response contains the given value you should use the following setup:
Contains and Matches modes expect a regular expression
Equals and Substring modes expect a plain text string
More information: How to Use JMeter Assertions in Three Easy Steps
Q1 Pagination is applied and I need to validate the response through text response , that value is present or not. SO there are 3 pages due to pagination, so value will appear in one of the three pages. So how and where should i add the response assertion?
Q2 Also if I select(pattern matching rules)as "matches" in response assertion, Then even same values appeared in response, but still assertion is getting failed.
For example: Under (pattern to test) I have added text to validate
"document":{"id":8},
Same value appeared in response after execution
"document":{"id":8},
But still assertion failed. why so?
Thanks in Advance
As per How to Use JMeter Assertions in Three Easy Steps article:
The Pattern can be either be:
a “string” for “Equals” or “Substring” clauses
a “Perl5-style” Regular Expression for “Contains” or “Matches” clauses
So given you chose "Matches" rule you need to use a PCRE, not a string as your input is considered as the Regular Expression
Just switch to "Substring" pattern matching rule and your assertion should start working as expected. If you want to proceed with the regular expression you need to amend your criteria correspondingly:
(?s)^.*"document":{"id":8},.*$
Assert given value:
"action":"GET /api/invoice_status","description":"[NO_AUTH] get
invoice statuses"
Assertion failure message:
Test failed: text expected to contain /"action":"GET
/api/invoice_status","description":"[NO_AUTH] get invoice statuses"/
It is adding / slashes just to represent properly but it's not an issue. Cross verify your response after running the script, there might be some difference in the response and the text which you are passing in assertion. Also, there is no need to pass complete response in assertion, just pass some important text like success, passed (if present in response) or any other text available in your response and click on contains radio button inside Response assertion
Got the solution by using the \ forward slash escape character.
As per How to Use JMeter Assertions in Three Easy Steps
The Pattern can be either be:
a “string” for “Equals” or “Substring” clauses
a “Perl5-style” Regular Expression for “Contains” or “Matches” clauses
So if you choose "Contains" mode - JMeter treats pattern as Regular Expression. Either escape meta characters with a backslash - \ or consider switching the Response Assertion into "Substring" mode instear.
I'm getting a very basic text resp failing in jmeter when it appears it should pass. Feels like some whitespace or character encoding issue, but got me stuck - any pointers would be great.
Applied to: HTTP sampler
Type of assert: Main sample only, text resp
Matching: equals
So I ran the sample with the asset disabled, and copied the content of the resp. body into the patterns to test field within the asset. The context should be static, so assumed it would pass.
However, the assert is tripping with:
Assertion failure message: Test failed: text expected to equal.
URL testing:- http://c1-orig-qa.adis.ws/c/AutomatedQA2/texttype_plain.xml
It's a bad practice to compare full xml as even 2 xmls that differ in whitespace should be equal.
Most probably here the issue is whitespace.
I suggest you just compare some texts line:
- ok
- [CDATA[/c/AutomatedQA2/texttype_plain]]>
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).