Selenium and JSF 2.0

When I generate SelectOneMenu with JSF2.0, the identifier I specified in xhtml binds to the generated identifier from JSF.

eg. from my_fancy_id it generates j_idt9:my_fancy_id

Now I want to test my page using the Selenium 2 web driver. I'm trying to find my menu:

 driver.findElement(By.id("my_fancy_id")); 

Of course, it does not find anything, because the identifier is changed. What is the best way to find a selection menu on a page?

+4
source share
3 answers

Typically, the form identifier is added to all element identifiers within the form. If you do not set the form identifier, JSF will do it for you ("j_idt9"). Decision. Assign an identifier to your form and try using the full identifier in the findElement method, for example:

 <h:form id="myForm"> ... </h:form> 

Call it like this:

 driver.findElement(By.id("myForm:my_fancy_id")); 
+6
source

or you can add <h: form prependId = "false"> so that the form id will not be added

+3
source

You set the identifier of the component on the controls; renderers give the client identifier to the markup.

This allows JSF to emit valid HTML identifiers (they must be unique) even in the face of templates and complex controls. The control will be placed in the names of any parent that is a NamingContainer (e.g., form ).

In some containers, the client identifier will be replaced by a namespace, but this usually only happens in portlet environments.

Some component libraries (such as Tomahawk ) have a forceId attribute, but care should be taken when using them. I wrote a more detailed post about client identifiers here .

+1
source

All Articles