Kontakt

Find Your Elements

Posted on Donnerstag, 18th November, 2010

In the last post I described the enhancements on PageTester which will improve the testability of your Tapestry applications. In this post I would like to show you a further improvement which will make your life easier when writing tests. In the following example you can see a simple example on how to render a page named SimplePage. The markup of the page contains a link that you want to click inside the test. So, the returned Document instance can be used to find the link by its client id. This can be done by passing the id of the link to th getElementById() method. The result of the method is an Element, if found, which represents the link.

public class SimplePageTest {

   private PageTester tester;

   @Test
   public void click() {
      Document doc = tester.renderPage("SimplePage");

      Element link = doc.getElementById("myLink");

      doc = tester.clickLink(link);

      assertTrue(doc.toString().contains(
         "Tapestry 5 Test"));
   }

   ...
}

Frequently it is easier to find an element inside a document by its name or any arbitrary attribute than by the client id, but unfortunately this was not possible with Tapestry till now. So last week I committed some changes which will be available in the upcoming 5.2.4 release.

In the following example you can see an example how to find an element which has class attribute set to value fancy.

public class SimplePageTest {

   private PageTester tester;

   @Test
   public void click() {
      Document doc = tester.renderPage("SimplePage");

      Element root = doc.getRootElement();

      Element link = root.getElementByAttributeValue(
                          "class", "fancy");

      doc = tester.clickLink(link);

      assertTrue(doc.toString().contains(
            "Tapestry 5 Test"));
   }

   ...
}

If you want more flexibility when finding your elements, you may provide a Predicate which is responsible to accept or decline an Element. This predicate will be used when Tapestry iterrates over all elements inside a Document. The first accepted Element is returned. Note that widht-first search is performed.

public class SimplePageTest {

   private PageTester tester;

   @Test
   public void click() {
      Document doc = tester.renderPage("SimplePage");

      Element root = doc.getRootElement();

      Element link = root.getElement(
         new Predicate<Element>(){
            public boolean accept(Element e) {
                return "a".equals(e.getName());
            }
        });

     doc = tester.clickLink(link);

      assertTrue(doc.toString().contains(
            "Tapestry 5 Test"));
   }

   ...
}

Hope this helps you to improve the tests of your Tapestry applications.

 

avatar

Your name

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Tapestry 5 Blog - Copyright © 2009 - Eclectic Theme by Your Inspiration Web - Powered by WordPress