How to achieve target Field with regex in selenium - xpath

I am using selenium and I am using xpath in target value .
I have a table and I need to count the number of rows.
For example I have an X path starting from //tr[2]/td/span/input to //tr[10]/td/span/input. Now I want to count the number of X path count by VerifyXpathCount.
I need to supply a regex in the Target field of Selenium like //tr[(*)]/td/span/input
I am not able to apply such regex so I need to add some regex.
How do I do this?

int cnt = selenium.getXpathCount("//tr[/td/span/input]");
//to deal with all element
for(int i=0;i<cnt;i++){
String loc = "//tr["+i+"]/td/span/input";
//use loc
}
in case if you want to find unique element than you can use other conditions with //tr[/td/span/input] instead of looping

try //tr[./td/span/input] or //tr[//td/span/input] to get/verify xpath count

Related

Extract last word using Xpath 1.0

I need to select only the last word using xpath 1.0. I have something like this:
<Example>
<Ctry> Portugal PT </Ctry>
</Example>
I want to select only the PT word but the order is not exact, i.e: <Ctry> Portugal - Lisbon - PT </Ctry>, but the word i want to extract is always the last one.
I've already tried:
//*[name()='Example'][substring(., string-length(.) - string-length('PT')+1) = 'PT']/text() but extracts always the whole string.
Can anyone help me please?
You're selecting a node using the substring as a predicate to filter out other nodes. If you want the substring to be your output, it shouldn't go inside brackets.
substring(//*[name()='Example'], string-length(//*[name()='Example']) - string-length('PT')+1)
note that /text() can be ommited when working with string functions

Jmeter - Regular expression extractor - multiple random variable

Jmeter 2.9
I am using Regex Extractor to extractor to extract a list of ids from a response message and write them to a variable idList.
Using beanshell, I want to extract multiple random items from the list.
If n is my random position in the list, then I am trying to extract the value from the idList using
String id = "${idList_" + n + "}";
s = ${__V(id)};
If n is 7 then this returns ${idList_7} rather than the value at idList_7.
I have also tried
String id = "idList_" + n;
s = ${__V(id)};
but this returns idList_7 rather than the value.
I have also tried using *__eval*.
Can yo help please?
You need to call a pre-defined variable vars
For example if you have a variable called idList_7 which contains some value you can access the value using following Beanshell code
String s = vars.get("idList_7");
or if you want to use concatenation
int seven = 7;
String s = vars.get("idLst_" + seven);
See How to use Beanshell guide for more detailed explanation on JMeter components which are exposed to Beanshell.

Whats the XPath equivalent to SQL In query?

I would like to know whats the XPath equivalent to SQL In query. Basically in sql i can do this:
select * from tbl1 where Id in (1,2,3,4)
so i want something similar in XPath/Xsl:
i.e.
//*[#id= IN('51417','1121','111')]
Please advice
(In XPath 2,) the = operator always works like in.
I.e. you can use
//*[#id = ('51417','1121','111')]
A solution is to write out the options as separate conditions:
//*[(#id = '51417') or (#id = '1121') or (#id = '111')]
Another, slightly less verbose solution that looks a bit like a hack, though, would be to use the contains function:
//*[contains('-51417-1121-111-', concat('-', #id, '-'))]
Literally, this means you're checking whether the value of the id attribute (preceeded and succeeded by a delimiter character) is a substring of -51417-1121-111-. Note that I am using a hyphen (-) as a delimiter of the allowable values; you can replace that with any character that will not appear in the id attribute.

XPath 2.0:reference earlier context in another part of the XPath expression

in an XPath I would like to focus on certain elements and analyse them:
...
<field>aaa</field>
...
<field>bbb</field>
...
<field>aaa (1)</field>
...
<field>aaa (2)</field>
...
<field>ccc</field>
...
<field>ddd (7)</field>
I want to find the elements who's text content (apart from a possible enumeration, are unique. In the aboce example that would be bbb, ccc and ddd.
The following XPath gives me the unique values:
distinct-values(//field[matches(normalize-space(.), ' \([0-9]\)$')]/substring-before(., '(')))
Now I would like to extent that and perform another XPath on all the distinct values, that would be to count how many field start with either of them and retreive the ones who's count is bigger than 1.
These could be a field content that is equal to that particular value, or it starts witrh that value and is followed by " (". The problem is that in the second part of that XPath I would have refer to the context of that part itself and to the former context at the same time.
In the following XPath I will - instead of using "." as the context- use c_outer and c_inner:
distinct-values(//field[matches(normalize-space(.), ' \([0-9]\)$')]/substring-before(., '(')))[count(//field[(c_inner = c_outer) or starts-with(c_inner, concat(c_outer, ' ('))]) > 1]
I can't use "." for both for obvious reasons. But how could I reference a particular, or the current distinct value from the outer expression within the inner expression?
Would that even be possible?
XQuery can do it e.g.
for $s
in distinct-values(
//field[matches(normalize-space(.), ' \([0-9]\)$')]/substring-before(., '(')))
where count(//field[(. = $s) or starts-with(., concat($s, ' ('))]) > 1
return $s

Ruby String manipulation of HTML Element IDs

I'm looking for a safe way of building a string of a HTML element id from another element id.
I need to go from something like "attr_name_442_a" to "attr-row-422", the text part of the ids are consistent with just the number changing. This number will be in the range of 1 to 4 digits.
I thought of doing this but is there a better Ruby style method?
newID = oldID.gsub("_","-").gsub("name","row").gsub("-a","")
You may be able to pull this off with a single Regex
newID = oldID.gsub /(\w+)_name_(\d+)_a/, '\1-row-\2'
new_id = "#$1-row-#$2" if $old_id =~ /\A([^_]+)_name_(\d{1,4})_a\z/
I would probably add an else condition if the regex didn't match just so I knew I was catching everything.

Resources