/enhanced-wickettester

Framework for comfortable testing of wicket pages and components.

Primary LanguageJavaApache License 2.0Apache-2.0

Enhanced WicketTester

Description

The WicketTester is a helper class to ease unit testing of wicket component and pages. There is no need for a servlet container, because it uses a mocked servlet context and a mocked wicket application. In the following it is possible to test the rendered page and the contained components. This is mostly done by selecting components with their wicket path as string. As you can image this is a common error on refactoring components on pages which results in changed wicket paths.

The following example shows how the WicketTester renders a test page and asserts that a bookmarkable link is present to that page.

//given
final WicketTester tester = new WicketTester();
tester.startPage(new TestPage());

//when
final Component link = tester.getComponentFromLastRenderedPage("some:wicket:path");

//then
assertThat(link, is(not(nullValue())));

Now this example can be rewritten selecting the component using the component matchers. The Enhanced WicketTester uses a component matcher construct for several functions you are already used to call with a wicket-path.

//given
final EnhancedWicketTester tester = new EnhancedWicketTester();
tester.startPage(new TestPage());

//when
final BookmarkablePageLink<?> link = tester.getChildMatching(
    ComponentMatchers.type(BookmarkablePageLink.class));

//then
assertThat(link, is(not(nullValue())));

Features

  • select components without the need of the wicket path
  • base wickettester functionality using component matchers

Usage

Selectors for components can be formed using the ComponentMatcherBuilder which uses a builder pattern:

ComponentMatchers.type(BookmarkablePageLink.class).wicketId("link").visible();

Selectors can be combined together:

ComponentMatchers.type(BookmarkablePageLink.class).wicketId("link").visible()
    .havingChild(ComponentMatchers.type(BookmarkablePageLink.class));

Selectors

In the following you will find a list of all selectors possible:

ComponentMatcherBuilder<T> type(final Class<T> componentClass)
ComponentMatcherBuilder<T> wicketId(final String wicketId)
ComponentMatcherBuilder<T> visible()
ComponentMatcherBuilder<T> enabled()
ComponentMatcherBuilder<T> type(final Class<T> componentClass)
ComponentMatcherBuilder<T> not(final ComponentMatcherBuilder<T> builder)
ComponentMatcherBuilder<T> modelObject(final Object modelObject)
ComponentMatcherBuilder<T> havingChild(final ComponentMatcherBuilder<MT> builder)
ComponentMatcherBuilder<T> havingDirectChild(final ComponentMatcherBuilder<MT> builder)
ComponentMatcherBuilder<T> havingSibling(final ComponentMatcherBuilder<MT> builder)
ComponentMatcherBuilder<T> havingDirectParent(final ComponentMatcherBuilder<MT> builder)

WicketTester functions

Select a list of children

List<T> getChildrenMatching(final MarkupContainer root, final ComponentMatcherBuilder<T> builder)
List<T> getChildrenMatching(final ComponentMatcherBuilder<T> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders) 
List<T> getChildrenMatching(final ComponentMatcherBuilder<T> builder) 

Select a single child

T getChildMatching(final MarkupContainer root, final ComponentMatcherBuilder<T> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)
T getChildMatching(final MarkupContainer root, final ComponentMatcherBuilder<T> builder) 
T getChildMatching(final ComponentMatcherBuilder<T> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)
T getChildMatching(final ComponentMatcherBuilder<T> builder)

Select first child of a list of children

T getFirstChildMatching(final MarkupContainer root, final ComponentMatcherBuilder<T> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)
T getFirstChildMatching(final ComponentMatcherBuilder<T> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)
T getFirstChildMatching(final MarkupContainer root, final ComponentMatcherBuilder<T> builder) 
T getFirstChildMatching(final ComponentMatcherBuilder<T> builder) 

Get the wicket-path

String getPathRelativeToRoot(final Component root, final Component component) 
String getPathRelativeToPage(final Component component) 

Create a TagTester for attribute testing

TagTester getTagTesterByComponent(final Component component) 
TagTester getTagTesterByComponentMatcher(final ComponentMatcherBuilder<? extends Component> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders) 

Create a FormTester

FormTester newFormTester(final Form<?> form)
FormTester newFormTester(final ComponentMatcherBuilder<? extends Form> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)

Execution

void clickLink(final ComponentMatcherBuilder<? extends Component> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)
void executeAjaxEvent(final String event, 
    final ComponentMatcherBuilder<? extends Component> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)

Assertion helpers

void assertModelValue(final Object expectedModelObject, 
    final ComponentMatcherBuilder<? extends Component> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)
void assertVisible(final Component component) 
void assertVisible(final ComponentMatcherBuilder<? extends Component> builder,
    final ComponentMatcherBuilder<? extends MarkupContainer>... parentBuilders)
void assertInvisible(final Label component) 
void assertFeedback(final ComponentMatcherBuilder<? extends Component> builder, 
    final String... feedback) 

Gradle, Maven

You can find all releases in Maven Central and in the public Sonatype repository:

https://oss.sonatype.org/content/repositories/releases

The current release is 0.2:

  • de.otto:enhanced-wickettester:0.2

For development usage you should use the snapshot instead:

https://oss.sonatype.org/content/repositories/snapshots

The current snapshot-release is 0.3-SNAPSHOT:

  • de.otto:enhanced-wickettester:0.3-SNAPSHOT

License

Apache 2