我通常会尽量减少使用 Selenium 的测试,并最大限度地使用普通的旧后端测试(JUnit,模拟)。使用 Tapestry,我发现很难以后一种方式测试页面和组件,因为回调函数具有“魔力”。
你能解决这个问题吗?或者您只是将 Selenium 用于整个 Web 层(页面、组件)?
根据 Tapestry 文档,使用 PageTester 是对页面和组件进行单元测试的适当方法:https ://tapestry.apache.org/unit-testing-pages-or-components.html
但这似乎类似于 HtmlUnit 样式的 Web 测试,因为交互是通过类似 Web 浏览器的界面而不是通过页面或组件的界面发生的。
编辑
我刚刚尝试了一个简单的页面单元测试,效果很好:
public class FooPageTest extends AbstractServiceTest{
@Autobuild
@Inject
private FooPage fooPage;
@Test
public void setupRender(){
fooPage.setupRender();
}
}
AbstractServiceTest 提供了一个测试运行器,它为单元测试类提供 Tapestry 依赖注入。使用 Autobuild,您可以满足 FooPage 的 @Inject 依赖关系,并且对于组件注入和 @Property 注释元素,您需要找出其他东西。
只是为了具体说明蒂莫的建议:
public class AbstractServiceTest
{
@Before
public void before() throws IllegalAccessException {
// startupRegistry();
injectServices();
}
private void injectServices() throws IllegalAccessException {
for(Field field : getClass().getDeclaredFields()) {
field.setAccessible(true);
if(field.isAnnotationPresent(Inject.class))
field.set(this, registry.getService(field.getType()));
if(field.isAnnotationPresent(Autobuild.class))
field.set(this, registry.autobuild(field.getType()));
}
}
}
然后,您将在测试中正确注入字段。记住你@Inject 服务(接口)和你@Autobuild 实现(类)