我们另一个方面可以测试模板文件,模板文件就是HTML文件,在HTML文件中可能涉及到测试:
*页面字段显示测试
*测试link动作
*测试表单提交form
1. 页面字段显示测试
在我们第1章的Start模板主要是断言显示“Hello World!”字符串。测试程序如清单2.5所示。
- package com.kingbegin.web.pages;
- import org.apache.tapestry.dom.Document;
- import org.apache.tapestry.test.PageTester;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
- public class StartTemplateTest {
- PageTester tester;
- Document doc;
- @Before
- public void setUp() throws Exception {
- String appPackage = "com.kingbegin.web";
- String appName = "App"; // IoC中Modeule名字,默认AppModule.java。
- tester = new PageTester(appPackage, appName, "WebRoot");
- doc = tester.renderPage("Start");
- }
- @Test
- public void testGetMessage() {
- String message = doc.getElementById("label1").getChildMarkup().trim();
- Assert.assertEquals(message, "Hello World!");
- }
- }
PageTester对象是一个测试辅助类,实例化PageTester对象需要三个参数:appPackage是应用程序的基本包、appName是IoC容器的Modeule名字默认AppModule.java,我们的案例中没有Modeule,所以它的默认App、"WebRoot"是模板文件所在的位置。renderPage方法是模拟呈现页面得到一个Document对象,XHTML文件可以看成是一个XML文档,Document对象就是整个的页面,Document的getElementById("label1")方法可以帮助我们获得id为"label1"的Element对象,再调用getChildMarkup()可以获得元素中的内容,因为返回的内容可能还有回车和空格,所以还要调用trim()方法。最后断言label1中的内容为"HelloWorld!"。
- tapestry-test-5.0.11.jar
- If the page requires a context, you can pass it this way:
- public class MyTest extends Assert
- {
- @Test
- public void test1()
- {
- String appPackage = "org.example.app";
- String appName = "LocaleApp";
- PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
- Object[] context = new Object[]{ "abc", 123 };
- Document doc = tester.invoke(new ComponentInvocation(new PageLinkTarget("MyPage"), context));
- assertEquals(doc.getElementById("id1").getChildText(), "hello");
- }
- }
http://tapestry.apache.org/tapestry5/tapestry-core/guide/unit-testing-pages.html
Testing an action link
After rendering a page, you may want to "click" on an action link and then assert against the resulting page. You can do it this way:
- public class MyTest extends Assert
- {
- @Test
- public void test1()
- {
- String appPackage = "org.example.app";
- String appName = "LocaleApp";
- PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
- Document doc = tester.renderPage("MyPage");
- Element link = doc.getElementById("link1");
- doc = tester.clickLink(link);
- assertTrue(doc.toString().contains("abc"));
- }
- }
Testing a form submission
After rendering a page, you may want to fill out a form, submit it and then inspect the resulting page. You can do it this way:
- public class MyTest extends Assert
- {
- @Test
- public void test1()
- {
- String appPackage = "org.example.app";
- String appName = "LocaleApp";
- PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
- Document doc = tester.renderPage("MyPage");
- Element form = doc.getElementById("form1");
- Map<String, String> fieldValues = new HashMap<String, String>();
- fieldValues.put("field1", "hello");
- fieldValues.put("field2", "100");
- doc = tester.submitForm(form, fieldValues);
- assertTrue(doc.toString().contains("abc"));
- }
- }
To submit a form by clicking a submit button, call the clickSubmit() method instead.
Unit testing a component
To unit test a component, just create a test page containing that component. Then unit test that page.