How to find out component-path
Asked Answered
S

3

7

I use junit to assert the existing of wicket components:

wicketTester.assertComponent("dev1WicketId:dev2WicketId:formWicketId", Form.class);

This works for some forms. For complex structure, it is defficult to find out the path of the form by searching all html files. Is there any method how to find out the path easy?

Streamlined answered 26/11, 2012 at 12:45 Comment(0)
Z
9

If you have the component you can call #getPageRelativePath(). E.g.

// Supposing c is a component that has been added to the page.
// Returns the full path to the component relative to the page, e.g., "path:to:label"
String pathToComponent = c.getPageRelativePath();

You can get the children of a markup container by using the visitChildren() method. The following example shows how to get all the Forms from a page.

List<Form> list = new ArrayList<Form<?>>();
Page page = wicketTester.getLastRenderedPage();
for (Form form : page.visitChildren(Form.class)) {
    list.add(form);
}
Zachary answered 26/11, 2012 at 13:0 Comment(0)
T
7

An easy way to get those is to call getDebugSettings().setOutputComponentPath(true); when initializing your application. This will make Wicket to output these paths to the generated HTML as an attribute on every component-bound tag.

It's recommended to only enable this on debug mode, though:

public class WicketApplication extends WebApplication {
    @Override
    public void init() {
        super.init();

        if (getConfigurationType() == RuntimeConfigurationType.DEVELOPMENT) {
            getDebugSettings().setOutputComponentPath(true);
        }
    }
}
Tetragonal answered 26/11, 2012 at 19:8 Comment(1)
it does not work with last version of wicketPantsuit
K
1

Extending the RJo's answer.

It seems that the method page.visitChildren(<Class>) is deprecated (Wicket 6), so with an IVisitor it can be :

protected String findPathComponentOnLastRenderedPage(final String idComponent) {
    final Page page = wicketTester.getLastRenderedPage();
    return page.visitChildren(Component.class, new IVisitor<Component, String>() {
        @Override
        public void component(final Component component, final IVisit<String> visit) {
            if (component.getId().equals(idComponent)) {
                visit.stop(component.getPageRelativePath());
            }
        }
    });
}
Kala answered 24/9, 2016 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.