Jmeter: Xpath to get text upto certain number of characters - jmeter

From below snippet I want to get title as My status Report(ABCDEFGH12160916)
I have thousands of titles in my html.
//td[#class="dealertitle"]//text() -- this gets me
My status Report (ABCDEFGH12160916)
* Live, Billable, CRM *
I have also tried
//td[#class="dealertitle"]//text()//substring-before(text(),')')---
Jmeter does not allow me to use substring-before. It says unknown node type
substring-before
Can someone please help me.
I want to get this text till the end - My status Report (ABCDEFGH12160916)
<html>
<head>
<body>
<table class="secondhead" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="title">
My status Report (ABCDEFGH12160916)
<span style="font-size:14pt;color:#00FF00"> * Live, Billable, CRM *
</span>
</td>
</tr>
</tbody>
</table>
</body>
</head>
</html>

//td[#class="dealertitle"]//text()[1]

You have an extra / in your XPath query
Your query:
//td[#class="dealertitle"]//text()
Correct query:
//td[#class="dealertitle"]/text()
Demo:
Explanation: as per XPath Syntax article
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
See Using the XPath Extractor in JMeter guide for more details on using XPath for correlation in JMeter tests.

Related

regex using dynamic input in Jmeter(regex extractor)

I have a query regarding Jmeter regex extractor. I am trying to implement 1 scenario however not able to do same. Below are the details:
Requirement :
In Jmeter I have defined user defined variable : String VAR = KZ
now I am trying to use Regex extractor so that from the HTML response, regex will match VAR value in HTML(defined below) and will fetch span class name, as I need to set checkbox ON for KZ.
Requirement is to handle checkbox ON functionality through user defined variable, that means I don't want to hardcode class name instead based on user defined variable(which will be td value i.e. in this example KZ) I have to fetch class name using Regex Extractor. Could someone please help how to proceed?
Below is HTML Code:
<tr class="trClass">
<td style="width: 13.5%;">
<span class="checkbox"><input id="ctl00ctl94" type="checkbox" name="$ctl95$"
onclick="return validatecheck();" /></span>
</td>
<td style="width: 41.2%;"> KZ </td>
<td style="width: 0%; display: none;"> 5581357 </td>
<td style="width: 32%;"> 06/03/2018 2:22:38 PM </td>
</tr>
<tr class="trClass">
<td style="width: 13.5%;">
<span class="checkbox"><input id="ctl00ctl95" type="checkbox" name="$ctl95$"
onclick="return validatecheck();" /></span>
</td>
<td style="width: 41.2%;"> TM </td>
<td style="width: 0%; display: none;"> 5581358 </td>
<td style="width: 32%;"> 06/03/2018 2:22:38 PM </td>
</tr>
<tr class="trClass">
<td style="width: 13.5%;">
<span class="checkbox"><input id="ctl00ctl96" type="checkbox" name="$ctl96$"
onclick="return validatecheck();" /></span> </td>
<td style="width: 41.2%;">TR </td>
<td style="width: 0%; display: none;"> 5581359 </td>
<td style="width: 32%;"> 06/03/2018 2:22:38 PM </td>
</tr>
Using regular expressions for parsing HTML is not the best idea as:
they are hard to develop and/or maintain
they are very sensitive to markup change hence fragile, i.e. if order of attributes changes or something will go to a new line - it will simply ruin your regex
So I would recommend going for another post-processor which can work with DOM directly, for instance XPath Extractor
The relevant XPath query which will fetch the classname of span which is above the KZ text would be something like:
//td[contains(text(),'KZ')]/preceding::*/span/#class
Of course you can substitute KZ with the JMeter Variable reference, i.e.
//td[contains(text(),'${VAR}')]/preceding::*/span/#class
However you will not be able to test your queries using XPath Tester mode of the View Results Tree listener, you will have to go for Debug Sampler instead to visualize the resulting variable.
Check out XPath Tutorial and Using the XPath Extractor in JMeter guide to get familiarized with XPath language.
Also be aware that according to JMeter project main page:
JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages.
So I don't believe fetching the span classname will solve your problem, most probably you will need to send underlying input name as a parameter so you should be looking for
//td[contains(text(),'KZ')]/preceding::*/span/input/#name

Jmeter: normalize-space to remove white spaces

How do I remove extra spaces in tile ? Using below throws an error as unknown
node:normalize-space. this is the one I tried.
//td[#class="title"]/text()/normalize-space(.)
<html>
<head>
<body>
<table class="secondhead" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="title">
My status Report (ABCDEFGH12160916)
<span style="font-size:14pt;color:#00FF00"> * Live, Billable, CRM *
</span>
</td>
</tr>
</tbody>
</table>
</body>
</head>
</html>
As per XPath functions reference
fn:normalize-space(string)
fn:normalize-space()
Removes leading and trailing spaces from the specified string, and replaces all internal sequences of white space with one and returns the result. If there is no string argument it does the same on the current node
Example: normalize-space(' The XML ')
Result: 'The XML'
So you should be using the following expression instead:
normalize-space(//td[#class="title"]/text())
Check out Using the XPath Extractor in JMeter guide to learn more about dealing with XPath and JSON Path in JMeter

XPATH Firebug filter does not filter as expected

Basically I have a list of presidents and I am only interested in the Nixon link and not Clinton or Obama.
What I find is that filtering as I have done returns the correct number of presidents (ie 1 in this case) but returns ALL of the a links instead of just the one for Nixon.
HTML:
<div class="headlineBlock">
<h2>Obama</h2>
<p class="tudor"><strong>Conditions:</strong> Always sunny </p>
<table class="resultGrid"><tr> <td class="first">
<h4><a href="http://www.thelinkiwant.com?params" title="Click to view result"</a></h4>
<div class="headlineBlock">
<h2>Nixon</h2>
<p class="nixon"><strong>Conditions:</strong> Sometimes late </p>
<table class="resultGrid"><tr> <td class="first">
<h4><a href="http://www.thelinkiwant.com/?params" title="Click to view result"</a></h4>
<div class="headlineBlock">
<h2>Clinton</h2>
<p class="tudor"><strong>Conditions:</strong> Never rainy </p>
<table class="resultGrid"><tr> <td class="first">
<h4><a href="http://www.thelinkiwant/?params" title="Click to view result"</a></h4>
XPATH:
$x("//div[#class='headlineBlock']/h2[not(contains('|Clinton|Obama|',concat('|',.,'|') ))]//../../table/a/#href")
There are several issues with your example.
There brackets missing after ever single "Click to view result", your "headlineBlock" divs and tables aren't closed, etc. So first you should make sure that your data is well formatted.
W3C's Xml Validator can help with that
Your XPath looks mostly ok, I think the issue is with the // at the end - they are a bit too early. Try this instead:
//div[#class='headlineBlock']/h2[not(contains('|Clinton|Obama|',concat('|',.,'|') ))]/..//a/#href
//div[#class='headlineBlock']
All divs of class headlineBlock ...
/h2[not(contains('|Clinton|Obama|',concat('|',.,'|') ))]
... that don't contain certain terms.
/..
Up one level (now we are at div headlineBlock again)
//a
Any direct descendants of element type a
/#href
H-Ref Attribute

Nokogiri stopped searching for sub tags

I was writing my ruby (2.0) script which reads a web page & it was working fine. Then I installed ruby 2.2 and suddenly my nokogiri stopped searching sub tags but not all. It still finds some sub tag and some aren't just being found in the same script.
I reverted back to 1.9 but its still doing the same thing. Currently I have installed ruby 2.1.6. I am loading pages with Watir-webdriver and open-uri. The following page is being opened by open-uri.
For an example, here is my code:
htmlPage = '<html>
<head></head>
<body>
<table width="100%" border="1">
<tbody>
<tr valign="top">
<td width="38%" bgcolor="#EFEFEF">
<b>
<font size="4">NPL Listing History</font>
</b>
</td>
<td width="62%" bgcolor="#EFEFEF">
<b>
<font size="4">Dates</font>
</b>
</td>
</tr>
</tbody>
</table>
</body>
<html>'
page = Nokogiri::HTML(htmlPage)
puts page.css("table [border='1']")
This should get me the table with border 1 but I get nil/null.
Am I doing something wrong? Or something I am missing?
Thanks!
Your problem is the space. In CSS selectors whitespace is the descendant combinator, so table [border='1'] means "all descendants of a table that have a border attribute equal to 1. What you want is table[border='1'], which means "all table elements that have a border attribute equal to 1."

How to get all the nodes which are coming after a particular tag using Nokogiri

I want to fetch all the HTML tags which are coming after the particular tag. For example:
<html>
<body>
<p>one</p>
<u><p>Two</p></u>
<b><p>Three</p></b>
<p>Four</p>
<table>
<tr><td>Five</td></tr>
<tr><td>Six</td></tr>
</table>
</body>
</html>
I want all the HTML tags which are coming after <u><p>Two</p></u> using Nokogiri.
My result should be:
<b><p>Three</p></b>
<p>Four</p>
<table>
<tr><td>Five</td></tr>
<tr><td>Six</td></tr>
</table>
The following-sibling XPath axis is what you want here. Your example isn’t valid HTML, and Nokogiri will change it when parsing as HTML making it hard to demonstrate using it, but with this similar code:
<html>
<body>
<p>one</p>
<p>Two</p>
<p>Three</p>
<p>Four</p>
<table>
<tr><td>Five</td></tr>
<tr><td>Six</td></tr>
</table>
</body>
</html>
this XPath expression:
//p[.="Two"]/following-sibling::*
will select this:
<p>Three</p>
<p>Four</p>
<table>
<tr><td>Five</td></tr>
<tr><td>Six</td></tr>
</table>
You might want to use node() instead of *, which will select all text nodes as well as elements (including whitespace only nodes):
<p>Three</p>
<p>Four</p>
<table>
<tr><td>Five</td></tr>
<tr><td>Six</td></tr>
</table>
(There will be some more leading whitespace on each line if you do this, I‘ve removed it here.)

Resources