Following this discussion:
http://kenai.com/projects/facestester/forums/forum/topics/1283-getFormById-can-only-find-forms-that-are-on-the-current-page?
Example:
page.xhtml contains some input fields (but no form) and uses template.xhtml as template file that contains the form with id "testForm".
In my test class I figured out the following:
Works fine and returns the HtmlForm:
facesPage.getComponentWithId("testForm").getWrappedComponent();
Does not work and brings up a AssertionError("HtmlForm 'testForm' does not exist on page."):
facesPage.getFormById("testForm");
I guess, for me it would be better when getFormById() would use the getComponentWithId() method rather than parsing the page's component tree.
Hi, I found the cause of this issue. It is a bug in getFormById().
The problem is not the template (in a simple application it worked fine with an outer form, hence my summary is wrong), but the nesting of the components.
If we look at getFormById():
for (UIComponent each : component.getChildren()) { ... }
you can see that this will iterate only over the first level. If, like in our case, the form is nested with other components (e.g. using nested templates), it can not be found.
Just surround the <h:form> with <ui:component> to get the form one level deeper in the component tree.
Using getComponentWithId(id).getWrappedComponent(); instead the for loop should resolve it.