Why is my JMeter XPath assertion failing? - xpath

I'm trying to run a simple load test, but some of my XPath assertions are failing and I can't figure out why. Here is a sample of html:
<div class="g1260">
<h1 class="top-head">
<div id="ctl00_mainContent_ctl00">
INVESTMENT CONSULTANTS
</div>
</h1>
</div>
and here is my XPath:
//div[#id="ctl00_mainContent_ctl00" and contains(text(),"INVESTMENT CONSULTANTS")]
The assertion fails, even though, when I look at the response in the results tree, the element is there.
I have also tried the XPaths
//div[#class="g1260"]/h1[#class="top-head"]/div
which fails, and
//div[#class="g1260"]/h1[#class="top-head"]
which passes.
All of the above XPaths work ok in the developer console, but fail in JMeter. What is going on here?

I cannot reproduce your issue, looking into XPath Tester mode of the View Results Tree listener:
Also your expression checks only for node presence, if you need to test whether text equals INVESTMENT CONSULTANTS you will need to amend your XPath expression to look like:
normalize-space(//div[#id="ctl00_mainContent_ctl00"]/text())='INVESTMENT CONSULTANTS'
Depending on the nature of your response you might (or might not) need to tick Use Tidy box, try the both options, if you are using Tidy - check jmeter.log file for any suspicious entries.
If the above steps won't help - share your full response and assertion requirement and we'll try to come up with the most appropriate configuration.
In the meantime check out this XPath Tutorial

Related

Jmeter: String Index Out of Bounds Exceptions on Xpath

Can some one please please help me on this. I am tired of looking into it.
In while loop I am reading data from csv file, navigate to web pages and get Xpath values.
I get String Index out of bounds exceptions on Xpath.
Please see this screenshot of program
Steps to execute were
1.dealers.tt -- HTTP request
2.edit_integrtaion_details.tt --- HTTP request
3.Dealer details -- HTTP Request
then Xpath entities under Dealer details
For some reason some of the records read from file... only 2 steps get executed as below.leaving other steps (3 and Xpath)
dealers.tt
edit_integrtaion_details.tt
then I get an error as
'Dealer details' : java.lang.StringIndexOutOfBoundsException: String index out of range: 8193
I tried selecting redirect automatically and follow redirects in HTTP requests to force step 3 to execute. In both cases get this error.There are no spaces for the name I get from Xpath
Most likely you are sufferin from JTidy issue #205, the options are in:
Uncheck "Use Tidy" box, if your response is valid XML or XHTML it might be the case you don't need it (unlikely though)
Compile latest JTidy from the source code and once done replace jtidy-r938.jar in "lib" folder of your JMeter installation with the brand new JTidy jar.
Switch to CSS/JQuery Extractor
Switch to Regular Expression Extractor
If XPath is totally a must you can also consider JSR223 PostProcessor and Groovy language, it has built-in support of some form of XPath. Check out Groovy Is the New Black article for more details.

Extracting a link with jmeter

So I need to delete an "onclick" dynamic link using jmeter.
Here is the sample of one of the links:
"Delete"
What I need is to extract number and post it in order to do the delete action. Every link is the same except the number.
I have tried to implement some of the solutions I've found on this site but it didn't work.
Thanks in advance
Peace
If you need to do it with XPath you could try going for substring-after function like:
substring-after(//a[text()='Delete']/#href,'param=')
The above expression returns everything which is after param= text in href attribute of a HTML tag having Delete text.
You can test your XPath expressions against actual server response using XPath Tester tab of the View Results Tree listener.
References:
substring-after Function Reference
XPath 1.0 Language Reference
Using the XPath Extractor in JMeter
XPath Tutorial

Extract value from javascript object in site using xpath and import.io

I want extract a number provided by javascript object in site, but I really don't understand that I am doing.
I tried different versions using alike examples and guidelines in import.io site and other tutorial sites, but I got only 1 of two results: extracted all numbers on given page or nothing at all.
I tried e.g. //[contains(.,"Unikālo apmeklējumu skaits:")]#type ; //[contains(.,"Unikālo apmeklējumu skaits:")] . Most likely it's necessary to add there something else, but I just don't know that.
Link I am interested in to extract from is: https://www.ss.lv/msg/lv/clothes-footwear/womens-clothes/trousers/ikcbb.html and information necessary is a number after text "Unikālo apmeklējumu skaits:" which is given by javascript.
Hopefully someone will be able to help me with this problem.
For someone who is new in web-scraping this should be a hard task, I'll ty to explain it. First of all, the xpath to get to that location could be something like this:
'//td[#class="msg_footer" and contains(text(), "Unik")]'
Now you have that tag (and what it contains), but if you check it doesn't contain the number you need, that content is being dynamically loaded with a javascript, and the javascript is this one:
<script type="text/javascript"><!--
var ss_w='rādīt numuru';
document.write( '<scr'+'ipt id="contacts_js" src="/js/2015-10-27/37863/VHoBGkpqSV8bfwkdTX9AXEpZXCVDlASIQ1ZV3kK.js?t='+new Date()+'"></scr'+'ipt>' );
--></script>
which could be gotten from the response with this xpath:
'//script[contains(text(), "contacts_js")]/text()'
from that string, you should replicate the url that comes in src, so this url for example:
/js/2015-10-27/37863/VHoBGkpqSV8bfwkdTX9AXEpZXCVDlASIQ1ZV3kK.js?t=
and add to the end the current date, as javascript creates it with new Date(). Then you should make a request to that url (adding the previous response domain), so something like:
https://www.ss.lv/js/2015-10-27/37863/VHoBGkpqSV8bfwkdTX9AXEpZXCVDlASIQ1ZV3kK.js?t=Wed%20Oct%2028%202015%2020:56:42%20GMT-0500%20(PET)
check that the date is urlencoded. it should return a response like:
var PHONE_CNT=-1;var PHONE_CNT2=-1;var PHONE_CNT3=-1;var EMAIL_CNT=-1;var SHOW_CNT=22;var PH_c="";var PH_1=0;var PH_2=0;var PH_3=0;
pcc_id=0;PH_1=gpzd("JTg3aCU3QyU1QnolN0MlN0JYcWh6JTVCdCU5NSU4QyU5MnV4ayU5QXElN0IlOTQlNUNweiU5MGtvJTdCJThFJTVF","55937369");
where you can check that the value inside SHOW_CNT is the number you want.
If you want to know how I figured out which request and which script was populating that response tag, well that I did using firebug, searching for SHOW_CNT inside all of the responses that involve calling to your URL, which pointed to the request I specified, and then trying to check who was requesting that.
Hope it helped.
support#import.io are the guys to speak to, they give free advice and help trouble shoot problems just like this all the time.
There are all kinds of tips and tricks you can use... for example import.io provide (an undocumented beta) JavaScript Pre-render service that would likely work for you in this scenario. API publish failures are sometimes caused by timeouts while waiting for sites to render JS, this would fix that.
http://support.import.io/knowledgebase/articles/623235-infinite-scroll-and-javascript-prerender-beta
I hope this helps.

How can I get the value from a hidden input field using JMeter

I am trying to get the value from a hidden input field. I researched and found many sites ( including instances on this site ) showing great examples. When I try them, I am not able to retrieve the value from this hidden field using the methods I have learned. I have tried both an xpath extractor and a regular expression extractor, but neither one retrieves the value from the hidden field.
Also, I will note that in the tree on the left side in JMeter, I put the extractors as a child to the HTTP Request where the token first appears. Are the extractors supposed to be children or are they supposed to be at the same level as the HTTP Request, but just after it in the flow of the test?
==============================
Here, I will explain my set up. There is an HTTP request from a form. There is a token on the form. I need to get the value for this token. Here is the html for the page where the token appears:
<form action="/folder1/part1/save/12345-1234-1234-1234-123456789012" method="post" name="mgr" id="mgr" >
<input type="hidden" name="token" value="1234-12-12-12-1234" id="token" />
==============================
For the Regular Expression Extractor, I have tried all of these, one at a time for each test run:
//input[#type="hidden" and name="token"]/#value
//input[#type="hidden"][#name="token"]/#value
//input[#type="hidden"]/[#name="token"]/#value
//input[#type="hidden"][#name="token"]/#value/[#id="token"]
//input[#type="hidden"]/[#name="token"]/#value/[#id="token"]
For the XPath Extractor, I have tried all of these, one at a time for each test run:
//[#id="token"]
/html/body/div/div[2]/div/form/input[1]
//html/body/div/div[2]/div/form/input[1]
Try this one: //input[#type="hidden" and #name="token"]/#value
Also, you could test your expressions exactly in JMeter. For example, this xpath extraction you could check in XPath Tester element in View Result Tree Listener. There you could find Regexp Tester too

JMeter XPath results are missing expected items

Using XPath assertion: //ul/#class after a HTTP request, the assertion returns:
success_message_1=links
success_message_2=accordion vertnav vertnav-top grid-full
success_message_3=grid-full classic
success_message_4=checkout-types
success_message_5=links
success_message_matchNr=5
It is missing exactly one element, which happens to be the success message I need to find. The structure of the success message is:
<ul class="messages"><li class="success-msg"><ul><li>Message</li></ul></li></ul>
Strangely targeting the item with //li[#class="success-msg"] returns nothing. Using jQuery I did a simple jQuery('ul') and it matches all of the <ul> including the missing one.
I've checked the response and the success message does exist in the response, but why can't I select it with XPath?
The correct XPath expression if you need to obtain "Message" "vanilla" XPath selector will look like
//li[#class='success-msg']/ul/li/text()
If your page is not XML/XHTML compliant you may require to enable "Use Tidy" box in your XPath Assertion.
Particularly for your code following XPath Assertion works for me:
//li/text()='Message'
You may wish to use XPath Extractor combiner with Debug Sampler and View Results Tree listener to see what is the output of your XPath queries and once you'll get "true" - the query is OK to be used in the assertion.

Resources