I'm using JSF 1.2. We want to write some selenium tests (based on xpath) but xpath does not seem to have wildcard matching on element id's.
We cannot turn off prefix ids as we are running as a portlet within IBM Portal Server 6.1 and our application breaks in that environment with the prefix turned off.
Currently we are using xpaths of the form
//*[substring(@id, 54)='id_distributorName']
which will match:
<select size="1" class="firstName" name="viewns_7_8000CB1A0GUIE0IJF799CR10O2_:commonEntryForm:id_distributorName" id="viewns_7_8000CB1A0GUIE0IJF799CR10O2_:commonEntryForm:id_distributorName" >
but it strikes me that assuming JSF will always generate a viewId of the same length is dangerous.
Is there a better way to do this?
We've tried using the name
attribute for our input controls but of course JSF ignores the attribute and writes it's own name attribute whose value matches the id presumably for evant handling scripting reasons)
If you're using XPath 2.0, just use ends-with()
function:
//*[ends-with(@id, ':id_distributorName')]
If you're using XPath 1.0, use string-length()
to calculate the begin of substring()
:
//*[substring(@id, string-length(@id) - 18) = ':id_distributorName']
Here, 18 is the length of id_distributorName
(without the :
prefix!).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With