4

We are about to write a full set of tests for one of our JSF applications using Selenium.

So far, it seems that there are two preferred approaches to uniquely identify each element: by ID or using a unique class name. The later is really a hack and doesn't make sense semantically. The former is the right approach, but the element IDs are generated by JSF.

All the different JSF implementations I've seen seem to be using the same approach: use the parent element as the namespace and then concatenate the element ID using a colon. Fair enough.

The question is: do you know if this is guaranteed in some part of the JSF specification? It'd be a real problem to find out later that we need to rewrite all the component selectors in the tests just because JSF x.y changed the way it generates the ID names.

Thanks!

Juan
  • 989
  • 1
  • 14
  • 22

3 Answers3

4

JSF usually generated the ID of components, if ID attribute is not explicitly mentioned. It will be generated in the format j_idXXX (XXX will be number incremented)

<h:form id="LoginForm">
    <h:inputText id="userName" .../>
</h:form>

for this inputText the id will be formed as LoginForm:userName and if id is not mentioned explicitly,then it will be formed something like LoginForm:j_id15

This is mentioned in JSF specification in section 3.1.6, But the exact format is not specified though. The clientId is generated using this method UIComponent.getClientId(); Follow this link UIComponent

Vivek
  • 182
  • 1
  • 8
2

Is the ID generated by JSF guaranteed to be the same across different versions and implementations?

No. You've to explicitly specify the component ID on the UIInput component of interest and all of its parent UINamingContainer components such as <h:form>, <ui:repeat>, <h:dataTable>, etc yourself. Those IDs will by default be woodstocked using separator character :.

However, the separator character is in turn configureable since JSF 2.0. So, if you change the separator character for your webapp from : to - or something, then you'd have to rewrite the selenium tests which are relying on the HTML element IDs.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

From the JSF (2.1) spec:

The client identifier is derived from the component identifier (or the result of calling UIViewRoot.createUniqueId() if there is not one), and the client identifier of the closest parent component that is a NamingContainer according to the algorithm specified in the javadoc for UIComponent.getClientId(). The Renderer associated with this component, if any, will then be asked to convert this client identifier to a form appropriate for sending to the client. The value returned from this method must be the same throughout the lifetime of the component instance unless setId() is called, in which case it will be recalculated by the next call to getClientId().

Aside from the spec, 3rd party plugins can affect the client identifier (e.g. protlet bridge APIs)

McDowell
  • 107,573
  • 31
  • 204
  • 267