Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out component-path

Tags:

wicket

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?

like image 524
Max_Salah Avatar asked Nov 26 '12 12:11

Max_Salah


3 Answers

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);
}
like image 99
RJo Avatar answered Nov 10 '22 14:11

RJo


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);
        }
    }
}
like image 27
tetsuo Avatar answered Nov 10 '22 15:11

tetsuo


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());
            }
        }
    });
}
like image 38
Stephane L Avatar answered Nov 10 '22 15:11

Stephane L