使用检票口测试器时,如何在单元测试中保持组件路径不变



我有几个针对可排序数据表的检票口测试,特别是 ajax 单击可排序的列标题并断言呈现的正文行的内容。现在,表组件后代的组件层次结构由 wicket 框架自动生成,并生成类似于以下内容的排序链接 (ajax) 路径:

table:topToolbars:toolbars:0:headers:1:header:orderByLink

但是,当 DataTable 跨测试重新呈现时,工具栏组件的索引每次都会递增,即类似于:

table:topToolbars:toolbars:1:headers:1:header:orderByLink

然后中断后续测试的硬编码路径,因为它们将不再匹配。

数据表构造的代码片段如下所示:

        final PayeesProvider dataProvider = new PayeesProvider();
    table = new DataTable<ResponsePayeeDetails>("payees", columns, dataProvider, rowsPerPage);
    table.setOutputMarkupId(true);
    table.addTopToolbar(new AjaxFallbackHeadersToolbar(table, dataProvider) {
        private static final long serialVersionUID = -3509487788284410429L;
        @Override
        protected WebMarkupContainer newSortableHeader(final String borderId, final String property, final ISortStateLocator locator) {
            return new AjaxFallbackOrderByBorder(borderId, property, locator, getAjaxCallDecorator()) {
                @Override
                protected void onRender() {
                    System.out.printf("Path: %sn", this.getPageRelativePath());
                    super.onRender();
                }
                private static final long serialVersionUID = -6399737639959498915L;
                @Override
                protected void onAjaxClick(final AjaxRequestTarget target) {
                    target.add(getTable(), navigator, navigatorInfoContainer);
                }
                @Override
                protected void onSortChanged() {
                    super.onSortChanged();
                    getTable().setCurrentPage(0);
                }
            };
        }
    });
    table.addBottomToolbar(new NoRecordsToolbar(table));
    add(table);

确切地说,当我运行测试时,上面的System.out.printf语句打印:

(第一次测试)

Path: payees:topToolbars:toolbars:0:headers:1:header
Path: payees:topToolbars:toolbars:0:headers:2:header

(第二次测试)

Path: payees:topToolbars:toolbars:2:headers:1:header
Path: payees:topToolbars:toolbars:2:headers:2:header

(第三次测试)

Path: payees:topToolbars:toolbars:4:headers:1:header
Path: payees:topToolbars:toolbars:4:headers:2:header

(第4次测试)

Path: payees:topToolbars:toolbars:6:headers:1:header
Path: payees:topToolbars:toolbars:6:headers:2:header
Path: payees:topToolbars:toolbars:6:headers:1:header
Path: payees:topToolbars:toolbars:6:headers:2:header
Path: payees:topToolbars:toolbars:6:headers:1:header
Path: payees:topToolbars:toolbars:6:headers:2:header

(第5次测试)

Path: payees:topToolbars:toolbars:8:headers:1:header
Path: payees:topToolbars:toolbars:8:headers:2:header

有谁知道我如何强制索引生成更具确定性/可重复性。或者,有没有一种方法可以通配符或以其他方式概括路径,以使它们不受这些增量的影响?

任何帮助将不胜感激的小伙子!

由于

默认的DataTable ItemReuseStrategy,每次刷新表时都会生成全新的项目,因此每次 id 都会递增。如果保持 id 相同是重中之重,您可能会有一些运气来实现自定义ItemReuseStrategy

我们遇到了类似的问题,并确定了与您的替代问题一致的策略。有了这个,您可以要求测试检查例如最后呈现页面上的第三行。注意:代码在 Scala 中。

简而言之,为了解决这个问题,我们实施了一种findNthComponentOnPage方法来增强WicketTester

/**
  * @param id Wicket id of component to find
  * @param n 1-based
  * @tparam T class of component to find
  * @return the Nth component of class T appearing on the lastRenderedPage
  */
def findNthComponentOnPage[T <: Component : ClassTag](id: String, n: Int): T = {
  tester.getLastRenderedPage.visitChildren(classTag[T].runtimeClass, new IVisitor[T, T] {
    var count = 0
    override def component(checkbox: T, visit: IVisit[T]): Unit = {
      if (checkbox.getId == id) {
        count += 1
        if (count == n) {
          visit.stop(checkbox)
        }
      }
    }
  })
}

确定路径可能非常困难,但不是必需的,因为这将包括集成测试而不是单元测试。测试列的排序不应依赖于周围的表,因此您可以在测试用例中创建一个虚拟表,为测试添加标题和一列(要排序的列)。如果您有工具栏对象,则可以立即使用它,也可以通过发出 getComponent(toolbar.getPageRelativePath()) 来重新检索它。但是,作为此类的用户,确保AjaxFallbackOrderByBorder实际工作不应该是你关心的问题。这应该由类的创建者完成。您只需要测试自己的代码(例如数据提供程序中的排序代码)...

最新更新