JSF Id dynamic view item id identifier

I am using JSF 1.2. We want to write some selenium tests (based on xpath), but xpath does not seem to have wildcards for element identifiers.

We cannot disable prefix identifiers because we are launching the portlet in IBM Portal Server 6.1, and our application is terminated in this environment with the prefix disabled.

We are currently 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 seems to me that assuming that JSF always generates viewId with the same length is dangerous.

Is there a better way to do this?

We tried to use the attribute namefor our input controls, but of course, JSF ignores the attribute and writes its own name attribute whose value matches the identifier, presumably for explaining the evant scripts)

+5
source share
3 answers

If you are using XPath 2.0, just use the function ends-with():

//*[ends-with(@id, ':id_distributorName')]

If you are using XPath 1.0, use string-length()to calculate the beginning substring():

//*[substring(@id, string-length(@id) - 18) = ':id_distributorName']

Here 18 is the length id_distributorName(no prefix :!).

+5
source

Ok, I just answered my question.

I admit that I know little about xpaths, this is my excuse!

The answer is to use contains()

i.e. //*[contains(@id, 'id_distributorName')]

+4
source

Using

//*[substring-after(@id, ':commonEntryForm:') = 'id_distributorName']
0
source

All Articles