查找表中的所有行,即使您必须向下滚动表

时间:2019-04-04 13:30:39

标签: kotlin vaadin ui-testing vaadin8 selenide

我正在尝试为Vaadin 8教程应用程序编写一个UI测试,该测试将对表中的行进行计数。使用Selenide和CSS选择器进行测试。

该表是可滚动的,其创建方式如下:

override fun init(vaadinRequest: VaadinRequest) {
    layout = VerticalLayout()
    grid = Grid(Customer::class.java)
    grid.id = "table.customers"
    grid.setColumns("firstName","lastName","email")
    layout.addComponents(grid)

    populateGrid()

    this.content = layout
}

private fun populateGrid() {
    val customers = customerService.findAll()
    grid.setItems(customers)
}

此测试在这里

@Test
fun displaysAllData(){
    //given
    val expectedCustomers = customerService.findAll()
    open("/customers")

    //when
    val displayedCustomers =  $(byId("table.customers"))
            .find("tbody:first-of-type")
            .findAll("tr")

    displayedCustomers.forEach{ println(it)}

    //then
    assertThat(displayedCustomers.size).isEqualTo(expectedCustomers.size)
}

给我

<tr class="v-grid-row v-grid-row-focused v-grid-row-has-data">Bernard Nilsen bernard@nilsen.com</tr>
<tr class="v-grid-row v-grid-row-stripe v-grid-row-has-data">Jaydan Jackson jaydan@jackson.com</tr>
<tr class="v-grid-row v-grid-row-has-data">Solomon Olsen solomon@olsen.com</tr>
<tr class="v-grid-row v-grid-row-stripe v-grid-row-has-data">Elvis Olsen elvis@olsen.com</tr>
<tr class="v-grid-row v-grid-row-has-data">Rene Carlsson rene@carlsson.com</tr>
<tr class="v-grid-row v-grid-row-stripe v-grid-row-has-data">Remington Andersson remington@andersson.com</tr>
<tr class="v-grid-row v-grid-row-has-data">Ann Andersson ann@andersson.com</tr>
<tr class="v-grid-row v-grid-row-stripe v-grid-row-has-data">Lara Martin lara@martin.com</tr>
<tr class="v-grid-row v-grid-row-has-data">Jamar Olsson jamar@olsson.com</tr>
<tr class="v-grid-row v-grid-row-stripe v-grid-row-has-data">Gunner Karlsen gunner@karlsen.com</tr>
<tr class="v-grid-row v-grid-row-has-data" displayed:false></tr>

org.opentest4j.AssertionFailedError: 
Expecting:
 <11>
to be equal to:
 <30>
but was not.
Expected :30
Actual   :11

不过,我确实是手工计算的,如果您滚动浏览该表,该表中有30行。

我还尝试过在Thread.sleep之后插入open(...),以防单元测试太急躁而又无法读取整个表。

这些是CSS选择器。如何获取表中的所有行(是否显示)?

1 个答案:

答案 0 :(得分:2)

浏览器中的Grid实现基于名为Escalator的子组件。自动扶梯的想法是重新使用行DOM元素。即滚动网格时,消失的行将移动到另一端并填充新数据(根据某些规则从服务器延迟加载)。因此,网格的“所有行”在浏览器中都是较新的。