Launchers

From LaughingPanda

This tutorial explains how to use launcher features the plugin provides. The launching mechanism has been designed so that it is possible to test Wicket components in isolation from rest of the Wicket application. This style of development is most beneficial when the application is rather large (takes time to reboot) and has pages containing many components.

Install wicket-bench-0.5.0 from http://www.laughingpanda.org/mediawiki/index.php/Wicket_Bench

An example application is a simple English-Finnish dictionary. A user can enter English words and dictionary will return Finnish translations. The source code for the example can be checked out from http://svn.laughingpanda.org/svn/wicket-bench/trunk/wicket-bench-test/ After checkout run 'mvn -Dmaven.test.skip=true install' once for maven to download all the required libs to your local repository. Set M2_REPO eclipse classpath variable to point to your local maven repository.

Verify installation by opening src/test/java/test/DictionaryPanelTest.java. Then right-click and select Run as -> Wicket Bench. This should deploy the component and open a browser view.

Note! If you get connection refused error, wait a bit and press refresh.

Type 'cat' to a search field and see what it is in Finnish. Then, start Selenium server: java -jar selenium-server-0.9.2.jar. After that run src/test/java/test/AllTests.java as a JUnit Test. This should run a suite of automated Selenium tests using Firefox browser.

Note! If nothing happens, see the console where you started Selenium server. 
Sometimes Selenium can't find Firefox. 
E.g. I need to set PATH in my environment as 'export PATH=$PATH:/opt/firefox/'

The launcher works by starting jetty servlet container. The component under test is then deployed to URL http://localhost:8090/wicket-bench/home. The component is set up by implementing method 'IComponentFactory createFactory()' from WicketBenchTestCase. The returned IComponentFactory will be used to instantiate the component:


    public IComponentFactory<MarkupContainer> createFactory() {
        return new IComponentFactory<MarkupContainer>() {
            public DictionaryPanel createComponent(String componentId) {
                return new DictionaryPanel(componentId);
            }
        };
    }

In this simple example DictionaryPanel is just instantiated. Often the components may need some services or daos however and 'createComponent' is a place where these may be injected. This is all there needs to be to use 'Run as -> Wicket Bench' feature.


IComponentFactory.createComponent(String componentId) is called within Wicket stack. 
Therefore it is possible to access Wicket session and request. E.g. 

            public Component createComponent(String componentId) {
                ((MySession) Session.get()).logIn(new User("test"));
                return new MyPanel(componentId);
            }

The same JUnit test can be used to add Selenium tests to automate testing of the components.


    public void testSearchPanelOnSelenium() {
        openSelenium(createFactory());
        getSelenium().type("query", "cat");
        getSelenium().click("submit");
        getSelenium().waitForPageToLoad("5000");
        String text = getSelenium().getBodyText();
        assertTrue(text.contains("kissa"));
    }

The tests can be grouped as a suite by extending wicketbench.junit.AbstractTestSuite:


    public class AllTests extends AbstractTestSuite {
        public static Test suite() throws Exception {
            AllTests suite = new AllTests();
            suite.addTest(suite("target/test-classes"));
            return suite;
        }
    }

By default the launched component is attached to a wicketbench.web.WicketBenchApplication. The application class can be changed by calling WicketBench.setApplicationClassName("com.example.MyApp"); This is often needed for instance when using Spring injection or custom wicket.Session.

Non packaged resources can be configured from Properties -> Wicket -> Web resources. Set the context root and all css files. Css files are contributed for deployed component. The settings are saved at project root to a file .wicketprops.

Example maven2 project file is at http://svn.laughingpanda.org/svn/wicket-bench/trunk/wicket-bench-test/pom.xml

Selenium tests can be run from command line too. 'mvn test' will run all tests. That way it is possible to run tests with continuous integration tool.